Build notes
MQL5 EA for Multiple Symbols: One Expert Advisor Watching Every Symbol Without a Chart for Each
Batdorj Byambadorj · · 5 min read
MetaTrader 5 runs one Expert Advisor per chart. For a price alert tool that is an awkward rule. A trader can have alerts on several symbols and charts open for only some of them, and a chart that already carries a trading robot has no room for a second EA.
So my alert EA is attached once, to any spare chart, and watches every symbol that has an alert on it. The design is short to describe. What took the time is that most of the ways to get it wrong produce no error at all. The EA sits there looking alive and never fires, which for an alert tool is the worst failure there is.
Here are the four places it can go quiet, and what I measured at each.
1. OnTick is the wrong clock
OnTick runs when the chart's own symbol ticks. An EA attached to a gold chart gets gold's ticks and nobody else's. Anything it wants to know about EURUSD is only looked at when gold happens to move.
The engine therefore does nothing in OnTick. It runs on a millisecond timer, every 250 ms, and asks for each symbol's price itself.
int OnInit()
{
if(!EventSetMillisecondTimer(250))
return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
void OnTick()
{
// deliberately empty: this only fires for the chart's own symbol
}
void OnTimer()
{
// for every symbol that has an alert:
// SymbolInfoTick(symbol, tick) and evaluate
}
2. SymbolInfoTick needs SymbolSelect first
SymbolInfoTick(symbol, tick) works for any symbol the broker offers. But if the symbol is not in Market Watch, what comes back can be old or empty, and nothing announces it.
The cure is one call per symbol before the first read: SymbolSelect(symbol, true). I tested the case that worried me most, a symbol with no chart open and hidden from Market Watch. With SymbolSelect it worked end to end on the first test, on EURGBP. The trader never has to open a chart for the engine's benefit.
3. A wrong name is error 4305, and it has to be loud
Brokers do not spell symbols the same way. One has EURUSD, another EURUSDm, another EURUSD.r. Ask SymbolSelect for a spelling this broker does not have and it fails with error 4305.
That error is the single most useful thing the platform tells you here, so the engine does not swallow it. It goes to the log and into the engine's status file as a line of its own:
ResetLastError();
bool ok = SymbolSelect(name, true);
if(!ok)
PrintFormat("SymbolSelect(%s) failed, err=%d - check the broker's spelling (suffix?)",
name, GetLastError());
It also keeps trying, once every 40 timer passes, about every ten seconds. A symbol can appear later, and a name can be corrected. Giving up for good would turn a typo into a dead alert. What a suffix means for someone simply using the EA is covered in the FAQ.
4. The first tick can be late
Straight after SymbolSelect, a symbol may have nothing to report. The first tick can lag, and for a moment even the digits can read as zero. An engine that evaluates at once is comparing a line against a price that is not there.
Two rules handle it:
- A symbol is not evaluated until it has been seen with two distinct ticks. A crossing is the move between two observations, so one observation proves nothing.
- The previous and current tick only advance when something really changed: time, bid or ask. If an unchanged tick pushed "current" into "previous", the two would be equal and every crossing would disappear.
A symbol that has stopped moving is reported as stale and otherwise left alone. Over a weekend every symbol is stale, and that is correct.
What it looked like when it worked
On 10 October the EA was attached to a gold M15 chart. It had four alerts on Bitcoin. No Bitcoin chart was open anywhere in the terminal. All four were watched and all four fired.
Nine days later the EA ran at a broker that puts an m on the end of every symbol, and alerts on EURUSDm and XAUUSDm fired end to end.
Reading the charts, not only the prices
Trendlines live on charts, so the engine also has to see every chart. That part raises no surprises:
long cid = ChartFirst();
while(cid >= 0)
{
string sym = ChartSymbol(cid);
int total = ObjectsTotal(cid, -1, -1);
for(int i = 0; i < total; i++)
{
string name = ObjectName(cid, i, -1, -1);
if(ObjectGetInteger(cid, name, OBJPROP_TYPE) == OBJ_TREND)
{
// anchors: OBJPROP_TIME and OBJPROP_PRICE, index 0 and 1
}
}
cid = ChartNext(cid);
}
How a line's price is then worked out is its own subject, in the trendline article.
Clicks on the other charts
This was the part I expected to be impossible. OnChartEvent delivers events only from the chart the EA is attached to. A button drawn on any other chart will never report a click.
It does not need to. An OBJ_BUTTON is the terminal's control, not mine. When the trader presses it, MT5 itself sets OBJPROP_STATE on the object, whether or not any program is running on that chart. So the engine draws its button on every chart, reads the state on each 250 ms pass, acts, and sets it back. OBJPROP_SELECTED on a trendline can be read the same way, which is how the engine knows which line the trader means.
The result: an alert can be set from any chart, with one EA and no helper indicators. I confirmed it with the EA on a gold chart and alerts created from AUDUSD and EURUSD charts. One limit remains. A full dialog window built on the standard library's CAppDialog only works on the EA's own chart, because its controls do depend on events.
The short version
- One EA, any chart. Evaluate in
OnTimer, leaveOnTickempty. SymbolSelect(symbol, true)for every symbol beforeSymbolInfoTick.- Treat error 4305 as a message to the user about spelling, and retry.
- Wait for two distinct ticks before evaluating a symbol.
- For clicks on other charts, do not wait for an event. Read
OBJPROP_STATE.
The EA these notes come from is free to download.
Batdorj Byambadorj
Batdorj Byambadorj has been trading forex for his own account since 2007, mostly gold. He is the sole developer of fx-alert and writes every article on this site himself.