EA Doctor rule · Signals and data
Signal read from the bar that is still forming
The EA compares indicator values of bar 0, which change with every tick until the bar closes, so its entries differ from what the chart shows later.
Why it matters
Index 0 of a series array is the current bar, and its indicator values move with every tick until the bar closes. A condition on buffer[0] can be true at one tick and false at the next.
The EA then enters on a crossover that is gone when the bar closes, and its live trades differ from what the chart and a closed-bar backtest show afterwards.
When the rule fires
An EA (it opens trades) copies an indicator buffer starting at position 0 into an array set as series (ArraySetAsSeries(…, true)) and compares element [0] of that array.
When it stays quiet
Signals from closed bars (start position 1, or elements [1], [2] …), and indicators, where bar 0 is drawn on purpose.
How to fix it
- Copy from position 1:
CopyBuffer(handle, 0, 1, 2, buf)gives the last two closed bars. - Evaluate the signal once per new bar.
Code that fires it
int rsiH = INVALID_HANDLE;
int OnInit()
{
rsiH = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE);
return(rsiH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
void OnTick()
{
double rsi[];
ArraySetAsSeries(rsi, true);
if(CopyBuffer(rsiH, 0, 0, 2, rsi) != 2)
return;
if(rsi[1] < 30.0 && rsi[0] >= 30.0 && PositionsTotal() == 0)
if(!trade.Buy(InpLots, _Symbol))
Print("Buy failed: ", trade.ResultRetcodeDescription());
}Fixed code
int rsiH = INVALID_HANDLE;
int OnInit()
{
rsiH = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE);
return(rsiH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
void OnTick()
{
static datetime lastBar = 0;
const datetime bar = iTime(_Symbol, PERIOD_CURRENT, 0);
if(bar == lastBar)
return;
lastBar = bar;
double rsi[];
ArraySetAsSeries(rsi, true);
if(CopyBuffer(rsiH, 0, 1, 2, rsi) != 2) // [0] = last closed bar, [1] = the one before
return;
if(rsi[1] < 30.0 && rsi[0] >= 30.0 && 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 signals and data
- MediumCopyBuffer() or CopyRates() result not checkedA Copy function is called as a plain statement, so the code reads the array even when nothing was copied.
- MediumIndicator handle not checked for INVALID_HANDLEAn indicator handle is stored without checking for INVALID_HANDLE, so a missing custom indicator goes unnoticed and every read fails.
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.