How an Expert Advisor works

By Otto Research 3 min read Article 43 of 95

An Expert Advisor is a program that waits for something to happen, does some arithmetic when it does, and sometimes sends an order to the broker as a result. Everything else is detail. This page gives enough of the detail to understand what an EA can and cannot be doing when it appears to sit quietly on a chart.

What starts it

MetaTrader 5 wakes the EA whenever a new price arrives for the chart's instrument, and, if the EA asks for it, on a timer. Most well-designed EAs ignore the individual prices and act only when a new bar begins on their timeframe, because the decisions they make are based on completed bars. An EA on the four-hour chart therefore does its real work six times a day; the rest of the time it is checking whether anything has changed.

What it reads

The EA reads the price history of the instruments it trades, calculates whatever it needs from it, moving averages, volatility, highs and lows over a period, and reads the state of the account: balance, equity, margin, and the positions it already holds. It identifies its own positions by a number called a magic number, attached to every order it places, so that it does not interfere with trades placed by hand or by another EA on the same account.

What it decides

It compares what it has read with its rules. If the conditions for a trade are met and its own limits allow, it calculates a position size from the stop distance and the risk it intends to take, and sends an order to the broker's server with the stop loss and, usually, the take profit attached. The server confirms the fill or returns an error, and the EA records what happened. For existing positions it applies whatever management its rules contain: moving a stop to breakeven, trailing it, or closing at a time limit.

Where it keeps its state

An EA holds its working memory in the platform. When the platform restarts, that memory is gone, and the EA rebuilds what it needs from the account's open positions and the price history. This is why an EA that keeps its stops in memory rather than at the broker is fragile: a restart leaves nothing behind. It is also why an EA is usually identified as the source of a position only by the magic number, which survives restarts because it is stored with the order.

Worked example

At 8am a new four-hour bar opens on the DAX. The EA wakes, sees the new bar, reads the last few hundred bars of history, and calculates that the previous bar closed at a fresh short-term low while the index remains above its long-term average. Its rules say buy. It reads the account balance, applies its risk setting, works out a stop 180 points below, and calculates 0.11 lots. It sends a buy order with the stop and a target attached and a magic number identifying the strategy. The broker fills it at 18,204. The EA logs the trade and goes back to waiting. At noon it wakes again, sees nothing to do, and waits until 4pm.

What this means when an EA is trading

This sequence is visible in the Experts tab of the Toolbox, where a properly written EA logs each step. When an EA is doing nothing, the log shows it checking and finding no signal. When it places a trade, the log shows the calculation and the broker's response. When something goes wrong, the log shows the error code. Reading the log is the difference between wondering what an EA is doing and knowing.

From theory to live

See it put to work.

The concepts on these pages are what Otto is built on. Watch it trade them live, on real accounts.