Refactor SMA implementation to lib/sma.myc
Move the SMA implementation from examples/err.myc and examples/sma.myc to a new dedicated file in lib/. This change also cleans up unused code and improves the SMA logic.
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
(do
|
||||
(def SMA
|
||||
(fn [length]
|
||||
(do
|
||||
(def history (series :float))
|
||||
(def sum 0.0)
|
||||
|
||||
(fn [val]
|
||||
(do
|
||||
(def hist_len (len history))
|
||||
(if (>= hist_len length)
|
||||
(assign sum (- sum (history (- hist_len 1))))
|
||||
...)
|
||||
|
||||
(push history val)
|
||||
(assign sum (+ sum val))
|
||||
(/ sum (len history))
|
||||
)))))
|
||||
|
||||
(def sma10 (SMA 10))
|
||||
|
||||
(sma10 1)
|
||||
(sma10 2)
|
||||
(sma10 3)
|
||||
(sma10 4)
|
||||
(sma10 5)
|
||||
(sma10 6)
|
||||
(sma10 7)
|
||||
(sma10 8)
|
||||
(sma10 9)
|
||||
(sma10 10)
|
||||
(sma10 11)
|
||||
|
||||
)
|
||||
+6
-18
@@ -1,24 +1,12 @@
|
||||
;; Output: 5.5
|
||||
|
||||
(do
|
||||
(def SMA
|
||||
(fn [length]
|
||||
(do
|
||||
(def history (series :float))
|
||||
(push history 0)
|
||||
|
||||
(fn [val]
|
||||
(do
|
||||
(def sum (+ val (history 0)))
|
||||
(push history sum)
|
||||
(/ sum length)
|
||||
))
|
||||
)))
|
||||
|
||||
(def sma20 (SMA 20))
|
||||
|
||||
(def n 100)
|
||||
(while (> n 0)
|
||||
(do
|
||||
(def n 1000)
|
||||
(while (> n 0)
|
||||
(do
|
||||
(print "val=" n " sma20=" (sma20 n))
|
||||
(assign n (- n 1))
|
||||
))
|
||||
)
|
||||
)
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
(do
|
||||
(def SMA
|
||||
(fn [length]
|
||||
(do
|
||||
(def history (series :float))
|
||||
(def sum 0.0)
|
||||
|
||||
(fn [val]
|
||||
(do
|
||||
(def hist_len (len history))
|
||||
(if (>= hist_len length)
|
||||
(assign sum (- sum (history (- length 1)))))
|
||||
|
||||
(push history val)
|
||||
(assign sum (+ sum val))
|
||||
|
||||
(if (>= hist_len length)
|
||||
(/ sum length))
|
||||
)))))
|
||||
)
|
||||
Reference in New Issue
Block a user