DEV Community

Omais Ali
Omais Ali

Posted on

Running one MQL5 EA on several symbols without missing new bars

A common request is an Expert Advisor that sits on one chart but trades a basket, for example EURUSD, GBPUSD and USDJPY. The first version usually puts all the logic in OnTick and checks every symbol there. It works in a quick test and then misbehaves on a live account. This post explains why, and shows a small pattern that has held up well for me.

Why OnTick is the wrong clock for a basket

OnTick fires only for ticks of the chart symbol. If the EA is attached to EURUSD and EURUSD goes quiet for twenty seconds, your GBPUSD and USDJPY logic also waits twenty seconds, even if those markets are moving. Worse, if your rule is "act once when a new bar opens", a slow chart symbol can make you see the GBPUSD new bar late, or in rare cases skip it when two bars pass between checks.

The fix is to drive the basket from a timer and to track the last bar time separately for each symbol.

The pattern

input string InpSymbols = "EURUSD,GBPUSD,USDJPY";
input ENUM_TIMEFRAMES InpTf = PERIOD_M15;

string   g_syms[];
datetime g_lastBar[];

int OnInit()
{
   int n = StringSplit(InpSymbols, ',', g_syms);
   if(n <= 0) return INIT_PARAMETERS_INCORRECT;
   ArrayResize(g_lastBar, n);
   for(int i = 0; i < n; i++)
   {
      StringTrimLeft(g_syms[i]);
      StringTrimRight(g_syms[i]);
      if(!SymbolSelect(g_syms[i], true))
      {
         Print("Cannot select ", g_syms[i]);
         return INIT_FAILED;
      }
      g_lastBar[i] = 0;
   }
   EventSetTimer(1);
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) { EventKillTimer(); }

void OnTimer()
{
   for(int i = 0; i < ArraySize(g_syms); i++)
   {
      datetime t = iTime(g_syms[i], InpTf, 0);
      if(t == 0) continue;              // history not ready yet
      if(g_lastBar[i] == 0) { g_lastBar[i] = t; continue; }
      if(t != g_lastBar[i])
      {
         g_lastBar[i] = t;
         OnNewBar(g_syms[i]);
      }
   }
}

void OnTick() { OnTimer(); }

void OnNewBar(const string sym)
{
   Print(sym, " new bar at ", TimeToString(iTime(sym, InpTf, 0)));
   // signal and order logic for sym goes here
}
Enter fullscreen mode Exit fullscreen mode

Details that matter

  1. The first pass only records the bar time. Without that, a restart would treat the current bar as new on every symbol and fire a burst of entries.

  2. iTime can return 0 while history for a symbol is still loading, especially right after the terminal starts. Skipping that pass is safer than acting on missing data.

  3. Calling the same routine from OnTick keeps the chart symbol responsive, and the per symbol bar time makes double processing harmless.

  4. Every symbol needs its own spread, stops level and volume step. Never reuse values read from the chart symbol. Read them with SymbolInfoDouble and SymbolInfoInteger for the symbol you are about to trade.

  5. Use one magic number for the EA and filter positions by both magic and symbol, so the exit logic for USDJPY never touches the GBPUSD position.

  6. Broker suffixes are real. If the broker lists EURUSD.m, the input list must match, so validate each name in OnInit and fail loudly rather than trading two symbols out of three.

Testing it

In the Strategy Tester, choose "Every tick based on real ticks" and make sure every symbol in the list has history downloaded. The tester loads extra symbols on demand, and a missing one usually shows up as a silent lack of trades rather than an error, so check the journal for each name.

A one second timer is plenty for bar based logic. If you need tick level reactions on several symbols, that is a different design and usually calls for one EA instance per chart instead.

Top comments (0)