range over the last 1000 bars?

 

so if you want to use the average range of the last 1000 bars of the current chart:

sum(high-low)/n, how would you code this to use as a variable in your code?

This would need to be adapted somehow..

for(int i=0; i<limit; i++)

{

CurrentHigh = iHigh(NULL,0,i);

CurrentLow = iLow(NULL,0,i+RPeriod);

range=CurrentHigh-CurrentLow;

Range[i] = range;

}



but how ..

little help pls

 
Have you looked at the ATR indicator? It does something similar (or even better).
 
blogzr3:
Have you looked at the ATR indicator? It does something similar (or even better).

yes but I don't want that, I want simply the range: I guess I would have to use iMAOnArray,

but how set it the right way? This doesn't give any errors in the MetaEditor, but in MT4 it gives:

"2009.07.25 17:41:44 range USDJPY,H1: first parameter for iMAOnArray indicator must be an array"



CurrentHigh = iHigh(NULL,0,i);

CurrentLow = iLow(NULL,0,i+RPeriod);

range=CurrentHigh-CurrentLow;

range = ArraySetAsSeries(range,true);

double avgrange=iMAOnArray(range,0,1000,0,MODE_SMA,0);

Range[i] = avgrange;

//Range[i] = range;


so how do I fix this?

 
MrH:

so if you want to use the average range of the last 1000 bars of the current chart:

sum(high-low)/n, how would you code this to use as a variable in your code?

This would need to be adapted somehow..

for(int i=0; i<limit; i++)

{

CurrentHigh = iHigh(NULL,0,i);

CurrentLow = iLow(NULL,0,i+RPeriod);

range=CurrentHigh-CurrentLow;

Range[i] = range;

}



but how ..

little help pls

Hello MrH

Unsure as to your full needs but have you looked at iHighest() and iLowest()

Gives eg, for the last 1000bars for ccy pair and timeFrame of choice the Highest or Lowest. The function uses the Terminals series arrays - the id of which one you want inspected is given in call

No loops, just two lines of code from what I gather and you'll have the two values.

I think my uncertainty lies with 'range' since various meanings could be meant...

hth

 
fbj:

Hello MrH

Unsure as to your full needs but have you looked at iHighest() and iLowest()

Gives eg, for the last 1000bars for ccy pair and timeFrame of choice the Highest or Lowest. The function uses the Terminals series arrays - the id of which one you want inspected is given in call

No loops, just two lines of code from what I gather and you'll have the two values.

I think my uncertainty lies with 'range' since various meanings could be meant...

hth


I don't need iHighest because that takes the maximum value over ... bars.

What I need is the highest (iHigh) in every bar, substract the low of every bar: this gives you the range: High-Low

This value (range) I want to average over 1000 bars and then use this in my code.

 
MrH:

I don't need iHighest because that takes the maximum value over ... bars.

What I need is the highest (iHigh) in every bar, substract the low of every bar: this gives you the range: High-Low

This value (range) I want to average over 1000 bars and then use this in my code.


As blogzr3 already said, take a look at ATR. It takes into account gaps in price also. Here is how ATR is calculated:

true range = max(high,close prev ) − min(low,close prev )

 
MrH:

yes but I don't want that, I want simply the range: I guess I would have to use iMAOnArray,

Can't you just do something like the following? It's not using a built-in function such as iATR() or iMAOnArray() but it's still trivially fast. (If you're calling it more than once per bar, you can speed it up even further by caching the total range for bar 1 to bar N rather than continually recalculating it.)


int MaxBars = 1000; // Change as desired...

int UseBars = MathMin(Bars, MaxBars); // Cap at actual number of bars available

double TotalRange = 0;

for (int i = 0; i < UseBars; i++)

{

TotalRange += (High[i] - Low[i]);

}

double AverageRange = TotalRange / UseBars;


...But, as everyone else is saying, the ATR calculation does have notable advantages compared to what you're trying to do, such as taking gaps into account.

 
jjc:

Can't you just do something like the following? It's not using a built-in function such as iATR() or iMAOnArray() but it's still trivially fast. (If you're calling it more than once per bar, you can speed it up even further by caching the total range for bar 1 to bar N rather than continually recalculating it.)


int MaxBars = 1000; // Change as desired...

int UseBars = MathMin(Bars, MaxBars); // Cap at actual number of bars available

double TotalRange = 0;

for (int i = 0; i < UseBars; i++)

{

TotalRange += (High[i] - Low[i]);

}

double AverageRange = TotalRange / UseBars;


...But, as everyone else is saying, the ATR calculation does have notable advantages compared to what you're trying to do, such as taking gaps into account.


I guess everyone else is right...

ATR is better than what I'm trying to do,

but thanks for the feedback anyway!

cheers