Skip to content

Commit

Permalink
added trailing linear regression helper
Browse files Browse the repository at this point in the history
  • Loading branch information
wizard50 committed Apr 2, 2024
1 parent 0cfd04d commit 7ca6564
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 16 additions & 0 deletions lib/indicator/src/ta/indicator/rolling.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@
:datatype :float64})
n (:close bar-ds)))

(defn trailing-linear-regression
"trailing simple linear regression"
[n v]
(let [ds (tc/dataset {:col v})]
(:out (r/rolling ds {:window-type :fixed
:window-size n
:relative-window-position :left
:edge-mode :zero}
{:out {:column-name [:col]
:reducer (fn [w]
(let [y (into [] (drop-while #(= % 0.0) w))
c (count y)
x (range 1 (inc c))
regressor (dfn/linear-regressor x y)]
(regressor c)))}}))))

(defn prior-window
"this does not work!"
[ds]
Expand Down
20 changes: 11 additions & 9 deletions lib/indicator/test/ta/indicator/stats_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
[ta.indicator.util.fuzzy :refer [all-fuzzy= nthrest-fuzzy=]]
[ta.indicator.util.ta4j :as ta4j]
[ta.indicator.util.data :refer [ds]]
[ta.indicator.rolling :as roll]
[tech.v3.datatype.functional :as dfn]))
[ta.indicator.rolling :as roll]))

(deftest test-mad-2
(is (all-fuzzy=
Expand All @@ -18,7 +17,7 @@

(deftest test-mad-4
(is (nthrest-fuzzy= 4
(ta4j/close ds :statistics/MeanDeviation 4)
(ta4j/close ds :statistics/MeanDeviation 4)
(roll/trailing-mad 4 (:close ds)))))

(deftest test-variance
Expand All @@ -32,6 +31,12 @@
(ta4j/close ds :statistics/StandardDeviation 3)
(roll/trailing-stddev 3 (:close ds)))))

(deftest test-linear-regression
(is (nthrest-fuzzy= 1
(ta4j/close ds :statistics/SimpleLinearRegression 3)
(roll/trailing-linear-regression 3 (:close ds)))))


(comment
(:close ds)

Expand All @@ -41,16 +46,13 @@
(ta4j/close ds :statistics/Variance 4)
(roll/trailing-variance 4 (:close ds))



(roll/trailing-stddev 3 (:close ds))
(ta4j/close ds :statistics/StandardDeviation 3)

(ta4j/close ds :statistics/SimpleLinearRegression 3)
(roll/trailing-linear-regression 3 (:close ds))




;
;
)


0 comments on commit 7ca6564

Please sign in to comment.