MqlRates incorrectly calculated?

To add comments, please log in or register
Christoph Stallmann
429
Christoph Stallmann  

I've come across a weird thing with regards to the MqlRates. I've used the data for each tick from the OnTick() event and calculated my own rates using the ticks.


So assume I want to calculate the "high" price at a given day at 9h00 using a period of H1. Hence, you would use all ticks between 8h00 and 9h00 and look for the maximum value which you will use to represent 9h00. However, when comparing those values against the MqlRate high price, I realized that my calculation's trend was always lagging a bit behind.


So I tried something different by using the data from 9h00 to 10h00 (which seems wrong, since you are using the future "unavailable" samples). And then suddenly the trend of my calculation lined up with the ones from MqlRate.


Did I miss something, or is MqlRate calculating things incorrectly?

Alain Verleyen
39530
Alain Verleyen  
Without any code you can't receive help.
Christoph Stallmann
429
Christoph Stallmann  

Well, the code is quite simple:


datetime start = D'06.01.2015 08:00:00';
datetime end = D'06.01.2015 09:00:00';

MqlRates rates[];
CopyRates(Symbol(), PERIOD_H1, end, 1, rates);

MqlTick ticks[];
int size = CopyTicks(Symbol(), ticks, COPY_TICKS_ALL, start, 100000);

double max = 0;
for(int i = 0; i < size; ++i)
{
        if(ticks[i].time > end) break;
        if(ticks[i].bid > max) max = ticks[i].bid;
}

Print("MqlRates: ", rates[0].high, " Custom Rates: ", max);


So, I want the high price at 9h00. My way of calculating the high will not be exactly the same as Mql, because of rounding errors and whether or not the 1st if-statement in the loop should be ">" or ">=". But that doesn't matter for now, since I'm only looking for a similar trend (peaks and troughs at the same place) if plotting the Mql rates against my custom rates.


So with the given example, I'm using the ticks between 8h00 and 9h00 to calculate the high at 9h00. However the print statement at the end will show quite a substantial deviation between my and Mql's high price. However, when I change the start and end dates for my custom high to 9h00 and 10h00 respectively, the deviation to Mql's high is way lower. Hence, when plotting over a larger timespan, Mql's high and my custom high almost perfectly align if I use the MqlRates at 9h00 and calculate my custom high between 9h00 and 10h00.


Does this mean that MqlRates at a given time represent the values in the hour to come (hence, 9h00 will indicate everything between 9h00 and 10h00)?

Alain Verleyen
39530
Alain Verleyen  
goocreations:

Well, the code is quite simple:

Where is the error checking ? CopyRates is a function that can have return an error, CopyTicks also.


So, I want the high price at 9h00. My way of calculating the high will not be exactly the same as Mql, because of rounding errors and whether or not the 1st if-statement in the loop should be ">" or ">=". But that doesn't matter for now, since I'm only looking for a similar trend (peaks and troughs at the same place) if plotting the Mql rates against my custom rates.


So with the given example, I'm using the ticks between 8h00 and 9h00 to calculate the high at 9h00. However the print statement at the end will show quite a substantial deviation between my and Mql's high price. However, when I change the start and end dates for my custom high to 9h00 and 10h00 respectively, the deviation to Mql's high is way lower. Hence, when plotting over a larger timespan, Mql's high and my custom high almost perfectly align if I use the MqlRates at 9h00 and calculate my custom high between 9h00 and 10h00.

CopyTicks gives you no more then 2,000 ticks ! You can have more than 2000 ticks in 1 hour (you can even have missing ticks, but that's an other matter). So you need to check you have all needed ticks.

More important, "The function receives ticks accumulated by the terminal for the current work session and writes them into the ticks_array.", you can use it to get "history" ticks.

Does this mean that MqlRates at a given time indicate the value that happens in the hour to come (hence, 9h00 will indicate everything between 9h00 and 10h00)?

CopyRates(Symbol(), PERIOD_H1, end, 1, rates);

rates[0].high give you the high of the H1 bar containing the 'end' datetime. So in your case it's the high of bar starting at 06 January 2015, 09:00. So yes between 9h and 10h.

Please check the documentation attentively : CopyRates

Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
Documentation on MQL5: Timeseries and Indicators Access / CopyTicks
  • www.mql5.com
Timeseries and Indicators Access / CopyTicks - Reference on algorithmic/automated trading language for MetaTrader 5
To add comments, please log in or register