Skip to content

Commit

Permalink
indicator: MAD
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Mar 27, 2024
1 parent 7f13122 commit 1a330b7
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 63 deletions.
41 changes: 40 additions & 1 deletion lib/indicator/src/ta/indicator/rolling.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[tech.v3.dataset.rolling :as r]
[tech.v3.datatype.functional :as dfn]
[tablecloth.api :as tc]
[ta.indicator.returns :as ret]))
[ta.indicator.returns :as ret]
[ta.math.stats :as stats]
))

(defn rolling-window-reduce [rf n vec]
(let [ds (tc/dataset {:in vec})]
Expand All @@ -25,6 +27,36 @@
[n v]
(rolling-window-reduce r/min n v))


(defn trailing-variance
"returns the trailing-variance over n bars of column v.
the current row is included in the window."
[n v]
(rolling-window-reduce r/variance n v))


(defn trailing-variance-population
"returns the trailing mean-deviaton over n bars of column v.
the current row is included in the window."
[n v]
(rolling-window-reduce (fn [col-name]
{:column-name col-name
:reducer stats/variance-population
:datatype :float64})
n v))



(defn trailing-mad
"returns the trailing mean-deviaton over n bars of column v.
the current row is included in the window."
[n v]
(rolling-window-reduce (fn [col-name]
{:column-name col-name
:reducer stats/mad
:datatype :float64})
n v))

(defn trailing-return-stddev
"returns the trailing-stddev over n bars of column v.
the current row is included in the window."
Expand Down Expand Up @@ -87,6 +119,13 @@

(trailing-return-stddev 3 ds)

(trailing-mad 2 (:close ds))
; tmd-1: 1.1 + 2.2 = 3.3 / 2 = 1.65 - 2.2 = -0.55

(rolling-window-reduce
r/min
2
(:close ds))
;
)

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

(defn ind-helper [sub-ns class-key & args]
(let [namespace (str "org.ta4j.core.indicators." sub-ns ".")
_ (println "namespace: " namespace)
ctor (constructor namespace "Indicator")]
(ctor class-key args)))

Expand Down
21 changes: 9 additions & 12 deletions lib/indicator/test/ta/indicator/atr_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@

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


(comment
(comment
(ind/tr ds)

(->> (ind/atr {:n 4} ds)
(into []))
(into []))

(ind/sma {:n 4} [30.0 30.0 40.0 40.0])

(->> (ind/atr-mma {:n 4} ds)
(into [])
)

(ta4j/bar ds :ATR 4)

(into []))

(ta4j/bar ds :ATR 4)

;
)

Expand Down
5 changes: 4 additions & 1 deletion lib/indicator/test/ta/indicator/bollinger_test_bad.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
[ta.indicator.util.fuzzy :refer [all-fuzzy=]]
[ta.indicator.util.ta4j :as ta4j]
[ta.indicator.util.data :refer [ds]]
[ta.indicator.band :as band]))
[ta.indicator.band :as band])
(:import [org.ta4j.core BaseStrategy #_BaseTimeSeries$SeriesBuilder
;TimeSeriesManager
])s)


; ta4j calculates bollinger via facade;
Expand Down
57 changes: 57 additions & 0 deletions lib/indicator/test/ta/indicator/stats_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(ns ta.indicator.stats-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.rolling :as roll]
[tech.v3.datatype.functional :as dfn]))

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

(deftest test-mad-3
(is (all-fuzzy=
(-> (ta4j/close ds :statistics/MeanDeviation 3) rest rest)
(-> (roll/trailing-mad 3 (:close ds)) rest rest))))

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



#_(deftest test-stddev
(is (all-fuzzy= 0.1
(ta4j/close ds :statistics/StandardDeviation 4)
(->> (roll/trailing-stddev 4 ds) (into []))
;(-> (ind/atr-mma {:n 4} ds) (round))
)))
(comment
(ta4j/close ds :statistics/MeanDeviation 2)
(roll/trailing-mad 2 (:close ds))

(ta4j/close ds :statistics/Variance 2)
;; => (0.0 0.25 193.55555555555554 259.25 216.5 54.1875 85.6875 116.0 230.75 200.75 17.1875 17.1875 54.6875 92.1875 50.0)

(roll/trailing-variance 2 (:close ds))

(roll/trailing-variance-population 4 (:close ds))



(ta4j/close ds :statistics/StandardDeviation 4)




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


;
)


48 changes: 0 additions & 48 deletions lib/indicator/test/ta/indicator/stats_test_bad.clj

This file was deleted.

53 changes: 53 additions & 0 deletions lib/math/src/ta/math/stats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
(defn mean [coll]
(/ (reduce + coll) (count coll)))


(defn variance-sample [coll]
(let [avg (mean coll)
squares (map #(Math/pow (- % avg) 2) coll)]
(-> (reduce + squares)
(/ (dec (count coll))))))

(defn variance-population [coll]
(let [avg (mean coll)
squares (map #(Math/pow (- % avg) 2) coll)]
(-> (reduce + squares)
(/ (count coll))
Math/sqrt)))

;;for sample (not population)
(defn standard-deviation [coll]
(let [avg (mean coll)
Expand All @@ -14,6 +28,15 @@
(/ (dec (count coll)))
Math/sqrt)))

(defn standard-deviation-population [coll]
(let [avg (mean coll)
squares (map #(Math/pow (- % avg) 2) coll)]
(-> (reduce + squares)
(/ (count coll))
Math/sqrt)))



(defn standardize [xs]
(-> xs
(dfn/- (dfn/mean xs))
Expand All @@ -22,3 +45,33 @@
(defn rand-numbers [n]
(dtype/clone
(dtype/make-reader :float32 n (rand))))


(defn mad
"mad = mean absolute deviation.
the mean of the absolute deviation of the mean.
more applicable to reality than the standard deviation"
[vec]
(let [m (dfn/mean vec)
ad (-> (dfn/- vec m) (dfn/abs))]
(dfn/mean ad)))

(comment
(mad [1 2 3])
; mean = 2.
; diffs: 1 0 1
; mad (2/3)

(dfn/variance [1 2 3 4 5])
(variance-sample [1 2 3 4 5])
(variance-population [1 2 3 4 5])


(dfn/standard-deviation [1 2 3 4 5])
(standard-deviation [1 2 3 4 5])
(standard-deviation-population [1 2 3 4 5])


;
)

0 comments on commit 1a330b7

Please sign in to comment.