Averages/Simple moving average: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: updated code)
Line 1,054:
 
=={{header|Elena}}==
ELENA 3.x1 :
<lang elena>#import system'routines.
#import system'collections.
#import extensions.
 
class SMA
Line 1,072:
append : aNumber
[
theList += append:aNumber.
 
var aCount := theList length.
^ aCount =>
0 [ ^0.0r ];
! [
if (aCount > thePeriod)
[
theList remove &index:0.
aCount := thePeriod.
var n := aSum / aCount. := thePeriod
].
var aSum := theList summarize:(Real new &int:0).
var n := aSum / aCount.
^ aSum / aCount.
].
]
}
Line 1,097:
var SMA5 := SMA new:5.
 
1 to:5 &doEach: (:i)
[
console writeprint:"sma3 + " :i :" = ": (SMA3 += append:i) &paddingRight:30 &with:#$32.
console writeLineprintLine:"sma5 + " :i :" = ": (SMA5 += append:i).
].
 
5 to:1 &doEach: (:i)
[
console writeprint:"sma3 + " :i :" = ": (SMA3 += append:i) &paddingRight:30 &with:#$32.
console writeLineprintLine:"sma5 + " :i :" = ": (SMA5 += append:i).
].