Hi C��drick,
for smallCharts, I had to implement the same, but for stock market time series, which are a bit bigger than your example (like creating a 38 day moving average over 5 years).
Always copying the values to average is far too slow in this case.
My solution is to add up the first n (for an n-movAvg) numbers and remember the sum. The new average point is of course the sum divided by n.
For the next point you subtract the first number from the sum and add the next (1 + n th) number to the sum.
Happy hacking,
������������������������������ Christian
Von: Pharo-users <pharo-users-bounces@lists.pharo.org> Im Auftrag von C��drick B��ler
Gesendet: Mittwoch, 8. April 2020 10:07
An: Any question about pharo is welcome <pharo-users@lists.pharo.org>
Betreff: [Pharo-users] Moving/rolling average implementations ?
Hi,
I wanted to do a moving/rolling average on raw data [1].
I haven���t find code for that (maybe this is done in polymath though).
So I ended writing that (I thing this is SMA):
SequenceableCollection>>movingAverage: anOrder
"Answer the moving or rolling average for anOrder window"
| retval size x y |
anOrder <= 0 ifTrue: [ Error signal: 'the order must be positive'].
���������������� size := self size - anOrder.
������������������ size negative ifTrue: [ Error signal: 'the collection size is too small'].
���������������� retval := self species ofSize: size + 1.
���������������� x := 1.
������������������������ y := anOrder.
������������������ [y <= self size ] whileTrue: [
�������������������������������������������������������� retval at: x put: (self copyFrom: x to: y) average
�������������������������������������������������������� x := x + 1. y := y + 1
������������������������ ].
���������������� ^retval
Not perfect but seems to works quite well (that���s probably better to remove copyFrom: and use some kind of buffer instead).
Any interest in that ? If any existing code too, I���ll be interested especially for other implementation (weighted, exponential) ?

(#(118 113 105 105 103 99 98 101 100 107) movingAverage: 3) collect: [:v | v asScaledDecimal: 1 ] .
"an Array(112.0s1 107.7s1 104.3s1 102.3s1 100.0s1 99.3s1 99.7s1 102.7s1)"
Cheers,
C��drick
[1] https://www.meilleursbrokers.com/techniques-de-trading/moyennes-mobiles.html (in French but is understandable)