Home › Guides › Automation › How an EA works
How an EA works under the hood
Knowing what an EA does from one tick to the next removes a lot of the fear and solves half the problems. An EA is not a black box: it is a program that the terminal notifies when something happens. Here is the complete path, from attaching it to the chart to sending an order, with the official references so you can verify it.
The lifecycle: which events it responds to
MetaTrader 5 calls a series of expert functions depending on what happens in the terminal. Almost all the work of an EA lives in OnTick (official documentation).
| Function | When it runs | What it is used for |
|---|---|---|
OnInit() | Once, when the EA is loaded onto the chart | Prepare variables, validate parameters, create indicator handles |
OnTick() | Every time a new tick for the symbol arrives | The heart: read data, decide and, if it is time to, send orders |
OnTimer() | At the interval set with EventSetTimer() | Periodic tasks: refresh a panel, check schedules, publish data |
OnTradeTransaction() | When something changes in trades or positions | React to executions, partial closes or cancellations |
OnDeinit() | When the EA is unloaded from the chart | Clean up objects, release resources, leave a trace |
OnTick. An EA that is very active on M1 can
spend a long time without running on an index with spaced-out ticks, and that is not a bug: it is how the
terminal works.
What it looks at on each tick
Before deciding anything, an EA reads from the terminal. All the information comes from query functions (symbol specification and time series):
- Current price and the symbol's spread, as well as the tick value
(
SymbolInfoDoublewithSYMBOL_TRADE_TICK_VALUE), which is essential for calculating the lot size based on risk. - Candles from historical data, for any structure calculation (CopyRates) or indicator values through their handles.
- Account status: balance, equity, free margin, leverage.
- Open positions and orders, filtering by the expert's identifier (magic number) so as not to touch what isn't theirs.
How a trade is sent
To trade, the expert builds a request and sends it to the server with
OrderSend() (official documentation).
The request carries the symbol, volume, price, stop, target and the magic number. The server can
accept or reject it, and that is where most surprises lie:
- Minimum stops: each symbol has a minimum distance between price and stop. If the expert proposes a shorter stop, the server rejects the order.
- Volume: there is a minimum, a maximum and a step (for example 0,01). A calculated lot smaller than the minimum cannot be sent.
- Execution mode: the symbol supports certain fill modes; using an unsupported one produces an error and the expert must retry with another.
- Margin: if there is not enough free margin for that volume, the order will not go through even if there is a signal.
The logic, in four lines
Stripped of frills, almost every risk management expert does this on every tick. Seeing it written down helps to understand why the order of the steps matters:
2 · Is my entry condition met and am I not outside trading hours? → continue.
3 · Calculate the lot size using risk and distance to the stop. If it comes out below the minimum, DO NOT trade.
4 · Send the order with stop and target; if the server rejects it, log the reason and do not keep retrying in a loop.
Step 3 is what separates a risk-controlled bot from an account-burning machine. An expert that sends “0,10 lots” for no reason is ignoring the one thing that really should be automated: position size.
Why it sometimes ‘does nothing’
Before suspecting the strategy, rule out these causes. In our experience, most queries about ‘my EA doesn't trade’ are resolved here:
| Symptom | Common cause |
|---|---|
| The smiley face does not appear on the chart | The button Algorithmic trading is disabled, or the expert doesn't have permission to trade on the ‘Common’ tab |
| Smiley face but zero trades | Time filter, spread above the maximum allowed or entry condition not met |
| Numeric errors in the log | Volume below the minimum, stop shorter than the symbol minimum or insufficient margin |
| Indicators with strange values or the expert calculates nothing | Symbol history not downloaded: the terminal does not yet have enough candles |
| It is trading on demo and not live (or vice versa) | Permissions on the expert's “Common” tab |
How to check that it does what you think
- “Experts” tab of the terminal: messages from
Print, errors when sending and warnings. - Strategy Tester in visual mode: you see it trading on the chart, entry by entry, with the list of trades alongside.
- Demo account with the same broker over a period that includes bad days: that is where the real rejections, slippage and the spread for each hour appear.
- Results report in R, not in money: to compare the system with yourself when the size changes (how it is measured).
Frequently asked questions
What is OnTick and how often does it run?
It is the function called every time a new tick arrives for the chart symbol. It does not run at fixed intervals: it depends on the real tick frequency of the instrument, which on synthetics can be several per second or one every few seconds depending on the index.
Why does my EA do nothing even though the market is open?
Almost always for reasons unrelated to the strategy: “Algorithmic trading” disabled, symbol with no history, lot below the minimum, stop below the minimum allowed, spread above the filter, a position already open, or the instrument's daily pause. The Experts log says which one.
Can an EA trade several symbols at once?
Yes. An expert only receives ticks from the chart it is on, so two approaches are used: query the other symbols from a single chart, or open an instance on each chart with a magic number different one so that each manages only its positions.
How do I know what my EA is doing at any given moment?
With Print to the “Experts” tab, with the Strategy Tester in visual mode and with the terminal's
“Trades” tab. An expert that leaves no trail is impossible to audit.
Does it work the same live as in the tester?
No. The tester reconstructs the past with a simplified execution model: with no real latency, with approximate slippage and with a spread that does not reflect every hour of the day. That is why it is also tested on demo before risking money.
Understanding what it executes, before letting it loose
At the academy, what the expert does and why is reviewed first, and only then is it allowed to trade. If you cannot explain a trade in the log, the problem is not the market.
Keep reading
- EAs and bots for MetaTrader 5What can really be automated and what cannot, before programming anything.
- Installing an EA in MetaTrader 5 step by stepWhere the file goes, how to activate it and the typical errors.
- Copy trading with MT5What is copied, what is not, and how size is adjusted in each account.
- Martingale on synthetic indicesThe mistake that is most often automated unintentionally: doubling after a loss.
- All guidesComplete content index: synthetics, trading, automation, forex and gold.