When OnInit runs before MetaTrader 4 is connected: seven cold starts, two kinds of start
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, 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. An EA attached by a start-up configuration file was initialised at once, with no connection and no account.
What we had found, and what we called it
The EA was an opening-range breakout EA with optional entry filters. At a cold start on a demo terminal, three start-up checks had the same flaw:
- The timer. The EA set a one-second timer in OnInit with EventSetTimer. The call failed with error 4030, ERR_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.
We corrected all three so the EA waits and retries. Then we recorded the defect as what happens after a VPS reboot. That label is the part we had never tested.
Seven cold starts with a probe EA
The probe 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. MetaTrader 4 build 1492, one demo terminal, 2 December 2026. One start had both kinds of EA, and one was void: the terminal had not kept the EA on its chart.
| 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 |
In the three online starts from a configuration file, OnInit ran a second time 2.7 to 4.3 seconds after the first. No OnDeinit ran 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, so the refused timer of September stays a single measurement, in that EA and that set-up.
The MQL4 documentation, Program Running, says that without a connection "this may lead to a delay in the start of an Expert Advisor". It does not say which starts wait, and it does not mention a second OnInit with no OnDeinit before it.
Not measured: a restart of Windows itself, a network that comes up late, an EA attached by hand while the terminal is offline, MetaTrader 5, and any other build.
What breaks at an early OnInit
- 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 OnInit meant to run once. It ran twice.
- 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 the probe's name where yours will be:
Reason 8 is REASON_INITFAILED. The terminal reads the return value of OnInit only in programs compiled with #property strict.
What the EA should do at start-up
Hold instead of fail, and make OnInit safe to run twice. 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 OnTick every 10 seconds.
- OnTick waits 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.
Where OnInit already waits for the connection, as on a restored chart in our runs, these five points cost nothing.
Two tests, because they are two different starts
The Strategy Tester cannot stand in: a run there begins inside a terminal that is already running.
Test A, the restart path, for every EA. On a demo account, attach the EA and let it run. Close the terminal completely and open it again. Read the first lines of the Experts tab. Repeat with the network off, and once more 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, and again with the network off.
In both, 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.
The rule we took from it: name the start that produced a log line before acting on it.


