Add series, SMA, and WMA indicators

Introduce new indicators for Simple Moving Average (SMA) and Weighted
Moving Average (WMA), along with their Hull Moving Average (HMA)
derivative.

Also refactors series implementation to use `RefCell` for interior
mutability and adds `SeriesMember` trait for better dynamic series
access. Updates `soa_series.myc` example to reflect new series creation
and push syntax.
This commit is contained in:
Michael Schimmel
2026-03-05 14:07:42 +01:00
parent d1b8d03604
commit 831525b402
10 changed files with 385 additions and 137 deletions
+30
View File
@@ -0,0 +1,30 @@
(do
;; Simple Moving Average O(1)
(def SMA (fn [len]
(do
(def s (Series len))
(def sum 0.0)
(fn [val]
(do
(if (>= (count s) len)
(set sum (- sum (get_item s (- len 1))))
(nop))
(push s val)
(set sum (+ sum val))
(/ sum (count s)))))))
;; Weighted Moving Average
(def WMA (fn [len]
(do
(def s (Series len))
(def weight_sum (/ (* len (+ len 1)) 2))
(fn [val]
(do
(push s val)
(def current_sum 0.0)
(def i 0)
(while (< i (count s))
(do
(set current_sum (+ current_sum (* (get_item s i) (- len i))))
(set i (+ i 1))))
(/ current_sum weight_sum)))))))