Skip to content

Commit

Permalink
ta4j bool indicator working
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Mar 28, 2024
1 parent fb54c06 commit ccdbf3e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/indicator/src/ta/indicator/candles.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns ta.indicator.candles
(:require
[tech.v3.datatype.functional :as dfn]
[ta.indicator :as ind]))

(defn doji
"A candle is considered Doji if its body height is lower than the average
multiplied by a factor.
http://stockcharts.com/school/doku.php?id=chart_school:chart_analysis:introduction_to_candlesticks#doji</a>
input: options + bar-ds
output: column with doji-signal (boolean)"
[n k bar-ds]
(assert n "doji needs n parameter")
(assert k "doji needs k parameter")
(let [open-close (dfn/- (:close bar-ds) (:open bar-ds))
avg-open-close (ind/sma {:n n} open-close)
avg-open-close-k (dfn/* avg-open-close k)
prior-avg-open-close-k (ind/prior avg-open-close-k)]
(dfn/< open-close prior-avg-open-close-k)))

(defn doji-absolute
"doji is a bar with Big range + small movement.
input: options + bar-ds
output: column with doji-signal"
[max-open-close-over-low-high bar-ds]
(assert max-open-close-over-low-high "doji needs max-open-close-over-low-high parameter")
(let [low-high (dfn/- (:high bar-ds) (:low bar-ds))
open-close (dfn/- (:close bar-ds) (:open bar-ds))
open-close-over-low-high (dfn// open-close low-high)]
(dfn/< open-close-over-low-high max-open-close-over-low-high)))


9 changes: 9 additions & 0 deletions lib/indicator/src/ta/indicator/ta4j/ta4j.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
(->> (map #(->> % (.getValue ind) .doubleValue)
(range n)))))

(defn ind-values-bool
([ind]
(println "ind-values-bool!")
(ind-values-bool (-> ind .getBarSeries .getBarCount) ind))
([n ind]
(println "get-values-bool: " ind)
(->> (map #(->> % (.getValue ind) .booleanValue)
(range n)))))

(defn num-double [d]
(DoubleNum/valueOf d))

Expand Down
35 changes: 35 additions & 0 deletions lib/indicator/test/ta/indicator/candles_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns ta.indicator.candles-test
(:require [clojure.test :refer :all]
[ta.indicator.util.fuzzy :refer [all-fuzzy=]]
[ta.indicator.util.ta4j :as ta4j]
[ta.indicator.util.data :refer [ds]]
[ta.indicator.candles :as indc]))


;; TESTS

#_(deftest test-doji
(is (all-fuzzy=
0.1
(ta4j/bar ds :volume/VWAP 2)
(vind/vwap 2 ds))))


(comment
(:close ds)

(ta4j/bar-bool ds :candles/Doji 2 1)
;; => (true false false true true
;; false false false true true
;; true true false true false)


(indc/doji 2 1 ds)
;; => [false false false true true
;; false false true true false
;; true true true false false]



;
)
13 changes: 13 additions & 0 deletions lib/indicator/test/ta/indicator/util/ta4j.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
(let [bar (ta4j/ds->ta4j-ohlcv ds)]
(calc-indicator ind-kw bar args)))

;; bool indicator

(defn calc-indicator-bool [ind-kw data args]
(let [args (conj args data)
_ (println "calc indicator " ind-kw " args: " (count args) (rest args))
ind (ta4j/indicator ind-kw args)]
(ta4j/ind-values-bool ind)))

(defn bar-bool [ds ind-kw & args]
(let [bar (ta4j/ds->ta4j-ohlcv ds)]
(calc-indicator-bool ind-kw bar args)))


; facade

(defn- get-values-for [o name]
Expand Down

0 comments on commit ccdbf3e

Please sign in to comment.