Obtain ready and coherent data from other timeframes in MT5

 

Hello community,

I'm having some strange behaviors in MT5 when working with indicators that use other timeframes for calculations (ex. Pivot indicators).

Most of time they works fine on all symbols, but on some of them (in this particular case XAUUSD), data retrieved are not valid and so, all calculations are wrong.


Of course I tried to "return 0" in some scenarios in which current period and daily period series are surely not valid, for example. (This part is in the first part of OnCalculate of course)

   MqlRates rates[];
   int copied;
   copied = CopyRates(NULL,0,0,1,rates);
   if( copied<=0 ) return 0;
   copied = CopyRates(NULL,PERIOD_D1,0,1,rates);
   if( copied<=0 ) return 0;

This mitigate a lot of issues but not all.


Basically the cycle to calculate data is made in most of indicators in a similar way, cycling an integer variable from its value to 0.

int i = MathMin(rates_total-1,rates_total - prev_calculated + 1);

What I seen is that for some ticks, I need to refresh all datas for all "rates_total-1" rates, otherwise I get badly calculated values.


A workaround comes to my mind and can be descrived as "I refresh indicator for all rates_total until a specific number of ticks is received". I counted ticks with an int variable and when this variable is greater than X, i start refreshing indicators for "rates_total-prev_calculated" rates.

int i = MathMin(rates_total-2,rates_total - prev_calculated + 1);
if( ticks_count<5 ) i = rates_total-2;

In the end of my OnCalculate I increase ticks_count with ++ command.


This works fine and honestly I can use it without any problem but it sound too strange to me... Is it possible that there is no command to see if data are valid or not? I used CopyRates, checked iTime, checked SymbolIsSynchronized and so on... Not a single one solved my issue but only my rude and raw workaround.

I'm almost sure i'm missing something, but what?


Thanks to all that want to help my in finally found a sense of it. 

:-)