An Expert Advisor (EA) is a program that runs inside MetaTrader 5 and can analyse the market, open, manage and close trades automatically. EAs are written in MQL5, a language similar to C++. They range from simple helpers that manage your stops to fully automated systems.
What EAs are good for
- Trade management: moving stops to breakeven, trailing them, taking partial profits exactly as your plan says.
- Risk control: calculating position size from your stop, or closing everything at your daily loss limit.
- Fully mechanical strategies: rules precise enough to code completely.
- Testing: MT5's Strategy Tester runs an EA over historical data, including with real tick data.
The shape of an EA
Every EA is built around event functions. OnInit runs once at start-up, OnTick runs on every price update, and OnDeinit runs when the EA is removed. This skeleton shows the structure, including risk-based position sizing; the entry rule is left for you to write.
Expert Advisor skeleton (MQL5)
#include <Trade\Trade.mqh>
input double RiskPercent = 0.5; // risk per trade, % of balance
input int StopPoints = 200; // stop distance in points
CTrade trade;
int OnInit()
{
return(INIT_SUCCEEDED); // runs once when the EA starts
}
void OnTick()
{
// runs on every price update
if(PositionsTotal() > 0)
return; // one position at a time
if(!EntrySignal())
return;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double stop = ask - StopPoints * _Point;
double lots = LotsForRisk(StopPoints);
trade.Buy(lots, _Symbol, ask, stop, 0.0, "academy-example");
}
bool EntrySignal()
{
// your rules go here, e.g. a sweep and a structure shift
return(false);
}
double LotsForRisk(int stopPoints)
{
double risk = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100.0;
double tickVal = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double perLot = stopPoints * _Point / tickSize * tickVal;
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
return(MathFloor(risk / perLot / step) * step);
}From code to live
- Write and compile the EA in MetaEditor (press F4 in MT5).
- Test it in the Strategy Tester on several years of data, including costs.
- Run it on a demo account for weeks, comparing live behaviour with the test.
- Go live on small size, ideally on a VPS (a remote server) so it runs when your computer is off.
| Risk | What can go wrong | Defence |
|---|---|---|
| Bugs | A typo opens ten positions instead of one | Position limits in code; test on demo first |
| Over-optimisation | Parameters tuned to past data | Out-of-sample testing, few parameters |
| Connectivity | The terminal disconnects mid-trade | VPS, and stops placed on the broker's server |
| Bought EAs | Marketing results that don't hold up | Only trust results you can verify yourself |
| Prop firm rules | Some firms restrict or ban certain EAs | Check the firm's automation policy |
Key takeaways
- Expert Advisors are MQL5 programs that can trade and manage positions automatically in MetaTrader 5.
- They're especially useful for trade management and risk control, not just full automation.
- Every EA is built around event functions such as OnInit and OnTick.
- Test in the Strategy Tester, then demo, then small live size on a VPS; check prop firm rules.