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
- Add an
IsNewBar()helper that comparesiTime(_Symbol, PERIOD_CURRENT, 0)with the time it saved, and return early from the bar logic when it is the same bar. - Read signals from closed bars (index 1 and older) inside it.
Code that fires it
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
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
- HighIndicator handle created on every tickAn indicator function such as iMA() or iCustom() is called in OnTick(), so a handle is requested on every tick instead of once in OnInit().
- LowSleep() inside OnTick()OnTick() pauses with Sleep(), which holds up every other event of the EA and leaves it working with old prices.
- MediumThe whole trade history loaded on every tickHistorySelect(0, …) is called from OnTick(), so every deal and order of the account is loaded again on each tick.
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.