In September we found that one of our own EAs could be removed from its chart when MetaTrader 4 started, and we wrote down when it happens: after a VPS reboot. This week we tested that sentence before publishing it, and it did not hold. What is ready when OnInit runs depends on how the EA reached the chart: an EA the terminal restored with its saved chart was not initialised until the terminal had connected, while an EA attached by a start-up configuration file was initialised at once, with no connection and no account.
The fix we made in September was right. The scenario we attached to it was not.
OnInit is the function MetaTrader calls when an EA starts. An EA that fails its start-up checks there is removed from its chart, so it matters which starts call it early.
It Depends on How the EA Was Started
A restored EA waited for the connection. An EA attached by a start-up configuration file was initialised within 25 milliseconds of being loaded, and again once the terminal had connected.
| How the EA reached the chart | When OnInit ran | Connected at that moment | Ran a second time |
|---|---|---|---|
| Restored with its saved chart, online | 2.6 seconds after the EA was loaded | Yes | No |
| Restored with its saved chart, offline | Not in 99 seconds | — | — |
| Start-up configuration file | 13 to 25 milliseconds after the EA was loaded | No | Yes, once connected |
These figures come from seven cold starts of a probe EA on MetaTrader 4 build 1492, on one demo terminal, on 2 December 2026. Windows was not restarted, so a network that comes up late is not in these runs. Nor is MetaTrader 5, or any other build.
What We Found in Our Own EA, and What We Called It
The EA was an opening-range breakout EA with optional entry filters. In September 2026 we ran a cold start of it on a demo terminal, and three start-up checks had the same flaw:
- The timer. The EA set a one-second timer in
OnInitwithEventSetTimer(opens in a new tab). The call failed with error 4030, which the MQL4 error list (opens in a new tab) namesERR_CHART_NOREPLY: the chart did not reply. The EA treated that as a failed initialisation, and MetaTrader removed it from the chart. - The bars. With that corrected, the next cold start stopped with
array out of range. The chart had no bars yet, and the code read the current bar. - The connection. A third check treated “no connection to the trade server yet” as a failure. We found this one by reading the code, not running it.
The same start-up code had been in every build we delivered for about 15 months, and nobody had reported it. It is the kind of defect that sits inside an EA that works. We found it only because we ran a cold start before delivering a correction.
We corrected all three so the EA waits and retries. Then we recorded and described the defect as what happens after a VPS reboot. That label is the part we had never tested.
Seven MetaTrader 4 Cold Starts With a Probe EA
To test the label we wrote a probe EA that only reads and logs: the bars on the chart, whether the terminal is connected, whether an account is loaded, and whether a timer can be set. One start had both kinds of EA, and one was void: the terminal had not kept the EA on its chart.
Restored with its saved chart. This is what the terminal does when it starts again after a shutdown.
- Online, in two starts:
OnInitran 2.6 seconds after the EA was loaded, with the terminal connected, the account loaded and the bars on the chart. It ran once. - Offline, in one start:
OnInitwas not called in the 99 seconds before we closed the terminal.
Attached by a start-up configuration file. This is how our September cold starts had been launched.
- In four starts,
OnInitran 13 to 25 milliseconds after the EA was loaded, with the terminal not connected and the account number at zero. - On a chart with no stored history the bar count was zero.
- In the three online starts,
OnInitran a second time 2.7 to 4.3 seconds later, once connected. NoOnDeinitran between the two, and the probe’s own counters kept their values.
The timer was set in all six calls, four of them before the connection.
Program Running (opens in a new tab) gives an EA a start delay of up to five seconds while the terminal prepares its data, and says that without a connection “this may lead to a delay in the start of an Expert Advisor”. It also lists a connection to an account among the events that load an EA attached before the terminal was authorised. It does not say which starts wait, and it does not mention a second OnInit with no OnDeinit before it. Our offline start also waited far past five seconds.
Two consequences for our own record. The reboot label is not supported: on the path a restart takes, OnInit never ran before the connection in our runs. And the refused timer stays a single measurement, in that EA and that set-up, which the probe did not reproduce.
Where an Early OnInit Is Real, and What Breaks There
The early OnInit is real for an EA attached before the terminal has connected. We measured it for a start-up configuration file, which names the EA, the symbol and the timeframe when the terminal is launched. That is how a script or a scheduled task starts a terminal with an EA on a chart. An EA attached by hand while the terminal is offline was not measured.
Four things break there:
- A start-up check that reports failure. The EA is removed. Launched from a configuration file, MetaTrader also closed the terminal.
- State rebuilt from the order list. In our September launches, one start had a trade already open. Before the login the EA’s order list was empty. After the login it showed the trade.
- Anything in
OnInitmeant to run once. It ran twice, andOnDeinitdid not run in between. - A read of the current bar on a chart with no stored history. There is no bar to read.
A failed start looks like this in the Experts log, with our probe’s name where yours will be:
initialization failed (1)uninit reason 8close terminal with code -12347Expert ColdStartProbe EURUSD,M5: removed
The journal adds initializing of ColdStartProbe EURUSD,M5 failed.
The rule behind those lines is documented. A non-zero return from OnInit means the initialisation failed, with the deinitialisation reason REASON_INITFAILED, which is reason 8 (opens in a new tab). The terminal reads that return value only in programs compiled with #property strict (Event Handling Functions (opens in a new tab)).
What the EA Should Do at Start-Up
Hold instead of fail, and make OnInit safe to run twice. At barmenteros FX, we treat “not ready yet” at start-up as a reason to wait, never as a failed initialisation. This is what we built into the corrected EA:
- A timer that cannot be set does not fail the start. The EA retries it from
OnTickevery 10 seconds. OnTickwaits until the chart has bars before it reads one.- A missing connection pauses order operations. It does not fail the start.
- The count of trades already taken is stored, so an empty order list before the login does not reset it.
- The EA writes one log line when it starts waiting and one when it stops.
At the same kind of start, the corrected build stayed attached, waited, and opened its demo trade once the terminal was ready. Started again in the same trading session, it kept its trade count and took no second entry.
Where OnInit already waits for the connection, as on a restored chart in our runs, these five points cost nothing. This is the start-up handling we add when we make an existing EA production-ready.
Two Tests, Because They Are Two Different Starts
Test the start your EA really gets. The Strategy Tester cannot stand in: a run there begins inside a terminal that is already running. A failed start is one more failure a backtest does not catch.
Test A: the restart path. For every EA.
- On a demo account, attach the EA and let it run.
- Close the terminal completely, then open it again.
- Read the first lines of the Experts tab.
- Repeat with the network off.
- Repeat with a trade open, and check what the EA believes it holds.
Test B: the configuration launch. Only if your terminal is started that way.
- Run that same launch on a demo account, online.
- Run it again with the network off.
In both tests, read three things: whether the EA’s first line appears before or after the terminal connects, whether its start-up lines appear twice, and any of the failed-start lines above.
MetaTrader 5 was not measured for this article; the same two tests apply. Losing state across a restart is a different failure, covered in what an EA forgets when the terminal restarts.
One Rule for the EA, One for Whoever Reads Its Log
For the EA: at start-up, not ready yet means wait and retry. Only a real negative, such as settings that cannot work, is a reason to refuse.
For whoever reads its log: name the start that produced a line before acting on it. We had the first rule right in September and missed the second until this week.
An EA that will run unattended over the holidays is worth both tests before they start.

Leave a Reply