Averages/Simple moving average: Difference between revisions

Content added Content deleted
Line 1,093: Line 1,093:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.4 :
ELENA 4.x :
<lang elena>import system'routines.
<lang elena>import system'routines;
import system'collections.
import system'collections;
import extensions.
import extensions;


class SMA
class SMA
{
{
object thePeriod.
object thePeriod;
object theList.
object theList;
constructor new : aPeriod
constructor new(period)
[
{
thePeriod := aPeriod.
thePeriod := period;
theList := List new.
theList :=new List();
]
}
append : aNumber
append(n)
[
{
theList append:aNumber.
theList.append(n);


var aCount := theList length.
var count := theList.Length;
aCount =>
count =>
0 [ ^0.0r ];
0 { ^0.0r }
! [
: {
if (aCount > thePeriod)
if (count > thePeriod)
[
{
theList removeAt:0.
theList.removeAt:0;
aCount := thePeriod
count := thePeriod
].
};
var aSum := theList summarize(Real new).
var sum := theList.summarize(new Real());
^ aSum / aCount
^ sum / count
]
}
]
}
}
}


public program
public program()
{
[
var SMA3 := SMA new:3.
var SMA3 := SMA.new:3;
var SMA5 := SMA new:5.
var SMA5 := SMA.new:5;


1 to:5 do(:i)
for (int i := 1, i <= 5, i += 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
[
console printPaddingRight(30, "sma3 + ", i, " = ", SMA3 append:i).
console.printLine("sma5 + ", i, " = ", SMA5.append:i)
};
console printLine("sma5 + ", i, " = ", SMA5 append:i)
].


5 to:1 do(:i)
for (int i := 5, i >= 1, i -= 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
[
console printPaddingRight(30, "sma3 + ", i, " = ", SMA3 append:i).
console.printLine("sma5 + ", i, " = ", SMA5.append:i)
};
console printLine("sma5 + ", i, " = ", SMA5 append:i)
].
console readChar.
console.readChar()
]</lang>
}</lang>
{{out}}
{{out}}
<pre>
<pre>