MQL4 CODE BASICS

EA Opens Multiple Trades on the Same Candle (MQL4 Fix)

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.

Meaning:
A candle can have hundreds of ticks. If your “buy condition” stays true, your EA might open a new trade on every tick… inside the same candle.

Fix #1 (Best): Only allow one trade per candle

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.

Simple “one trade per candle” code

// 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
}
Example:
If candle time is “10:15”, you allow one trade at 10:15.
Any more ticks during 10:15 are ignored.

Fix #2: Only run your entry logic on a NEW candle

Instead of blocking trades after one happens, you can make your EA only check entry conditions once per candle close/open.

New candle detector

// 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
Meaning:
If you only check once per candle, you can’t open 10 trades on the same candle.

Fix #3: Make sure you aren’t already in a trade

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.

Simple “already in a trade” check (current symbol only)

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;
Example:
If you already have a BUY open on EURUSD, don’t open another one.

Best Practice (Most Reliable): Use ALL THREE

If you want your EA to be rock solid, do this order:

“Rock solid” entry structure

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];
      }
   }
}

Common reasons this happens (quick list)


Simple Checklist

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