If your EA opens 2, 3, 10 trades on the same candle, it usually means one thing: your entry code is running on every tick.
This is the most common solution. You store the time of the last candle where you placed a trade. If it’s the same candle again, you do nothing.
// Put this at the top of your EA (global variable)
datetime lastTradeCandleTime = 0;
// Call this inside OnTick() where you open trades
datetime candleTime = Time[0]; // current candle open time
if(candleTime == lastTradeCandleTime)
{
// We already traded this candle
return;
}
// If your entry condition is true and you successfully open a trade:
int ticket = OrderSend(Symbol(), OP_BUY, 0.10, Ask, 3, 0, 0, "My EA", 0, 0, clrLime);
if(ticket > 0)
{
lastTradeCandleTime = candleTime; // block more trades this candle
}
Instead of blocking trades after one happens, you can make your EA only check entry conditions once per candle close/open.
// Put this function in your EA
bool IsNewBar()
{
static datetime lastBarTime = 0;
if(Time[0] != lastBarTime)
{
lastBarTime = Time[0];
return(true);
}
return(false);
}
// In OnTick()
if(!IsNewBar())
return;
// Now your entry checks run once per candle
Sometimes your EA is allowed to open multiple trades because it never checks if a trade already exists. This is a very common missing piece.
bool HasOpenTradeForThisSymbol()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
continue;
if(OrderSymbol() != Symbol())
continue;
// Market orders only:
if(OrderType() == OP_BUY || OrderType() == OP_SELL)
return(true);
}
return(false);
}
// Use it before placing a new trade:
if(HasOpenTradeForThisSymbol())
return;
If you want your EA to be rock solid, do this order:
void OnTick()
{
if(!IsNewBar())
return;
if(HasOpenTradeForThisSymbol())
return;
// Your entry conditions go here...
bool buySignal = (Close[1] > Open[1]); // example only
if(buySignal)
{
RefreshRates();
int ticket = OrderSend(Symbol(), OP_BUY, 0.10, Ask, 3, 0, 0, "My EA", 0, 0, clrLime);
if(ticket > 0)
{
// Optional: store lastTradeCandleTime if you want extra safety
// lastTradeCandleTime = Time[0];
}
}
}
If you add the new candle check + open trade check, the “multiple trades on one candle” problem usually disappears instantly.
Click here to return to MQL4 Basics Directory