Question about unsynced bars read by CopyOpen/CopyRates/... - page 2

Stanislav Korotky #:

This is unclear. Every new bar is added at index 0, but this happens at arbitrary moment, not at the timer event.

What is your requirement?

PS&NB: the tester handles ticks/bars differently depending from the mode - you did not specify which you use.

Ok. The question is how to know when is that "moment", if it is not on timer event?
I am inserting in image, what settings for strategy tester i am using.
Alexibg1000 #:
Ok. The question is how to know when is that "moment", if it is not on timer event?
I am inserting in image, what settings for strategy tester i am using.

Forum on trading, automated trading systems and testing trading strategies

Question about unsynced bars read by CopyOpen/CopyRates/...

Ryan L Johnson, 2026.10.07 23:02

I experienced issues in the Tester while running a millisecond timer, so I conditionally switched back to OnTick() using MQLInfoInteger(MQL_TESTER).

bool   g_isTester = false;

//+------------------------------------------------------------------+
//| Init                                                             |
//+------------------------------------------------------------------+
int OnInit()
  {
   // MQL_TESTER is true in backtest AND optimization -> covers the whole tester.
   g_isTester = (bool)MQLInfoInteger(MQL_TESTER);

   if(!g_isTester)
     {
      // Live / demo: a timer drives the strategy.
      if(!EventSetTimer(InpTimerSeconds))
        {
         Print("EventSetTimer failed, error=", GetLastError());
         return(INIT_FAILED);
        }
     }
   // Tester: no timer on purpose -> OnTick() drives the strategy.

   PrintFormat("Init done. tester=%s", g_isTester ? "true" : "false");
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Deinit                                                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(!g_isTester)
      EventKillTimer();          // only kill what we created
  }

//+------------------------------------------------------------------+
//| Tester only: every modeled tick drives the strategy              |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(g_isTester)
      ProcessStrategy();
   // Live: intentionally empty, OnTimer() handles it.
  }

//+------------------------------------------------------------------+
//| Live only: periodic timer drives the strategy                    |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(!g_isTester)
      ProcessStrategy();
  }

// ProcessStrategy() is a custom function that contains your existing trading logic.
// This is merely a code snippet─not to be compiled without editing

Ryan L Johnson #:
I experienced issues in the Tester while running a millisecond timer, so I conditionally switched back to OnTick() using MQLInfoInteger(MQL_TESTER).
What issues ?
Alain Verleyen #:
What issues ?

A complete stall with a 21ms timer likely due to:

Forum on trading, automated trading systems and testing trading strategies

Question about unsynced bars read by CopyOpen/CopyRates/...

Ryan L Johnson, 2026.10.07 23:02

Note that the relevant documentation states that:

In general, when the timer period is reduced, the testing time is increased, as the handler of timer events is called more often (https://www.mql5.com/en/docs/event_handlers/ontimer).


Ryan L Johnson #:

Thanks for idea. But If my strategy is in OnTick function, there will be a big 'spam' of events. I think that there manually i have to check with small compare buffer for new bars. My strategy works only when bar completes ( in my case timeframe is H1).
Alexibg1000 #:
Ok. The question is how to know when is that "moment", if it is not on timer event?
I am inserting in image, what settings for strategy tester i am using.

You should detect the moment by tick analysis in OnTick, for example, so (just a draft):

void OnTick()
{
   static datetime lastBar;
   if(lastBar == iTime(_Symbol, _Period, 0)) return;
   lastBar = iTime(_Symbol, _Period, 0);
   ... // here you go only once for every new bar
}
Stanislav Korotky #:

You should detect the moment by tick analysis in OnTick, for example, so (just a draft):

Thank you very much for help! That will work, yeah.