What Is an Expert Advisor?
OnInit, OnTick, OnDeinit — the lifecycle of algorithmic strategies.
Written and reviewed by the Fundezy team · how we produce this
An Expert Advisor (EA) is a trading strategy written as code. Instead of watching a chart and clicking, you describe exactly when to enter, exit and size — and the machine executes it without fatigue, fear or FOMO (Course 04's entire curriculum, deleted by construction).
The three-event lifecycle
Every EA on this platform (whose language borrows from the classic retail trading platforms) is organized around three functions:
OnInit() runs once when the EA starts
OnTick() runs on every price tick — the trading brain
OnDeinit() runs once when the EA stops (cleanup/logging)
State that survives between ticks — counters, flags, last-entry-price — lives in global variables declared outside the functions. Input parameters (input in the EA dialect) are the knobs you can tune without editing logic:
input int FastMAPeriod = 10; // tunable from the tester UI
input int SlowMAPeriod = 30;
input double LotSize = 0.01;
int OnInit() { return(INIT_SUCCEEDED); } // 0 = OK
void OnDeinit(const int reason) { }
void OnTick() { /* called for every tick */ }
What your EA can call
The platform's sandbox provides a focused function set:
| Family | Functions |
|---|---|
| Price series | iClose, iOpen, iHigh, iLow, iTime, iVolume, iBars — each takes (symbol, timeframe, shift), where shift 0 = current bar, 1 = previous |
| Indicators | iMA, iRSI, iBands, iMACD, iStochastic, iATR, iADX, iSAR, iIchimoku |
| Account | AccountBalance, AccountEquity, AccountFreeMargin |
| Trading | OrderSend(...), OrderClose(ticket) |
| Utilities | Print, Symbol, Period, TimeCurrent, NormalizeDouble, MathMin/Max/Abs... |
Constants like OP_BUY (0), OP_SELL (1), MODE_SMA (0), PRICE_CLOSE (0) mirror the classic retail-platform originals.
How execution actually runs
Your EA runs in your browser, inside an isolated Web Worker the platform spins up when you press Start — on your device, not a server. The worker parses the source, seeds inputs/globals, and calls OnTick against each new simulated tick (roughly once a minute in live mode, once per bar in the Strategy Tester). Every order the EA places goes through the same server-validated route as your manual trades — the server never executes your code. Three properties to know:
- Real sandbox isolation: the worker can't touch the page, the network, or anyone else's account — and it dies with the tab. Reopen the app and it reattaches with your EA's saved variables.
- Keep
OnTickcheap: the worker is single-threaded. A hungOnTickblocks later ticks and any queued orders (they only send afterOnTickreturns) until you press Stop. Compute, decide, return. - Deterministic ticks: in backtests, the same code and data produce the same result — that reproducibility is the entire scientific value of the tool.
Why write EAs at all?
The deepest reason isn't automation — it's falsifiability. A vague idea ("I trade pullbacks in trends") can't be wrong. An EA must answer: pullback to what, defined how, sized by what formula, exited where? Once written, it can be run over years of real data and graded. The next four lessons take you from idea to evidence.
By the numbers
The market these programs trade is enormous and almost entirely electronic: the 2022 BIS survey put daily FX turnover at $7.5 trillion, most of it machines talking to machines. The EA contract is three functions — OnInit, OnTick, OnDeinit — and on this platform OnTick fires about once a minute against the simulated feed, so an hour-long session is roughly sixty decisions.
Check your understanding
4 questions · answer all, then submit.