An interesting discussion guys but I still do not think this should be happening. I put the same code in an indicator and indeed the first value was always wrong then with subsequent 'ticks' the correct value was displayed.
The MT4 platform is notorious for its' poor data history compared to the brokers' customised trading platform. I can understand if you first opened a new MT4 platform you need to load history data to ensure you can make certain calculations and the traditional way of doing this is hold down the 'HOME' key on each timeframe until all data is loaded for M1 to MN1.
This should mean at this stage that the 'history' database for each timeframe is loaded. My concern is that the function MqlRates should store all data for all timeframes since we can ask it for any value at any time. This clearly is not the case....consistently anyway.
Also how sure can we be that if we are running an EA/Indicator on a particular timeframe but regularly asking for data values from other timeframes, that we are getting the correct values? What if you used the first value given (i.e on the first tick) for further calculations?
I know good coding says we need to check for successful completion like for OrderSend(), OrderSelect() etc; but how do we do this for simple in-built functions like iHigh()?
If we had to do this every time for these types of functions the programmes would be millions of lines long!
Opening another chart and scrolling to the beginning ensures longer history only (the script alone will not load more than 2048 last bars for other timeframes).
I do not consider a problem checking consistency of data for a programmer. But there are additional problems involved:
- brokers have inconsistent primary data often
- loading the chart leaves large gaps in the history occasionally
- accessing other timeframe allocates additional buffers with the entire chart history (a copy of the original history), for every single script (i.e. you can easily hit memory limits)
the problem here, with checking successful completion, is that the wrong value of e.g. iHigh, can be anything, and not only zero, so there is no easy way to check for
Luckily there are some workarounds ; all I can find ATM, is to force a single ChartSetSymbolPeriod() inside on OnInit(), as follows
#property copyright " "
#property link " "
#property version " "
iHigh(NULL,PERIOD_M30,0); iHigh(NULL,PERIOD_MN1,0); /* examples of TFs */
ChartSetSymbolPeriod(0,NULL,0); singleton=1; /* if someday program hangs, we may put singleton=1 first */
int singleton; /* a global variable - if #property strict is used, it has to be int singleton=0; */
/* so , TFs will be available now, on first call of e.g. OnTimer */
After all , a remark is MQL is supporting nameless functions (the headless block which contains iHigh calls above)
edit: the nameless block is eye candy only :)
edit 2 . Ok, once more, what i wrote is somehow useless :) there is no need for ChartSetSymbolPeriod(), as the effect is that the time passed between OnInit and OnTimer is enough for the asked prices (from iHigh) to be downloaded; so what someone needs is maybe have a Sleep() inside OnInit as to be more probable that the download will be finished by the time OnTimer or OnTick will be called
edit 3 . In Timeseries and Indicators Access/CopyRates we can read "If requested timeseries are not yet built or they need to be downloaded from the server, the function will immediately return -1". So maybe here is a clue as to how be certain if the history is present or not in our computers Or maybe RefreshRates
I still find the fact that we have to wait for the inadequate Mt4 platform to download history data unacceptable. I originally used ArrayCopyRates() in very my small programme and this did not show -1 (fail) but did still give the wrong data values!
Anyway, we are still discussing an even simpler programme which just uses iHigh(). For me whichever way you look at it if I use an in-built function like iHigh() I expect it to give correct results - loading databases is not rocket science - all other custom broker platforms (i.e FXCM's Trading Station) do not have this problem.
I know there are gaps in the history sometimes and you can physically see these on the chart - if I can physically see that the most recent bar (bar zero) is on the chart then I should be able to interrogate the statistics of that bar using iHigh(), iLow() etc. - for me this is very simple but very frightening that something so simple is so inconsistent.
There are many traps in MQL4 programmes but this should not be one of them!