Previous Bars Calculation

 

Hello, I was wondering if someone could help me out with a simple calculation that I cannot seem to figure out.


I've been using an EA that works really good depending on what the range of the currency pair is. As the range changes, the input parameter needs to change. Currently, I would calculate the range taking the LOW price of X previous candlesticks and subtracting it from the HIGH price of previous candlesticks. Whatever that price range in pips is, I would divide it by 50. I was wondering if someone could help me build this into a code.


For example:


In the parameters, the input that I want to change is called "TradeRange"


Currently, it looks like this "extern double TradeRange = 70.0;"


However, I want it to look something like this "extern double TradeRange = RangeCalculated;"

Where RangeCalculated is the LOW price subtracted from the HIGH price of X amount of previous candlesticks; which will all be divided by Y.


If someone could put me on the right track, that would be great!

 

iATR() Does not doing it?

 
qjol:

iATR() Does not doing it?


Thanks a lot for the quick response. I'm not too sure exactly what that function does. I tried looking it up, but I do not fully understand what goes inside the parentheses and how to add the divided portion part of it. I'll keep on trying to figure it out and also see where in the code I should put it.


Thanks for the suggestion.

 

read more about

average true range (ATR)

 

Can you please tell me if I'm on the right track. The EA doesn't seem to compile it correctly.


I want to, for example, take the difference between the high/low of the last 100 periods (candlesticks/bars) and divide it by 50.


The info for iATR is "iATR( string symbol, int timeframe, int period, int shift)"


Does this mean I express it like this:

Range calculated = (iATR(Symbol(), int timeframe, int period, 100)/50)


I used Symbol() for the "string symbol" so it can work for which ever currency pair I'm using.

I didn't know if I should put anything for "int timeframe" and "int period" because I wasn't sure what goes there. I assumed if it wanted the default values that I should just leave it alone.

I put 100 for "int shift" so that it can calculate the last 100 periods.

Finally, I put /50 at the end so that it can divide the entire amount by 50.


Am I doing this correctly?

 
qjol:

read more about

average true range (ATR)


ohh ok, I just saw your post, I will look it up now.
 

After using iATR, I don't think it produces the correct results.


True Range is the greatest of the following three values:

  • difference between the current maximum and minimum (high and low);

  • difference between the previous closing price and the current maximum;

  • difference between the previous closing price and the current minimum.


So, the Average True Range would be just the average of multiple true ranges. For example, lets say that I want to take the range of the last three months (60 banking days or 60 candlesticks). iATR would just take the true ranges of all 60 days and then find the average value of them. This means that I would get an average daily trading range over a 60 day period.


However, I want the program to take the LOWEST price of the past 60 days and subtract it from the HIGHEST price of the last 60 days. Whatever the difference in pips is, I would like to divide it by 100 to get my desired range set.


Is there another way that I can calculate this or a different function that I can use? Or is iATR the correct one and I completely misunderstood its meaning?

 
 

Hey qjol,


I took your suggestion and tried it. It seems to work partially, but when it gets to certain times, it just stops working and the strategy tester just freezes. Can you just take a quick look at the coding to see if I inserted it correctly.


Lets say that I want the desired range parameter to equal 100 for every 1000 pips difference between the highest/lowest prices over a 60 day period. This means if the prices are 1.10000 (high) and 1.00000 (low), the difference will be .1. So, I would need to multiply .1 by 1000 to get my desired value of 100. Did I enter it correctly below?


Thanks!


int highval;
highval = iHighest(NULL,PERIOD_D1,MODE_HIGH,60,0);
 
int lowval;
lowval = iLowest(NULL,PERIOD_D1,MODE_LOW,60,0);

int DesiredRange;
DesiredRange = MathCeil((highval - lowval) * 1000);
 

Have not checked in depth, First a Integer can not contain a decimal number You should use doubles for this

double highval = iHighest(NULL,PERIOD_D1,MODE_HIGH,60,0);

double lowval = iLowest(NULL,PERIOD_D1,MODE_LOW,60,0);

double DesiredRange = MathCeil((highval - lowval) * 1000);

seconod It should be like this

double highval = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,60,0)];

double lowval = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,60,0)];
 

Q: if it's equal 0.14411 then what

Reason: