Averages/Simple moving average: Difference between revisions

Content deleted Content added
Trizen (talk | contribs)
m →‎{{header|Sidef}}: updated code
Line 1,054: Line 1,054:


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


class SMA
class SMA
Line 1,072: Line 1,072:
append : aNumber
append : aNumber
[
[
theList += aNumber.
theList append:aNumber.


var aCount := theList length.
var aCount := theList length.
^ aCount =>
aCount =>
0 [ 0.0r ];
0 [ ^0.0r ];
! [
! [
if (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,097: Line 1,097:
var SMA5 := SMA new:5.
var SMA5 := SMA new:5.


1 to:5 &doEach: (:i)
1 to:5 doEach: (:i)
[
[
console write:"sma3 + " :i :" = ": (SMA3 += i) &paddingRight:30 &with:#32.
console print:"sma3 + " :i :" = ": (SMA3 append:i) paddingRight:30 with:$32.
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
console printLine:"sma5 + " :i :" = ": (SMA5 append:i)
].
].


5 to:1 &doEach: (:i)
5 to:1 doEach: (:i)
[
[
console write:"sma3 + " :i :" = ": (SMA3 += i) &paddingRight:30 &with:#32.
console print:"sma3 + " :i :" = ": (SMA3 append:i) paddingRight:30 with:$32.
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
console printLine:"sma5 + " :i :" = ": (SMA5 append:i)
].
].