Problem with Shift parameter on first tick of the hour

 

Hello,

I'm having a problem with the shift parameter for the iMACD and iStochastic functions.

I wrote a piece of code that is run at the first tick of every hour: this condition is verified by reading the current Hour() and checking if it's > than the Hour() value saved in a static variable during the previous OnTick() loop.

If it's the first tick of the hour, the code reads the value of the MACD for the last closed period in this way:

myMacd = iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

It then gets the value of the Stochastic at the close of the previous period in this way:

myStoch = iStochastic(NULL,PERIOD_H1,14,3,3,MODE_SMA,0,MODE_MAIN,1);

I then print a line in the log with the 2 values above.

What I found is that the values printed are not the ones of the previous hour, but of the hour before (as if I had used shift = 2, instead of 1). I have been reading the log for several hours in real time, so I'm pretty sure that as soon as the new hour comes up, the log line is writing the MACD and Stochastic values of two 1H periods ago, not the last one that just closed.

What am I dong wrong? Is it possible that during the first OnTick event of each hour the "shift" parameter is not updated yet, which means that the record #0 in the iMACD/iStochastic arrays doesn't get shifted to record #1 until AFTER the first OnTick event is processed?

Thanks in advance.

 
corrado.toxiriMQL:

Hello,


Where are you getting the hour from?  you need to make sure it is from the H1 bar data and not the terminal time.

 
myMacd = iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

myStoch = iStochastic(NULL,PERIOD_H1,14,3,3,MODE_SMA,0,MODE_MAIN,1);

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)

 
William Roeder #:

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)

Thanks, this would explain my error, I'll give it a try.

Since I'm looking at executing an operation in the code right after the 1H candle switches to the next, should I just skip the first tick if I get the above errors and try again at the next tick?

 
Paul Anscombe #:

Where are you getting the hour from?  you need to make sure it is from the H1 bar data and not the terminal time.

Thanks, I guess this is another error I might be making: I'm currently using the Hour() function (returning the server time).

I guess I need to use the iTime() function, right?

 
William Roeder #:

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)

Hi William,

I added a check for those errors in my code which looks like in the example below.

I'd love to hear your opinion about the position of the check: is it enough to check for the 4066/4073 errors the first time I call iTime for the current candle, or do I need to handle the error again when calling the iMACD function afterwards for the previous 3 closed candles?

In short, can we assume that - if the history has been updated for that specific symbol/period - it's updated for all candle indicators and values?

//This code is inside the OnTick() function

   ResetLastError();
   currentCandleTime = iTime(NULL, PERIOD_H1,0);

   if(_LastError == ERR_HISTORY_WILL_UPDATED || _LastError == ERR_NO_HISTORY_DATA)
     {
      if(DebugMessages)
         Print("****DEBUG**** - Candle History not ready, skipping this Tick ****");
      return;
     }

    //Find the MACD Instogram values for the last 3 H1 closed candles

      myMacdIstogram = iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,1) - iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
      myMacdIstogramPrev = iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,2) - iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
      myMacdIstogramPrev2 = iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_MAIN,3) - iMACD(NULL,PERIOD_H1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,3);

    //Rest of the code follows


 
corrado.toxiriMQL #: In short, can we assume that - if the history has been updated for that specific symbol/period - it's updated for all candle indicators and values?

What part of “before accessing indicator values” was unclear?

 
William Roeder #:

What part of “before accessing indicator values” was unclear?

Hi William,

thank you for your patience. I do check for errors 4066/4073 and handle them before accessing the iMACD indicator's value.

However, my question is if I need to check/handle those error for every subsequent access to the same (or other) indicator values, or if I can assume that - once the candle history gets updated - it's valid for all subsequent indicator calls within the same tick.


PS:

Meanwhile, after your suggestion to check for those errors and Paul's suggestion to use candle time, I modified the EA and it's not making the error for which I opened this thread, so thank you again!

Reason: