Download MetaTrader 5

Tester' TimeCurrent() vs Live' TimeCurrent() Differs?

To add comments, please log in or register
Have you developed a profitable strategy? Launch your trading signal and make profit from it!
Ndumiso Mavuso
4373
Ndumiso Mavuso 2016.02.11 18:27 

I have noticed something which I do not know if its true yet, I would like someone to give their opinion on this.

I think Strategy Tester calculations on TimeCurrent() gives different results than live, for an example, If I send an order ==> record the time when it is sent ==> take the recorded time and compare it with (current time - 2seconds). On Tester 2 seconds might be faster compared to real life?

To get same Speed Live as was in Strategy Tester, I think 2 seconds would have to be -2seconds because 2 seconds will be too slow Live.

A code of what I am saying here:

//+------------------------------------------------------------------+
//|                                                      Testing.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+-----------------------------------------------------------Ndumiso+
  #property copyright "Copyright 2015, MetaQuotes Software Corp."
  #property link      "https://www.mql5.com"
  #property version   "1.00"
  
  extern double TimeToChnge=2;//The Hline will move after 2 seconds
  extern double Dis=5;

  double UsePoint;
  int TimeBar,Obj;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+-----------------------------------------------------------Ndumiso+
  int OnInit()
  {
//---
   UsePoint = PipPoint(Symbol());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+-----------------------------------------------------------Ndumiso+
  void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+-----------------------------------------------------------Ndumiso+
  void OnTick()
  {
  
  if(ObjectFind(0,"Hline")!=0){
  if(ObjectCreate("Hline", OBJ_HLINE, 0,0,NormalizeDouble(Bid + Dis * UsePoint,Digits),0,0)!=-1) TimeBar=TimeCurrent();//record the time
  ObjectSet("Hline", OBJPROP_STYLE, 3);
  ObjectSet("Hline", OBJPROP_COLOR, LimeGreen);}
  
 if(ObjectFind(0,"Hline")!=-1)  {Obj=ObjectGet("Hline",OBJPROP_PRICE1);}     

  if (Obj!=0)
  {
  if (TimeBar<TimeCurrent()-TimeToChnge)
  {
  if(ObjectSet("Hline",OBJPROP_PRICE1,NormalizeDouble(Bid + Dis * UsePoint,Digits))) TimeBar=TimeCurrent();
  }
  }
   
  } 
//+------------------------------------------------------------------+
//| Broker Digits                                                    |
//+-----------------------------------------------------------Ndumiso+
  double PipPoint(string Currency)
  {
  int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
  if(CalcDigits == 2 || CalcDigits == 3)double CalcPoint = 0.01;
  else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
  return(CalcPoint);
  }
//+------------------------------------------------------------------+
//|                            End                                   |
//+-----------------------------------------------------------Ndumiso+
Alain Verleyen
Moderator
30724
Alain Verleyen 2016.02.12 09:41  

https://docs.mql4.com/dateandtime/timecurrent

During testing in the Strategy Tester, TimeCurrent() is simulated according to historical data.

TimeCurrent - MQL4 Documentation
TimeCurrent - MQL4 Documentation
  • docs.mql4.com
TimeCurrent - MQL4 Documentation - MQL4 Documentation
Ndumiso Mavuso
4373
Ndumiso Mavuso 2016.02.12 16:53  
Which one you think might be accurate  between TimeCurrent vs GetTickCount? During Back Testing

If TimeCurrent is used the Hline will move accurately during back test But move slower Live, Or mybe its ticks that are coming faster Live vs Back test simulated ticks?
Ndumiso Mavuso
4373
Ndumiso Mavuso 2016.02.13 18:31  

Ok I just read somewhere that back-tester time-current and real-life time-current can never be the same.

I just want to know in which scale does it differ, can it be worked around so that it can be the same. Or any other way to count seconds after a function is executed? that can if not close match tester and live counts.

Ndumiso Mavuso
4373
Ndumiso Mavuso 2016.02.15 14:29  

Problem is solved.

On Live OnTick() TimeCurrent() wait for a tick from a broker, if there is no tick, TimeCurrent calculations do not work. While on tester ticks are always available so TimeCurrent calculations are accurate.

So I added Sleep(100) and TimeCurrent calculations seems to return accurate e.g Hline updated every after 2sec.

//+------------------------------------------------------------------+
//|                                                      Testing.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+-----------------------------------------------------------Ndumiso+
  #property copyright "Copyright 2015, MetaQuotes Software Corp."
  #property link      "https://www.mql5.com"
  #property version   "1.00"
  
  extern double TimeToChnge=2;//The Hline will move after 2 seconds
  extern double Dis=5;

  double UsePoint;
  int TimeBar,Obj;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+-----------------------------------------------------------Ndumiso+
  int OnInit()
  {
//---
   UsePoint = PipPoint(Symbol());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+-----------------------------------------------------------Ndumiso+
  void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+-----------------------------------------------------------Ndumiso+
  void OnTick()
  {
  while (!IsStopped())//Added
  {   
  if(ObjectFind(0,"Hline")!=0){
  if(ObjectCreate("Hline", OBJ_HLINE, 0,0,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK)+Step * UsePoint,Digits),0,0)!=-1) TimeBar=TimeCurrent();
  ObjectSet("Hline", OBJPROP_STYLE, 3);
  ObjectSet("Hline", OBJPROP_COLOR, LimeGreen);}
  
  
  if(ObjectFind(0,"Hline")!=-1)  {Pric=ObjectGet("Hline",OBJPROP_PRICE1); Obj=ObjectGet("Hline",OBJPROP_PRICE1);}else Pric=0;     

  if (Obj!=0)
  {
  if (TimeBar<=TimeCurrent()-TimeToChnge)
  {
  Print(TimeToStr(TimeCurrent(),TIME_SECONDS));
  if(ObjectSet("Hline",OBJPROP_PRICE1,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK) + Step * UsePoint,Digits))) TimeBar=TimeCurrent();
  }
  }
  Sleep (1000);//And added
  }
  } 
//+------------------------------------------------------------------+
//| Broker Digits                                                    |
//+-----------------------------------------------------------Ndumiso+
  double PipPoint(string Currency)
  {
  int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
  if(CalcDigits == 2 || CalcDigits == 3)double CalcPoint = 0.01;
  else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
  return(CalcPoint);
  }
//+------------------------------------------------------------------+
//|                            End                                   |
//+-----------------------------------------------------------Ndumiso+
To add comments, please log in or register