How to calculate average high

 

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 = High;

}

where p is the number of periods

 
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 = High;

}

where p is the number of periods

On straight reading, your code is the same as the following:

double v = High;

for(i=0; i <= Bars; i++) {

hi = v;

}

[/PHP]

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:

double v = 0;

for ( i = Bars-1; i >= Bars-1-p; i-- ) {

v += High;

}

hi[ Bars-1-p ] = v / p;

for( i = Bars-1-p-1; i >= 0; i--) {

hi = hi + ( High - High ) / p;

}

And a Smoothed Simple Moving Average, which is a simpler method, over the highs of bars would be coded like the following:

[PHP]

hi[ Bars-1 ] = High[ Bars-1 ];

for(i=Bars - 2; i >= 0; i--) {

hi = ( hi * ( p - 1 ) + High[ i ] ) / p;

}

 

hi ralph,

thank you very much for explaining the code to me !

What i actually need is to plot the highest value in size p, for each bar movement. So for the current bar(i) it finds the high of the last 15(p) bars, and then plots it on the chart.

Does this make sense.. my first post wasn't very clear...

thanks, anthony

Reason: