Add HMA example script
This commit introduces a new example script, `HMA.myc`, which demonstrates the implementation and usage of the Hull Moving Average (HMA) indicator. The script includes factory functions for Simple Moving Average (`make-sma`), Weighted Moving Average (`make-wma`), and the Hull Moving Average itself (`make-hma`). It also includes a small example loop to initialize and print HMA values.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
(do
|
||||
;; make-sma Factory (O(1))
|
||||
(def make-sma
|
||||
(fn [lookback]
|
||||
(do
|
||||
(def s (series lookback :float))
|
||||
(def running-sum 0.0)
|
||||
(def count 0)
|
||||
(fn [x]
|
||||
(do
|
||||
(if (< count lookback)
|
||||
(do (assign running-sum (+ running-sum x)) (assign count (+ count 1)))
|
||||
(do (assign running-sum (+ (- running-sum (s (- lookback 1))) x))))
|
||||
(push s x)
|
||||
(/ running-sum count))))))
|
||||
|
||||
;; make-wma Factory (O(1))
|
||||
(def make-wma
|
||||
(fn [lookback]
|
||||
(do
|
||||
;; Sicherstellen, dass lookback mindestens 1 ist
|
||||
(def n (if (< lookback 1) 1 lookback))
|
||||
(def s (series n :float))
|
||||
(def price-sum 0.0)
|
||||
(def weighted-sum 0.0)
|
||||
(def count 0)
|
||||
(def weight-total (/ (* n (+ n 1)) 2))
|
||||
(fn [x]
|
||||
(do
|
||||
(if (< count n)
|
||||
(do
|
||||
(assign count (+ count 1))
|
||||
(assign price-sum (+ price-sum x))
|
||||
(assign weighted-sum (+ weighted-sum (* x count)))
|
||||
(push s x)
|
||||
(/ weighted-sum (/ (* count (+ count 1)) 2)))
|
||||
(do
|
||||
(def oldest (s (- n 1)))
|
||||
(assign weighted-sum (+ (- weighted-sum price-sum) (* x n)))
|
||||
(assign price-sum (+ (- price-sum oldest) x))
|
||||
(push s x)
|
||||
(/ weighted-sum weight-total))))))))
|
||||
|
||||
;; make-hma Factory (O(1))
|
||||
;; Hull Moving Average: WMA(sqrt(n), 2*WMA(n/2) - WMA(n))
|
||||
(def make-hma
|
||||
(fn [lookback]
|
||||
(do
|
||||
(def n lookback)
|
||||
(def wma1 (make-wma (trunc (/ n 2))))
|
||||
(def wma2 (make-wma n))
|
||||
(def wma-final (make-wma (trunc (sqrt n))))
|
||||
(fn [x]
|
||||
(do
|
||||
(def v1 (wma1 x))
|
||||
(def v2 (wma2 x))
|
||||
(wma-final (- (* 2.0 v1) v2)))))))
|
||||
|
||||
;; Beispiele & Test
|
||||
(def hma16 (make-hma 16))
|
||||
(print "HMA (16) Initialisierung gestartet...")
|
||||
(def i 1.0)
|
||||
(while (<= i 20.0)
|
||||
(do
|
||||
(print "HMA Step" i ":" (hma16 (* i 10.0)))
|
||||
(assign i (+ i 1))))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user