Download MetaTrader 5

Problem with iTime in expert advisors

To add comments, please log in or register
TraderTester
8
TraderTester  

Hi everyone,

With my EA I have a strange problem with iTime. I have to check the last bar with iTime using this code:

iTime("SYMBOL",Period(),1);

I'm trying to get the information from multiple crosses, but I get weird data. If you try to run this debug code you can check out what is wrong .

int OnInit()
  {
   ff = iTime(Symbol(),Period(),0);
   return(INIT_SUCCEEDED);
  }
datetime ff ;
void OnTick()
  {
     if (ff != iTime(Symbol(),Period(),0)){
      int ts = TimeSeconds(TimeCurrent());
      if ( ts <= 10) {
        // Wait ten seconds
      return;
        
      }
       ff = iTime(Symbol(),Period(),0);
       
      LogToFile("EURJPY"," " + iTime("EURJPY",Period(),1));
      LogToFile("EURGBP"," " + iTime("EURGBP",Period(),1));
      LogToFile("EURUSD"," " + iTime("EURUSD",Period(),1));
      LogToFile("USDJPY"," " + iTime("USDJPY",Period(),1));
      LogToFile("GBPJPY"," " + iTime("GBPJPY",Period(),1));
      LogToFile("GBPUSD"," " + iTime("GBPUSD",Period(),1));
      LogToFile("AUDCAD"," " + iTime("AUDCAD",Period(),1));
      LogToFile("AUDNZD"," " + iTime("AUDNZD",Period(),1));
      LogToFile("AUDUSD"," " + iTime("AUDUSD",Period(),1));
      LogToFile("NZDCAD"," " + iTime("NZDCAD",Period(),1));
      LogToFile("USDCAD"," " + iTime("USDCAD",Period(),1));
      LogToFile("NZDUSD"," " + iTime("NZDUSD",Period(),1));
      LogToFile("CADCHF"," " + iTime("CADCHF",Period(),1));
      LogToFile("GBPCHF"," " + iTime("GBPCHF",Period(),1));
      LogToFile("NZDCHF"," " + iTime("NZDCHF",Period(),1));
      LogToFile("GBPCAD"," " + iTime("GBPCAD",Period(),1));
      LogToFile("GBPNZD"," " + iTime("GBPNZD",Period(),1));
      LogToFile("NZDCAD"," " + iTime("NZDCAD",Period(),1));
   }
  }
  string LogToFile(string s1="",string s2="",string s3="",string s4="",string s5="",string s6="",string s7="",string s8="",
                 string s9="",string s10="",string s11="",string s12="",string s13="",string s14="",string s15="") 
  {
  
      string out=StringTrimRight(StringConcatenate(IntegerToString( Hour())+":"+IntegerToString(Minute())+":"+IntegerToString(Seconds())+" - " ,s1," ",s2," ",s3," ",s4," ",s5," ",s6," ",s7," ",s8," ",
                                 s9," ",s10," ",s11," ",s12," ",s13," ",s14," ",s15));
      int h=FileOpen("TestiTIME_"+IntegerToString(Day())+".log",FILE_CSV|FILE_READ|FILE_WRITE,'~');
      FileSeek(h,0,SEEK_END);
      FileWrite(h,out);
      FileClose(h);

   return("");

If I launch the sample EA  on M1 I get this result:

18:32:11 - EURJPY  2017.05.04 11:59:00
18:32:11 - EURGBP  2017.05.04 11:59:00
18:32:11 - EURUSD  2017.05.04 15:59:00
18:32:11 - USDJPY  2017.05.04 18:31:00  <- EA is running on this cross
18:32:11 - GBPJPY  2017.05.04 11:59:00
18:32:11 - GBPUSD  2017.05.04 11:59:00
18:32:11 - AUDCAD  2017.05.04 11:59:00
18:32:11 - AUDNZD  2017.05.04 11:59:00
18:32:11 - AUDUSD  2017.05.04 11:59:00
18:32:11 - NZDCAD  2017.05.04 11:59:00
18:32:11 - USDCAD  2017.05.04 11:59:00
18:32:11 - NZDUSD  2017.05.04 11:59:00
18:32:11 - CADCHF  2017.05.04 11:59:00
18:32:11 - GBPCHF  2017.05.04 11:59:00
18:32:11 - NZDCHF  2017.05.04 11:59:00
18:32:11 - GBPCAD  2017.05.04 11:59:00
18:32:11 - GBPNZD  2017.05.04 11:59:00
18:32:11 - NZDCAD  2017.05.04 11:59:00
********************
18:33:11 - EURJPY  2017.05.04 18:31:00
18:33:11 - EURGBP  2017.05.04 18:31:00
18:33:11 - EURUSD  2017.05.04 18:31:00
18:33:11 - USDJPY  2017.05.04 18:32:00 <- EA is running on this cross
18:33:11 - GBPJPY  2017.05.04 18:31:00
18:33:11 - GBPUSD  2017.05.04 18:31:00
18:33:11 - AUDCAD  2017.05.04 18:31:00
18:33:11 - AUDNZD  2017.05.04 18:31:00
18:33:11 - AUDUSD  2017.05.04 18:31:00
18:33:11 - NZDCAD  2017.05.04 18:31:00
18:33:11 - USDCAD  2017.05.04 18:31:00
18:33:11 - NZDUSD  2017.05.04 18:31:00
18:33:11 - CADCHF  2017.05.04 18:31:00
18:33:11 - GBPCHF  2017.05.04 18:31:00
18:33:11 - NZDCHF  2017.05.04 18:31:00
18:33:11 - GBPCAD  2017.05.04 18:31:00
18:33:11 - GBPNZD  2017.05.04 18:31:00
18:33:11 - NZDCAD  2017.05.04 18:31:00
********************
18:34:12 - EURJPY  2017.05.04 18:33:00
18:34:12 - EURGBP  2017.05.04 18:33:00
18:34:12 - EURUSD  2017.05.04 18:33:00
18:34:12 - USDJPY  2017.05.04 18:33:00 <- EA is running on this cross
18:34:12 - GBPJPY  2017.05.04 18:33:00
18:34:12 - GBPUSD  2017.05.04 18:33:00
18:34:12 - AUDCAD  2017.05.04 18:33:00
18:34:12 - AUDNZD  2017.05.04 18:33:00
18:34:12 - AUDUSD  2017.05.04 18:33:00
18:34:12 - NZDCAD  2017.05.04 18:33:00
18:34:12 - USDCAD  2017.05.04 18:33:00
18:34:12 - NZDUSD  2017.05.04 18:33:00
18:34:12 - CADCHF  2017.05.04 18:33:00
18:34:12 - GBPCHF  2017.05.04 18:33:00
18:34:12 - NZDCHF  2017.05.04 18:33:00
18:34:12 - GBPCAD  2017.05.04 18:33:00
18:34:12 - GBPNZD  2017.05.04 18:33:00
18:34:12 - NZDCAD  2017.05.04 18:33:00

while on M5 

18:10:3 - EURJPY  2017.05.04 18:00:00
18:10:3 - EURGBP  2017.05.04 18:00:00
18:10:3 - EURUSD  2017.05.04 18:00:00
18:10:3 - USDJPY  2017.05.04 18:05:00   <---- The Ea is running on this cross
18:10:3 - GBPJPY  2017.05.04 18:00:00
18:10:3 - GBPUSD  2017.05.04 18:00:00
18:10:3 - AUDCAD  2017.05.04 18:00:00
18:10:3 - AUDNZD  2017.05.04 18:00:00
18:10:3 - AUDUSD  2017.05.04 18:00:00
18:10:3 - NZDCAD  2017.05.04 18:00:00
18:10:3 - USDCAD  2017.05.04 18:00:00
18:10:3 - NZDUSD  2017.05.04 18:00:00
18:10:3 - CADCHF  2017.05.04 18:00:00
18:10:3 - GBPCHF  2017.05.04 18:00:00
18:10:3 - NZDCHF  2017.05.04 18:00:00
18:10:3 - GBPCAD  2017.05.04 18:00:00
18:10:3 - GBPNZD  2017.05.04 18:00:00
18:10:3 - NZDCAD  2017.05.04 18:00:00
****************
18:15:3 - EURJPY  2017.05.04 18:05:00
18:15:3 - EURGBP  2017.05.04 18:05:00
18:15:3 - EURUSD  2017.05.04 18:05:00
18:15:3 - USDJPY  2017.05.04 18:10:00  <---- The Ea is running on this cross
18:15:3 - GBPJPY  2017.05.04 18:05:00
18:15:3 - GBPUSD  2017.05.04 18:05:00
18:15:3 - AUDCAD  2017.05.04 18:05:00
18:15:3 - AUDNZD  2017.05.04 18:05:00
18:15:3 - AUDUSD  2017.05.04 18:05:00
18:15:3 - NZDCAD  2017.05.04 18:05:00
18:15:3 - USDCAD  2017.05.04 18:05:00
18:15:3 - NZDUSD  2017.05.04 18:05:00
18:15:3 - CADCHF  2017.05.04 18:05:00
18:15:3 - GBPCHF  2017.05.04 18:05:00
18:15:3 - NZDCHF  2017.05.04 18:05:00
18:15:3 - GBPCAD  2017.05.04 18:05:00
18:15:3 - GBPNZD  2017.05.04 18:05:00
18:15:3 - NZDCAD  2017.05.04 18:05:00
**************
18:20:3 - EURJPY  2017.05.04 18:10:00
18:20:3 - EURGBP  2017.05.04 18:10:00
18:20:3 - EURUSD  2017.05.04 18:10:00
18:20:3 - USDJPY  2017.05.04 18:15:00   <---- The Ea is running on this cross
18:20:3 - GBPJPY  2017.05.04 18:10:00
18:20:3 - GBPUSD  2017.05.04 18:10:00
18:20:3 - AUDCAD  2017.05.04 18:10:00
18:20:3 - AUDNZD  2017.05.04 18:10:00
18:20:3 - AUDUSD  2017.05.04 18:10:00
18:20:3 - NZDCAD  2017.05.04 18:10:00
18:20:3 - USDCAD  2017.05.04 18:10:00
18:20:3 - NZDUSD  2017.05.04 18:10:00
18:20:3 - CADCHF  2017.05.04 18:10:00
18:20:3 - GBPCHF  2017.05.04 18:10:00
18:20:3 - NZDCHF  2017.05.04 18:10:00
18:20:3 - GBPCAD  2017.05.04 18:10:00
18:20:3 - GBPNZD  2017.05.04 18:10:00
18:20:3 - NZDCAD  2017.05.04 18:10:00

As you can see, the results are weird. The cross where the EA is running works well but on all the other crosses it seems that data aren't refreshed.

And what's more, I tried also on two MT4 platforms (activetrades). If I launch the sample ea at the same time, one works badly (the results above) and  the other one works always well.

I'm really confused.

Can anyone help me?

Thank you

Files:
Alain Verleyen
Moderator
33290
Alain Verleyen  

TraderTester:

Can anyone help me?

Thank you

You can't expect all the symbols to be synchronized. A new bar appears only on the first tick of a period, it can be after 1 ms, 1 sec, 10 sec or you can even miss a bar(certainly on M1).

To add comments, please log in or register