Study Example 2
Previous  Top  Next

A/D Oscillator using the ExpMovingAverageOfData:

In this method, we compute the value of the basic indicator for the whole history. Then we apply ExpMovingAverageOfData to it.

sub ComputeADOscillator2 {
my(@ar, @rc, $i, $n, $range);
$n = getNumDays();
$#ar = $n-1;
for($i=0;$i<$n;$i++) {
$range = getHigh($i)-getLow($i);
if($range<1e-6) {
@ar[$i]=0.5;
} else {
@ar[$i] = (getHigh($i) - getOpen($i) + getClose($i) - getLow($i))/2/$range;
}
}
@rc = ExpMovingAverageOfData(0.3,@ar);
return @rc[0];
}

The command
$#ar = $n-1;
grows the array @ar so that the last element of the array has index $n-1. We loop through the days available, computing the base indicator and storing the values in the array @ar. Then we use the command
@rc = ExpMovingAverageOfData(0.3,@ar);
to compute the full indicator for the whole history. We then return the current value. This method builds the final indicator by building all-the-days-at-once rather than one-day-at-a-time. We could have returned the prior days value just as easily as the current value.

Study Example 3 is another alternative.