Quote:
|
Originally Posted by anthonyrae
Hi,
I'm just wondering how you would calculate the moving average high for each candle.
I'm wondering if this is correct :
for(i=0; i <= Bars; i++)
{
hi[i] = High[iHighest(NULL, 0, MODE_HIGH, p, 0)];
}
where p is the number of periods
|
On straight reading, your code is the same as the following:
PHP Code:
double v = High[iHighest(NULL, 0, MODE_HIGH, p, 0)];
for(i=0; i <= Bars; i++) {
hi[i] = v;
}
which I wouldn't call a Moving Average of any description. Rather, it picks the highest high within the most recent period of size p, and then plots that value for all bars.
Rather, a Simple Moving Average over the highs of bars would be coded like the following:
PHP Code:
double v = 0;
for ( i = Bars-1; i >= Bars-1-p; i-- ) {
v += High[i];
}
hi[ Bars-1-p ] = v / p;
for( i = Bars-1-p-1; i >= 0; i--) {
hi[i] = hi[i+1] + ( High[i] - High[i+p] ) / p;
}
And a Smoothed Simple Moving Average, which is a simpler method, over the highs of bars would be coded like the following:
PHP Code:
hi[ Bars-1 ] = High[ Bars-1 ];
for(i=Bars - 2; i >= 0; i--) {
hi[i] = ( hi[i+1] * ( p - 1 ) + High[ i ] ) / p;
}