Averages/Simple moving average: Difference between revisions

Content added Content deleted
m (→‎{{header|zkl}}: always return float)
Line 1,054: Line 1,054:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.x :
<lang elena>#define system.
#define system'routines.
<lang elena>#import system'routines.
#define system'collections.
#import system'collections.
#define extensions.
#import extensions.


#class SMA
class SMA
{
{
#field thePeriod.
object thePeriod.
#field theList.
object theList.
#constructor new : aPeriod
constructor new : aPeriod
[
[
thePeriod := aPeriod.
thePeriod := aPeriod.
Line 1,070: Line 1,070:
]
]
#method append : aNumber
append : aNumber
[
[
theList += aNumber.
theList += aNumber.


#var aCount := theList length.
var aCount := theList length.
^ aCount =>
^ aCount =>
0 ? [ 0.0r ]
0 [ 0.0r ];
! [
! [
(aCount > thePeriod)?
if (aCount > thePeriod)
[
[
theList remove &index:0.
theList remove &index:0.
aCount := thePeriod.
aCount := thePeriod.
].
].
#var aSum := theList summarize:(Real new &int:0).
var aSum := theList summarize:(Real new &int:0).
var n := aSum / aCount.
^ aSum / aCount.
^ aSum / aCount.
Line 1,092: Line 1,092:
}
}


#symbol program =
program =
[
[
#var SMA3 := SMA new:3.
var SMA3 := SMA new:3.
#var SMA5 := SMA new:5.
var SMA5 := SMA new:5.


1 to:5 &doEach: (:i)
1 to:5 &doEach: (:i)
Line 1,108: Line 1,108:
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
].
].
console readChar.
].</lang>
].</lang>
{{out}}
{{out}}