
What you’ll learn
- How to tell whether a strategy is better automated or left discretionary
- The main backtesting traps: look-ahead bias, survivorship and over-fitting
- Walk-forward analysis and testing outside the sample
- The path from backtest to demo to the smallest live size
- How to structure an algorithm’s decision flow
- A cost-inclusive backtest worked step by step
- A walk-forward example with real arithmetic
- Simple stress tests beyond a single backtest
- Platform considerations for running code on TabTrade
What automates well
Automation works best when a rule is objective, frequent and fast. Trend-following that acts on clean signals, mean-reversion inside defined ranges, and execution algorithms built to split a large order are all sensible candidates.
TabTrade’s Algo & Automated Trading page covers the platform side of running these rules; this lesson covers the rules themselves.
Discretionary judgement is a poor fit. When your edge depends on interpreting the wording of a central bank statement, a script cannot do the reading for you.
A good test: if you can write the rule down so precisely that two people reading it would take exactly the same action, it can probably be coded. If the rule contains words like “when momentum feels exhausted”, it belongs in a discretionary journal, not an EA.
The anatomy of a trading algorithm
Most automated systems follow the same four-stage loop. Understanding the loop makes debugging easier when live behaviour deviates from the backtest.
The four-stage loop
- Signal logic computes the conditions that trigger a trade. It might be a moving-average cross, an RSI threshold or a price breakout.
- Risk gate checks position size, maximum exposure, correlation limits and whether the session is open. A trade that passes the signal but fails the risk gate is discarded.
- Order manager translates the decision into a working order, handling partial fills, rejections and timeouts.
- Position update records the fill, sets stop-loss and take-profit, and feeds the new state back into the risk gate for the next tick.
When a backtest shows a perfect equity curve but live trading disappoints, the gap often lives inside the risk gate or the order manager, because those are the parts a naive backtest skips.
Backtesting honestly
Most backtests look better than they should, and the reasons are well known:
- Look-ahead bias means taking a decision at the candle’s close when the algorithm would have had to act at the open.
- Survivorship bias comes from testing only on instruments that survive to the present.
- Over-fitting means adjusting parameters until the historical equity curve becomes beautiful. A curve built from noise has no predictive power.
- Ignoring costs lets spread, commission, swap and slippage combine to turn a system that looks profitable into a losing one.
Build the test with realistic tick data, treat the spread as something that changes rather than a constant, and always include slippage.
Cost modelling like this starts from the measured spread on EUR/USD.
A worked example: the drag of trading costs
Take a simple EURUSD mean-reversion system that produces 400 trades over two years of backtest data. Without costs, the raw results show:
| Item | Amount |
|---|---|
| Gross profit | $4,200 |
| Gross loss | $3,000 |
| Net profit (no costs) | $1,200 |
Now layer in realistic costs. Assume the Edge account on MetaTrader 5 with a round-turn commission of $7 per lot and an average spread of 0.03 pips. For a 1-lot trade on EURUSD, 0.03 pips is $0.30. Slippage of 0.1 pip per side is 0.2 pip round turn, or $2 per standard lot. Overnight swap on a small net-long bias adds another $180 across the two years.
| Cost source | Per trade | Total (400 trades) |
|---|---|---|
| Commission (round turn) | $7.00 | $2,800 |
| Spread (0.03 pips × $10/pip) | $0.30 | $120 |
| Slippage (0.1 pip per side, 0.2 pip round turn) | $2.00 | $800 |
| Swap (aggregate) | - | $180 |
| Total costs | $3,900 |
The net result flips from a $1,200 profit to a $2,700 loss. That is a common outcome when a backtest ignores the full cost stack. Always run the numbers with every cost you would pay live before you judge a system’s edge.
Walk-forward analysis
Divide the historical data into segments. Optimise on the first segment, then test on the second. Roll the window forward and do it again. Once the cycle is done, look only at the out-of-sample results when deciding whether the system is worth anything.
Strong in-sample numbers with flat out-of-sample numbers mean the model has been curve-fitted. That is the usual result on a first attempt, and exposing it is exactly the point of the test.
Walk-forward example with numbers
Suppose you have five years of daily EURUSD data. Split it into five one-year windows. Optimise the strategy’s two moving-average lengths on 2020 data, then test those fixed lengths on 2021. Next, optimise on 2021 and test on 2022, and so on. After the four forward tests, you have only out-of-sample trades.
| Period | In-sample profit factor | Out-of-sample profit factor |
|---|---|---|
| 2020 → 2021 | 1.65 | 1.12 |
| 2021 → 2022 | 1.58 | 1.08 |
| 2022 → 2023 | 1.72 | 0.96 |
| 2023 → 2024 | 1.61 | 1.04 |
The in-sample numbers promise a solid system. The out-of-sample numbers say otherwise. The average out-of-sample profit factor is barely above 1.05, and one year lost money. A trader who skipped walk-forward testing would have deployed the system on the strength of the 1.65 backtest and been disappointed. The walk-forward exposes the truth before capital is at risk.
Metrics worth watching
| Metric | Read it as |
|---|---|
| Profit factor | Gross wins divided by gross losses. Anything below 1.2 is fragile. |
| Max drawdown | The worst peak-to-trough fall. Would you actually be able to sit through it? |
| Sharpe ratio | Return measured per unit of volatility |
| Expectancy | Average R per trade: the figure that compounds |
| Trade count | Any count below ~200 leaves conclusions weak |
Expectancy deserves a quick calculation. If your system wins 40% of trades, average win is 2R and average loss is 1R, then expectancy = (0.4 × 2) + (0.6 × -1) = 0.8 - 0.6 = +0.2R per trade. Over 300 trades, that compounds into 60R of profit, less costs. The arithmetic is simple, but it forces you to confront whether the edge is large enough to survive real-world friction.
Beyond the backtest: stress-testing the system
A single backtest, even an honest one, is just one path through history. A few simple stress tests reveal whether the system depends on a lucky run.
Monte Carlo simulation reshuffles the sequence of trades thousands of times. If the original backtest produced a 1.4 profit factor but 30% of the reshuffled sequences lose money, the system is fragile. A trader who only looks at the original curve never sees that risk.
Parameter sensitivity varies each input slightly. If moving the stop-loss from 20 pips to 25 pips cuts the profit factor in half, the system is too sensitive to a single number. A stable system should survive small changes without collapsing.
Out-of-market testing runs the same logic on a different instrument from the same asset class. If a system built on EURUSD shows a similar, though not identical, character on GBPUSD, the logic is not tied to one instrument. If it fails completely, the original result may be a fluke.
These tests do not guarantee future performance. They show whether a result survives conditions the original backtest did not cover.
The deployment path
Systems that reach live trading tend to pass through the same five stages, in this order:
- Backtest, run with realistic costs included
- Walk-forward test, to confirm the system holds up
- Demo forward test, for at least a month, since it exposes execution bugs a backtest cannot
- Live start at the smallest available size
- Size increase only once live results stay in line with the forward test
Of the five, the demo forward test is the stage whose omission costs a systematic trader the most, because the problems it catches leave no trace in a backtest.
What the demo forward test uncovers
A backtest assumes every order fills at the requested price, that the platform never disconnects, and that swap rates stay constant. A demo forward test on a live server exposes:
- Execution delays between signal and fill that eat into small edges
- Order rejections when the price has moved past a pending order’s level
- Swap charges that differ from the backtest estimate, especially around rollover
- Platform quirks such as how stop-losses are handled during news gaps
Run the demo test on the same account type and server you intend to use live. If you plan to use an Edge account on MetaTrader 5, test on that. The closer the test environment to live conditions, the fewer surprises when real money is on the line.
Automation on TabTrade’s infrastructure
TabTrade’s MetaTrader 5 platform supports fully automated trading through Expert Advisors (EAs) written in MQL5. The built-in Strategy Tester allows backtesting with tick data, visual mode, and the ability to include the commission and spread structure of the account type you select. When you move to forward testing, a VPS service is available so your EA can run 24 hours a day without needing your home computer to stay on.
The same cost structure you used in the worked example earlier comes directly from the published Edge account figures: $3.50 per lot per side on FX and metals, with average spreads as low as 0.02 pips on AUDUSD and 0.03 pips on EURUSD. These numbers are not theoretical. They are what you plug into the backtest to see whether your system survives real costs.
If you are coding an EA for the first time, start with a simple moving-average cross on a demo account. Get the order-management logic right before you add filters, time restrictions or multiple timeframes. A plain system that executes cleanly will teach you more than a complicated one that throws errors at 3 a.m.