24 lines
482 B
Plaintext
24 lines
482 B
Plaintext
T SMA
|
||
[Float] data
|
||
sum = 0.0
|
||
index = 0
|
||
n_filled = 0
|
||
Int period
|
||
|
||
F (period)
|
||
.period = period
|
||
.data = [0.0] * period
|
||
|
||
F add(v)
|
||
.sum += v - .data[.index]
|
||
.data[.index] = v
|
||
.index = (.index + 1) % .period
|
||
.n_filled = min(.period, .n_filled + 1)
|
||
R .sum / .n_filled
|
||
|
||
V sma3 = SMA(3)
|
||
V sma5 = SMA(5)
|
||
|
||
L(e) [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
|
||
print(‘Added #., sma(3) = #.6, sma(5) = #.6’.format(e, sma3.add(e), sma5.add(e)))
|