Introduction
Algorithmic trading uses computer code to analyse markets and place orders according to predefined rules. In MT4/MT5, these programs are called Expert Advisors (EAs). Automation can bring consistency and allow rigorous testing, but it introduces its own risks: flawed logic, over-optimisation and technology failures. This lesson introduces algorithmic trading concepts and how to evaluate automated strategies responsibly.
Learning objectives
By the end of this lesson you will be able to:
- Explain what an algorithm, Expert Advisor, indicator and script are in MT4/MT5.
- Convert a discretionary rule into precise, testable conditions.
- Calculate expectancy from backtest statistics in R-multiples and money.
- Explain over-optimisation and the purpose of out-of-sample and forward testing.
- Identify operational risks such as connectivity, VPS failures and broker differences.
Watch the Full Video Lesson
Your video lesson is coming soon. In the meantime, read the complete article and complete the knowledge quiz below.
Step 1: Key components
- Expert Advisor (EA): a program that can place and manage trades automatically.
- Custom indicator: calculates and displays values but does not trade.
- Script: runs once to perform a task (e.g. close all trades).
- Strategy Tester: simulates an EA on historical data.
EAs are written in MQL4 (MT4) or MQL5 (MT5).
Step 2: From idea to rules
A discretionary idea like "buy pullbacks in an uptrend" must become exact rules:
- Trend: close above EMA(200) on H1.
- Pullback: RSI(14) falls below 40 then crosses back above 40.
- Entry: market buy at the next candle open.
- Stop: 1.5 × ATR(14) below entry. Target: 2 × stop distance.
- Risk: 1% of balance per trade. Maximum one open trade.
Key terminology
- Backtest: testing rules on historical data.
- In-sample / out-of-sample: data used to build the strategy vs data kept aside to test it.
- Over-optimisation (curve fitting): tuning settings so closely to past data that they fail in the future.
- Walk-forward analysis: repeatedly optimising on one period and testing on the next.
- VPS: virtual private server that runs the platform continuously.
- R-multiple: result divided by initial risk.
Step 3: Evaluating a backtest (hypothetical)
Results over 200 trades: win rate 44%, average win 1.8R, average loss 1R.
Expectancy formula: Expectancy (R) = (Win rate × Average win) − (Loss rate × Average loss) = (0.44 × 1.8) − (0.56 × 1) = 0.792 − 0.560 = 0.232R per trade
USD account, risk $50 per trade: 0.232 × $50 = $11.60 per trade before costs; over 200 trades ≈ $2,320 before costs.
Costs: if average cost is 0.1R per trade, net expectancy = 0.132R → $6.60 per trade.
GBP account, risk £50: net ≈ 0.132 × £50 = £6.60 per trade.
These are hypothetical backtest figures, not predictions. Live results are often worse than backtests.
Step 4: Avoiding over-optimisation
- Keep rules simple with few parameters.
- Reserve at least 30% of data for out-of-sample testing.
- Test on several pairs and periods.
- Prefer stable results across a range of settings over the single "best" setting.
- Forward test on demo for months before considering live use.
Step 5: Advantages
- Consistent execution with no emotional deviation.
- Can monitor many markets continuously.
- Rules can be tested on large samples.
Step 6: Disadvantages and risks
- Programming errors can place unintended trades.
- Past performance may not repeat; market conditions change.
- Connectivity, power or VPS failures.
- Differences between broker data, spreads and execution.
- Purchased EAs and "guaranteed" robots are frequently misleading or scams.
MT4/MT5 demonstration
Open View → Strategy Tester (Ctrl+R). Select an EA (e.g. the built-in "Moving Average" sample), symbol, timeframe and date range. Choose Every tick or Every tick based on real ticks (MT5) for greater accuracy. Review the Report tab: total trades, profit factor, maximum drawdown. Enable AutoTrading/Algo Trading only on a demo account while learning.
Common beginner mistakes
- Buying EAs advertised with unrealistic returns.
- Running an untested EA on a live account.
- Optimising until the backtest equity curve looks perfect.
- Ignoring spreads, commissions and slippage in tests.
- Leaving an EA unmonitored.
Risk-management guidance
Build risk limits into the code (maximum lot size, daily loss limit, maximum open trades), test thoroughly out-of-sample and on demo, and monitor automated systems daily. Automation does not remove risk.
Practical student exercise
1. Write one trading idea as precise, testable rules. 2. Run the sample Moving Average EA in the Strategy Tester on two different periods and compare results. 3. Calculate expectancy in R from the report's win rate and average win/loss.
Summary
Algorithmic trading turns precise rules into code that can be tested and executed automatically. It offers consistency and testing power but carries risks of over-optimisation, coding errors and technical failure. Evaluate strategies with expectancy, out-of-sample and forward testing, and monitor them closely.
