Timestamp mismatch when using iCustom

 

The code below uses iCustom to get the value of the Stochastic indicator on CADJPY on PERIOD_M5. This is not a problem with the Stochastic indicator or this particular symbol, as the same problem manifests with any symbol/indicator.

The problem is that when this EA is applied to any symbol other than CADJPY, I get different timestamps for the same index, before and after using iCustom. I'm using the latest build of MT4 (1260).

This is not an issue with chart history, as the history is loaded and the same thing happens repeatedly.

Any help would be greatly appreciated!

datetime start_date=D'2020.04.27 19:35';
string symbol="CADJPY";
int tf=PERIOD_M5;
string indicator="Stochastic";

int OnInit()
{
        int shift=iBarShift(symbol,tf,start_date);
        Print("Value for "+TimeToStr(iTime(symbol,tf,shift))+"="+get(shift));
        ExpertRemove();
        return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) { }

double get(int shift)
{
        double v1,v2;
        int t1,t2;

        t1=iTime(symbol,tf,shift);              
        v1=iCustom(symbol,tf,indicator,0,shift);
        v2=iCustom(symbol,tf,indicator,1,shift);
        t2=iTime(symbol,tf,shift);              
        if (t1!=t2)
        {
                Print("Timestamp mismatch, t1="+TimeToStr(t1)+", t2="+TimeToStr(t2));
        }
        return(v1);
}
Documentation on MQL5: Technical Indicators / iCustom
Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
[in]  The name of the custom indicator, with path relative to the root directory of indicators (MQL5/Indicators/). If an indicator is located in a subdirectory, for example, in MQL5/Indicators/ [in] input-parameters of a custom indicator, separated by commas. Type and order of parameters must match. If there is no parameters specified, then...
 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. 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
 

1. Because I've never been here before and when I clicked "Traders Forum" the first suitable section is this one (and is simply called "Expert Advisors and Automated Trading", not "MT5 Expert Advisors and Automated Trading"). I didn't look all the way at the bottom where the MT4 section has been buried and there's no way for me to move this post. Apologies for ruining your day.

2. As stated in my post, all history is loaded and I get the same error repeatedly. I know about 4066 and 4073, these errors would only show up if history wasn't loaded.

 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
FX4Lulz: 2. As stated in my post, all history is loaded and I get the same error repeatedly. I know about 4066 and 4073, these errors would only show up if history wasn't loaded.
int OnInit()
{
        int shift=iBarShift(symbol,tf,start_date);
        Print("Value for "+TimeToStr(iTime(symbol,tf,shift))+"="+get(shift));
  1. You know about 4066/73. Yet you do nothing to prevent them. Link #1.2  Unless you've accessed CADJPY/M5 in the last 10 minutes, your script fails.
  2. don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Reason: