EA in MQL4, right after new day, accessing candle values with shift=1 do not always return the right (last) candle

 

Hello there super coder,

I am hitting my head to a wall for not understanding what's going on wrong in a code I am debugging:

for my last EA I am working on a daily chart, and I need to check values of several assets when the new candle is formed (on my broker it's at 23.00 CEST), accessing candle values with shift=1.

I have a code that sound something like this:

void OnTick()
  {
//---
   if(IsNewCandle())
     {
        Sleep(1200);
        RefreshRates();
        while(FileIsEnding(Handle)==false) // here I do check from a file the assett that are being traded
        {
        if(iClose(Instr,0,1)>iOpen(Instr,0,1)) { // Instr take the asset name, didn't put the whole code of the file reading to keep it simple to read
        // do something
                }
        }    
     }
  }

... Sounds pretty simple right?

The issue is that the simple code iClose and iOpen sometimes do take properly the value of the last candle, the one I need to check, while in some other cases that same code takes the open and close the the candle from the day before, meaning the previous one I do need to check (like if I had put shift=2 in my iClose and iOpen instructions).

In the beginning I thought that for some asset if there is not a new tick value in this day that just started, then the chart might not return the right candle (and its values), so I tried to add that delay so that I am sure that today's candle (of the day just started) is formed, and referring to the previous candle would refer to the right one of yesterday... but not!

Also thinking to it, even if there is not yet a new tick value the candle is formed anyway, so referring to the previous candle (shift=1) should return anyway the right candle of the chart..

Any idea why this happen? and how to be able to refer always to the proper candle on the chart? On other timeframe where I work on usually, e.g. H1, H4 and M15 I never had this problem, When I check values of a canlde it always reach the proper candle.

Thanks for heping out

Documentation on MQL5: Standard Library / Indicators / Timeseries classes / CiClose
Documentation on MQL5: Standard Library / Indicators / Timeseries classes / CiClose
  • www.mql5.com
CiClose - Timeseries classes - Indicators - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
        if(iClose(Instr,0,1)>iOpen(Instr,0,1)) { // Instr take the asset name, didn't put the whole code of the file reading to keep it simple to read

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 (2019.05.20)

Don't assume that Time[i] == iTime(otherPair, TF, i) always use iBarShift.

 

Hi William,

thanks for your help... wow it really gave me some hints on other check to be prformed as well, a lot to digest and a deep thought on how things are not always as you expect ahah

But anyway, I implemented a check like the one below, where basically I take the time of the candle on my chart and on every other chart whose data I load, I go looking for the candle of that specified time:

        // Here I do get the time of the beginning of the candle that I will have to check for al the pairs
        candlewhen=iTime(NULL,0,1);
        int erroreiTime=GetLastError(); // checking for errors
        if(erroreiTime==0) //if no errors then
           {

while(FileIsEnding(Handle)==false)  // While the file pointer that load the pair..
     {
      // ..is not at the end of the file

      Instr     =FileReadString(Handle);// Pair to be checked
      double Forcevalue=MarketInfo(Instr,MODE_BID);
      
      // Here I check which candle on the loaded pair is on the right time for me
      candelindex=iBarShift(Instr,0,candlewhen,true);

        // and now that I have my REAL index I do make my calculations like
        if(iClose(Instr,0,candelindex)>iOpen(Instr,0,candelindex))
                {
                // my code
                }
        }
}
What happen now is that every "open" value of the specified candle is the right one (and I check which shift of the candle has to be done) BUT sometimes the other values (e.g. high, low, close) are not the right one... might it be because the data are still being downloaded and are not updated?

Thanks again

L