Skip to content

Commit

Permalink
fix tr indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
wizard50 committed Mar 29, 2024
1 parent 6958d5b commit 0cfd04d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/indicator/src/ta/indicator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[tablecloth.api :as tc]
[ta.indicator.helper :refer [indicator]]
[ta.indicator.signal :refer [upward-change downward-change]]
[ta.indicator.returns :refer [diff-2col]]
[ta.helper.ds :refer [has-col]]
[ta.math.series :refer [gauss-summation]])
(:import [clojure.lang PersistentQueue]))
Expand Down Expand Up @@ -153,15 +154,20 @@
hl2))

(defn tr
"input: bar-ds with (:low :high) columns
output: (high-low)"
"input: bar-ds with (:low :high :close) columns
output: Max [(H−L), abs(H−Cprev), abs(L−Cprev)]"
[bar-ds]
(assert (has-col bar-ds :low) "tr needs :low column in bar-ds")
(assert (has-col bar-ds :high) "tr needs :high column in bar-ds")
(let [low (:low bar-ds)
high (:high bar-ds)
hl (dfn/- high low)]
hl))
(assert (has-col bar-ds :close) "tr needs :close column in bar-ds")
(let [{:keys [high low close]} bar-ds
hl (dfn/- high low)
hc (diff-2col high close (first hl))
lc (diff-2col low close (first hl))]
(dfn/max
hl
(dfn/abs hc)
(dfn/abs lc))))

(defn atr
"atr is a mma(n) on (tr bar)"
Expand Down
13 changes: 13 additions & 0 deletions lib/indicator/src/ta/indicator/returns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
(- (integrated-values idx)
(integrated-values (- idx n))))))))

(defn diff-2col
"returns a vector of the difference between 2 columns (shifted)
formula: x = col1 (current) - col2 (prev)
first value is passed as argument"
[col1 col2 v]
(let [len (count col1)]
(dtype/clone
(dtype/make-reader
:float64 len
(if (= idx 0)
v
(- (col1 idx) (col2 (dec idx))))))))

(defn return-stddev [price]
(let [d (diff price)]
(dfn/standard-deviation d)))
Expand Down
1 change: 0 additions & 1 deletion lib/indicator/test/ta/indicator/helper_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

(deftest test-tr
(is (all-fuzzy=
0.1
(ta4j/bar ds :helpers/TR)
(ind/tr ds))))

Expand Down
1 change: 0 additions & 1 deletion lib/indicator/test/ta/indicator/indicator_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

(deftest test-atr
(is (all-fuzzy=
0.1
(ta4j/bar ds :ATR 4)
(ind/atr {:n 4} ds))))

Expand Down
2 changes: 1 addition & 1 deletion lib/indicator/test/ta/indicator/util/data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
{:date (t/instant "2019-11-11T00:00:00.000Z") :open 130.0 :high 150.7 :low 90.055 :close 125.0 :volume 15000}
{:date (t/instant "2019-11-12T00:00:00.000Z") :open 125.0 :high 130.6 :low 90.044 :close 120.0 :volume 12000}
{:date (t/instant "2019-11-13T00:00:00.000Z") :open 120.0 :high 120.0 :low 90.033 :close 110.0 :volume 11000}
{:date (t/instant "2019-11-14T00:00:00.000Z") :open 101.0 :high 110.0 :low 90.022 :close 100.0 :volume 9000}
{:date (t/instant "2019-11-14T00:00:00.000Z") :open 101.0 :high 110.0 :low 88.022 :close 89.0 :volume 9000}
{:date (t/instant "2019-11-15T00:00:00.000Z") :open 100.0 :high 120.0 :low 90.011 :close 110.0 :volume 11000}]))

0 comments on commit 0cfd04d

Please sign in to comment.