Averages/Simple moving average: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}) |
|||
Line 1,093: | Line 1,093: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA |
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 |
constructor new(period) |
||
{ |
|||
thePeriod := |
thePeriod := period; |
||
theList := List |
theList :=new List(); |
||
} |
|||
append |
append(n) |
||
{ |
|||
theList |
theList.append(n); |
||
var |
var count := theList.Length; |
||
count => |
|||
0 |
0 { ^0.0r } |
||
: { |
|||
if ( |
if (count > thePeriod) |
||
{ |
|||
theList |
theList.removeAt:0; |
||
count := thePeriod |
|||
}; |
|||
var |
var sum := theList.summarize(new Real()); |
||
^ |
^ sum / count |
||
} |
|||
} |
|||
} |
} |
||
public program |
public program() |
||
{ |
|||
[ |
|||
var SMA3 := SMA |
var SMA3 := SMA.new:3; |
||
var SMA5 := SMA |
var SMA5 := SMA.new:5; |
||
for (int i := 1, i <= 5, i += 1) { |
|||
⚫ | |||
⚫ | |||
console |
console.printLine("sma5 + ", i, " = ", SMA5.append:i) |
||
⚫ | |||
⚫ | |||
⚫ | |||
for (int i := 5, i >= 1, i -= 1) { |
|||
⚫ | |||
[ |
|||
console |
console.printLine("sma5 + ", i, " = ", SMA5.append:i) |
||
⚫ | |||
⚫ | |||
]. |
|||
console |
console.readChar() |
||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |