Skip to content

EA Doctor rule · OnTick and performance

Bar-based logic runs on every tick

OnTick() copies bar data and makes entry decisions on every tick, with no check for a new bar anywhere in the EA.

Why it matters

OnTick() runs on every price change, often many times a second. Copying indicator buffers and evaluating bar signals on each tick repeats the same work, makes tests in the "every tick" modes slow, and evaluates conditions against a bar that is still forming, so a signal can appear and vanish within the same bar.

Logic that decides on closed bars should run once, when a new bar opens: compare the open time of bar 0 with the time saved on the last run.

When the rule fires

OnTick() (or a function it calls) copies bar data (CopyBuffer(), CopyRates(), CopyClose() …) and opens trades, and nothing in the file detects a new bar: no iTime(), SERIES_LASTBAR_DATE, CopyTime(), Bars(), iBars(), PeriodSeconds(), a static datetime, a bar's .time, or a name like new bar or last bar time.

When it stays quiet

Any new-bar check is present. Parts that must act on every tick (a trailing stop) can stay outside it.

How to fix it

  1. Add an IsNewBar() helper that compares iTime(_Symbol, PERIOD_CURRENT, 0) with the time it saved, and return early from the bar logic when it is the same bar.
  2. Read signals from closed bars (index 1 and older) inside it.

Code that fires it

MQL5 · fires this rule
int fastH = INVALID_HANDLE, slowH = INVALID_HANDLE;

int OnInit()
  {
   fastH = iMA(_Symbol, PERIOD_CURRENT, 10, 0, MODE_EMA, PRICE_CLOSE);
   slowH = iMA(_Symbol, PERIOD_CURRENT, 30, 0, MODE_EMA, PRICE_CLOSE);
   return(fastH == INVALID_HANDLE || slowH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
  }

void OnTick()
  {
   double f[], s[];
   if(CopyBuffer(fastH, 0, 1, 2, f) != 2 || CopyBuffer(slowH, 0, 1, 2, s) != 2)
      return;
   if(f[0] < s[0] && f[1] > s[1] && PositionsTotal() == 0)
      if(!trade.Buy(InpLots, _Symbol))
         Print("Buy failed: ", trade.ResultRetcodeDescription());
  }

Fixed code

MQL5 · fixed
int fastH = INVALID_HANDLE, slowH = INVALID_HANDLE;

int OnInit()
  {
   fastH = iMA(_Symbol, PERIOD_CURRENT, 10, 0, MODE_EMA, PRICE_CLOSE);
   slowH = iMA(_Symbol, PERIOD_CURRENT, 30, 0, MODE_EMA, PRICE_CLOSE);
   return(fastH == INVALID_HANDLE || slowH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
  }

bool IsNewBar()
  {
   static datetime last = 0;
   const datetime now = iTime(_Symbol, PERIOD_CURRENT, 0);
   if(now == 0 || now == last)
      return(false);
   last = now;
   return(true);
  }

void OnTick()
  {
   if(!IsNewBar())
      return;
   double f[], s[];
   if(CopyBuffer(fastH, 0, 1, 2, f) != 2 || CopyBuffer(slowH, 0, 1, 2, s) != 2)
      return;
   if(f[0] < s[0] && f[1] > s[1] && PositionsTotal() == 0)
      if(!trade.Buy(InpLots, _Symbol))
         Print("Buy failed: ", trade.ResultRetcodeDescription());
  }

The fixed code shows this rule’s fix and compiles in MetaEditor for MetaTrader 5 (with CTrade trade, InpLots and InpMagic declared, as in a typical EA). Other rules may still apply to it as a fragment: check your whole EA.

More rules on ontick and performance

Sources

Get your EA fixed

Check a whole EA for this and 29 other mistakes with EA Doctor, free and in your browser. Or send it to us: we confirm the price in writing, then fix it.