Skip to content

Commit

Permalink
Merge branch 'master' of github.com:clojure-quant/trateg
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Jun 21, 2024
2 parents d25f707 + 9b32550 commit 54dfb12
Show file tree
Hide file tree
Showing 10 changed files with 3,352 additions and 43 deletions.
1 change: 1 addition & 0 deletions lib/import/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
}
ta/helper {:local/root "../helper" :deps/manifest :deps}
ta/db {:local/root "../db" :deps/manifest :deps}
ta/calendar {:local/root "../calendar" :deps/manifest :deps}
}}

6 changes: 6 additions & 0 deletions lib/import/src/ta/import/provider/bybit/ds.clj
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,11 @@
{:start (-> "2024-02-29T00:00:00Z" t/instant)
:end (-> "2024-02-29T00:07:00Z" t/instant)})

(-> (get-bars
{:asset "BTCUSDT"
:calendar [:crypto :d]}
{:start (-> "2021-07-04T00:00:00Z" t/instant)
:end (-> "2024-05-02T00:00:00Z" t/instant)})
(tc/write-csv! "/clojure-quant/quanta/lib/indicator/test/ta/indicator/csv/BYBIT_SPOT_BTCUSDT_1D.csv"))
;
)
127 changes: 116 additions & 11 deletions lib/indicator/src/ta/indicator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
[tech.v3.dataset.rolling :as r]
[ta.indicator.rolling :as roll]
[tablecloth.api :as tc]
[ta.indicator.helper :refer [indicator]]
[ta.indicator.helper :refer [indicator nil-or-nan?]]
[ta.indicator.signal :refer [upward-change downward-change]]
[ta.indicator.returns :refer [diff-2col]]
[ta.indicator.returns :refer [diff-2col diff-n diff]]
[ta.helper.ds :refer [has-col]]
[ta.math.series :refer [gauss-summation]]
[fastmath.core :as fmath])
Expand Down Expand Up @@ -58,14 +58,13 @@
(defn wma
"Weighted moving average"
[n col]
(let [ds (tc/dataset {:col col})
norm (gauss-summation n)]
(:wma (r/rolling ds {:window-type :fixed
:window-size n
:relative-window-position :left}
{:wma {:column-name [:col]
:reducer (fn [window]
(wma-f window n norm))}}))))
(let [norm (gauss-summation n)]
(roll/rolling-window-reduce (fn [col-name]
{:column-name col-name
:reducer (fn [w]
(wma-f w n norm))
:datatype :float64})
n col)))

(defn- calc-ema-idx
"EMA-next = (cur-close-price - prev-ema) * alpha + prev-ema"
Expand Down Expand Up @@ -123,6 +122,112 @@
:datatype :float64})
n v))

(defn- adjust-src-val
[x d prev]
(if (> x (+ prev d))
(+ x d)
(if (< x (- prev d))
(- x d)
prev)))

(defn- ama-fn
"a2ma helper"
[x er prev-a]
(let [prev-a-nonzero (if (nil-or-nan? prev-a)
x
prev-a)]
(+ (* er x)
(* (- 1 er) prev-a-nonzero))))

(defn arma
"Autonomous Recursive Moving Average (ARMA)
https://www.tradingview.com/script/AnmTY0Q3-Autonomous-Recursive-Moving-Average/"
([n v]
(arma n v 3))
([n v g] ; TODO: zerolag flag
(let [tfn (indicator
[src-d (volatile! [])
src-ma (volatile! [])
prev-mad (volatile! [])
dsum (volatile! 0.0)]
(fn [i]
;; drop last
(when (>= (count @src-d) n)
(vswap! src-d #(vec (rest %))))
(when (>= (count @src-ma) n)
(vswap! src-ma #(vec (rest %))))
(when (>= (count @prev-mad) n)
(vswap! prev-mad #(vec (rest %))))
;;
(let [x (nth v i)
prev (if (last @prev-mad)
(last @prev-mad)
x)
xprevn (if (>= i n)
(nth v (- i n)))
diff (if xprevn
(Math/abs (- xprevn prev))
0.0)
next-sum (vswap! dsum + diff)
d (if (> i 0)
(/ (* next-sum g) i)
0.0)
next-src-val (adjust-src-val x d prev)
_ (vswap! src-d conj next-src-val)
sma0 (if (>= i (dec n))
(sma {:n n} @src-d))
_ (if sma0
(vswap! src-ma conj (last sma0)))
sma1 (if (>= i (- (* 2 n) 2))
(sma {:n n} @src-ma))
next-ma (if (>= i (- (* 2 n) 2))
(last sma1))]
(vswap! prev-mad conj next-ma)
next-ma
(if next-ma
next-ma
Double/NaN))))]
(into [] tfn (range 0 (count v))))))

(defn a2rma
"Adaptive Autonomous Recursive Moving Average (A2RMA)
https://www.tradingview.com/script/4bI1zjc6-Adaptive-Autonomous-Recursive-Moving-Average/"
([n v]
(a2rma n v 3))
([n v g]
(let [diff-n (dfn/abs (diff-n n v))
diff-1 (dfn/abs (diff v))
trailing-sum (roll/rolling-window-reduce-zero-edge r/sum n diff-1)
er (dfn// diff-n
trailing-sum)
tfn (indicator
[prev-ma0 (volatile! nil)
prev-ma (volatile! nil)
dsum (volatile! 0.0)]
(fn [i]
(let [x (nth v i)
prev-ma-nz (if (nil-or-nan? @prev-ma)
x
@prev-ma)
diff (Math/abs (- x prev-ma-nz))
next-sum (+ @dsum diff)
d (if (> i 0)
(/ (* next-sum g) i)
0.0)
y (adjust-src-val x d prev-ma-nz)
cur-er (nth er i)
a0 (ama-fn y cur-er @prev-ma0)
a (ama-fn a0 cur-er @prev-ma)
next-ma0 (if (>= i n) a0)
next-ma (if (>= i n) a)]
(vreset! prev-ma0 next-ma0)
(vreset! prev-ma next-ma)
(vreset! dsum next-sum)
(if (nil-or-nan? next-ma)
Double/NaN
next-ma))))]
(into [] tfn (range 0 (count v))))))

(defn- ehlers-tfn
[{:keys [c1 c2 c3]}]
(let [f (fn [x y1 y2]
Expand Down Expand Up @@ -355,7 +460,7 @@

(prior (:close ds))

(into [] sma2 [4 5 6 7 8 6 5 4 3])
(into [] (sma2 3) [4 5 6 7 8 6 5 4 3])

(tr ds)

Expand Down
3 changes: 3 additions & 0 deletions lib/indicator/src/ta/indicator/helper.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
r# (xf# result# r#))
result#)))))))

(defn nil-or-nan? [n]
(or (nil? n) (NaN? n)))

(comment

(defn field-xf [f]
Expand Down
6 changes: 2 additions & 4 deletions lib/indicator/src/ta/indicator/returns.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(ns ta.indicator.returns
(:require
[tech.v3.datatype :as dtype]
[tech.v3.datatype.functional :as dfn]))

(defn- nil-or-nan? [n]
(or (nil? n) (NaN? n)))
[tech.v3.datatype.functional :as dfn]
[ta.indicator.helper :refer [nil-or-nan?]]))

(defn diff
"returns a vector of the difference between subsequent values.
Expand Down
Loading

0 comments on commit 54dfb12

Please sign in to comment.