Averages/Simple moving average: Difference between revisions

m
Line 1,093:
 
=={{header|Elena}}==
ELENA 3.4.x :
<lang elena>import system'routines.;
import system'collections.;
import extensions.;
 
class SMA
{
object thePeriod.;
object theList.;
constructor new : aPeriod(period)
[{
thePeriod := aPeriod.period;
theList :=new List new.();
]}
append : aNumber(n)
[{
theList append:aNumber.append(n);
 
var aCountcount := theList length.Length;
aCountcount =>
0 [{ ^0.0r ];}
!: [{
if (aCountcount > thePeriod)
[{
theList .removeAt:0.;
aCountcount := thePeriod
].};
var aSumsum := theList .summarize(Real new Real().);
^ aSumsum / aCountcount
]}
]}
}
 
public program()
{
[
var SMA3 := SMA .new:3.;
var SMA5 := SMA .new:5.;
 
1for (int i to:= 1, i <= 5, do(:i += 1) {
console printLine.printPaddingRight(30, "sma5sma3 + ", i, " = ", SMA5 SMA3.append:i);
[
console printPaddingRight.printLine(30, "sma3sma5 + ", i, " = ", SMA3 SMA5.append:i).
[};
console printLine("sma5 + ", i, " = ", SMA5 append:i)
].
 
5for (int i to:= 5, i >= 1, do(:i -= 1) {
console printLine.printPaddingRight(30, "sma5sma3 + ", i, " = ", SMA5 SMA3.append:i);
[
console printPaddingRight.printLine(30, "sma3sma5 + ", i, " = ", SMA3 SMA5.append:i).
].};
console printLine("sma5 + ", i, " = ", SMA5 append:i)
].
console readChar.readChar()
]}</lang>
{{out}}
<pre>
Anonymous user