What this lesson adds
The backtesting a gold strategy lesson explains the principles: what to measure, which biases to avoid, how to split data. This lesson is the hands-on method: where to get free data, how to prepare it, how to turn rules into something testable, how to model costs, and how to read the result. If you have never run a backtest, start here and read the principles lesson alongside.
The method has seven steps. Each has a deliverable, and skipping one is where most backtests go wrong.
Step 1: write rules a machine could follow
A strategy is testable only if two people reading the rules would take identical trades. Write every rule as a condition on data you will have in the file.
| Vague rule | Testable rule |
|---|---|
| “Buy pullbacks in an uptrend” | “On H1, if close > 50 EMA and the previous candle’s low < 20 EMA and this candle closes above its open, buy at the next open” |
| “Stop below support” | “Stop = lowest low of the previous 5 H1 candles minus $1.00” |
| “Take profit at resistance” | “Target = entry + 2 x (entry minus stop)” |
| “Avoid news” | “No entries between 08:15 and 08:45 New York time on days flagged in the events file” |
| “Only trade London and New York” | “Entries only when the candle’s New York time is between 02:00 and 11:30” |
Deliverable: a one-page rules document with every number specified, written before you look at the data.
Step 2: get and clean the free data
Export candles from your platform or download from a free provider. For most intraday gold strategies you want M15 or H1 candles for at least two years; for swing strategies, D1 for ten years or more. Then clean:
- Check the time zone. Platform exports are usually in the broker’s server time. Convert to New York time and note whether the data applies daylight saving. Session rules fail silently if this is wrong.
- Find gaps. Count the candles per day. Missing hours are common in free intraday data; decide whether to drop those days or fill forward, and record the choice.
- Check for spikes. A single candle with a high $200 above its neighbours is almost always bad data. Remove or flag it.
- Confirm the price basis. Spot XAUUSD from a broker, a futures continuous contract and a daily London benchmark are different series. Do not mix them.
- Record provenance. Source, download date, time zone, number of candles, gaps found. This goes at the top of the results file.
Deliverable: a clean CSV with columns for time (New York), open, high, low, close, plus your gap and spike log.
Step 3: split the data before you look
Divide the clean data into in-sample (roughly the first 60% to 70%) and out-of-sample (the rest). Develop and adjust only on in-sample. Open out-of-sample once, at the end, and accept what it says. If you peek, you no longer have an out-of-sample set. The forward test on a demo account afterwards is the third and final check.
Deliverable: two files, with the out-of-sample file left unopened.
Step 4: choose a tool and code the rules
Three options, in rising order of power and effort:
- Spreadsheet. Fine for D1 and H1 strategies with simple rules. Each row is a candle; columns compute the indicators, the signal, the entry price, the stop, the target and the outcome. Transparent and easy to audit. Slow beyond a few thousand rows.
- Platform strategy tester. Convenient, handles intrabar logic, often has a visual mode. Limits: cost modelling is sometimes crude, the data is the platform’s own, and the code lives in a proprietary language.
- A script in a general-purpose language. The most flexible and the easiest to run at several cost levels and parameter settings. Needs some programming, but a rules-based gold strategy on candles is a few dozen lines.
Whichever you pick, test the tester. Hand-check the first ten trades against the chart. Confirm that entries happen at the next candle’s open, not at the signal candle’s close, and that the stop and target are evaluated against the candle’s high and low, with a rule for the case where both are hit in one candle (assume the stop, which is the conservative choice).
Deliverable: a trade list with entry time, entry price, stop, target, exit time, exit price and result in dollars and in R-multiples.
Step 5: model the costs honestly
Every trade pays spread, usually some slippage, and swap if held overnight. Build these in:
- Spread: subtract a round-trip spread from every trade. Use your broker’s typical spread in the hours the strategy trades, not the minimum advertised.
- Slippage: add a small amount to stop exits, more for stops during 08:30 to 09:00 New York time.
- Swap: for trades held past 17:00 New York time, subtract the nightly swap, triple on Wednesdays. The gold swap calculator gives current figures.
- Run three cost scenarios: optimistic, realistic, pessimistic. The realistic one is your result. If the strategy only survives the optimistic one, stop here.
Deliverable: three versions of the results, with the cost assumptions written at the top of each.
Step 6: read the result as a whole
Look at the equity curve before the summary statistics. Then compute:
| Metric | What it tells you | Warning signs |
|---|---|---|
| Number of trades | Whether the sample means anything | Under about 100 |
| Expectancy per trade in R | Average outcome per unit of risk after costs | Below about 0.1R; or positive only in the optimistic cost case |
| Profit factor | Gross wins divided by gross losses | Below about 1.2 after costs |
| Maximum drawdown | The worst peak-to-trough fall in equity | Larger than you could sit through in real money |
| Longest losing streak | How many consecutive losses to expect | Longer than your risk rules survive comfortably |
| Trades per month | Whether the strategy fits your life | Too few to learn from, or too many to execute |
| Result by year and by regime | Whether the edge is spread across conditions or concentrated | Most of the profit in one quarter |
The trading math and expectancy lesson explains each metric and how they interact. Then, and only then, run the rules once on the out-of-sample file and compare. A drop of a third or so in expectancy is normal; a change of sign means the rules were fitted to the past.
Deliverable: a one-page results summary with in-sample and out-of-sample figures side by side.
Step 7: decide, and write it down
Three outcomes:
- Proceed to forward test on demo for at least two months, using the demo account practice plan, and compare live results with the backtest’s expectations.
- Adjust and retest, but only in-sample, and only one change at a time, with the reason recorded. Every adjustment reduces the credibility of the out-of-sample result, so limit yourself to two or three rounds.
- Discard. A clean negative is a good result; it saved you money.
Deliverable: a decision line at the bottom of the results file, dated.
Worked example (hypothetical)
A trader tests a London open breakout rule on two years of free M15 XAUUSD data, converted to New York time, with 14 days dropped for missing candles.
Rules: mark the 19:00 to 02:00 New York range; buy on the first M15 close above the range high between 02:00 and 05:00, or sell on the first close below the range low; stop at the range midpoint; target 1.5x the range height; one trade per day; no entries on FOMC, CPI or NFP days from the events file.
Split: first 16 months in-sample, last 8 months out-of-sample.
In-sample, at a realistic cost of $0.45 round trip plus $0.20 stop slippage:
- 214 trades, 41% winners
- Average win 1.32R, average loss 1.04R (losses exceed 1R because of slippage)
- Expectancy about +0.11R per trade, profit factor about 1.24
- Maximum drawdown 13.8R, longest losing streak 9
At the pessimistic cost of $0.70 plus $0.40 slippage, expectancy falls to about +0.02R: essentially break-even. The edge is thin and cost-sensitive.
Out-of-sample, realistic cost: 108 trades, expectancy about +0.07R, maximum drawdown 11.2R. Same sign, lower magnitude, as expected.
Decision: proceed to a two-month forward test at 0.25% risk per trade, with a note that the strategy is only viable on an account with a tight London-hours spread, and that a spread widening of $0.25 would likely remove the edge. All figures are hypothetical and illustrate the method; they are not a claim about any real strategy’s performance.
Combining it with other concepts
- Backtesting a gold strategy: the principles and biases behind each step.
- Trading math and expectancy: reading the metrics.
- Gold trading journal templates: the forward test record.
- Asian range breakout: the strategy family used in the example.
- Gold volatility tool: checking whether the test period’s volatility resembles today’s.
Best timeframes and sessions to test
| Strategy type | Data needed | Minimum history |
|---|---|---|
| Session and news strategies | M15 or M5 in New York time, plus an events file | Two years |
| Intraday structure strategies | M15 or H1 | Two to three years |
| Swing strategies | H4 and D1 | Ten years or more, covering several regimes |
Gold’s volatility regime changes a lot. A strategy tested only on a period with a $40 ADR may behave very differently at $75; check the gold volatility tool and include both kinds of period if you can.
Common mistakes
- Writing rules after looking at the data. Fix: the rules document comes first, dated.
- Ignoring the time zone. Fix: convert to New York time and verify a known event lands at the right hour.
- Entering at the signal candle’s close. Fix: enter at the next open; you cannot trade a close you have not seen.
- Optimistic costs. Fix: three cost scenarios; the realistic one is the result.
- Peeking at out-of-sample. Fix: one look, at the end, accepted as final.
- Optimising parameters until the curve looks good. Fix: one change at a time, in-sample only, maximum three rounds.
- Trusting a win rate. Fix: expectancy, profit factor, drawdown and streak together.
Limitations and honest caveats
A backtest is a statement about the past under assumptions you chose. Free data has gaps and errors, spreads and slippage in the future will differ from your estimates, and a strategy can stop working the week after a perfect test. No backtest result is a prediction, and no verified statistic exists for how often backtested retail strategies work live. Leveraged trading carries a high risk of loss.
- Free intraday data rarely includes the broker’s actual spread at each moment; your cost model is an estimate.
- Survivorship in your own process is real: you remember the strategies that tested well and forget the dozen that did not.
- The more rules a strategy has, the more easily it fits the past. Prefer fewer, simpler rules.
Pro tips
- Keep a strategy graveyard. A file listing every idea tested and why it failed stops you re-testing the same thing a year later.
- Test the null. Run the same entries with random direction and the same stops and targets. If the result is similar, the entry rule is not doing anything.
- Plot trades by hour of day and by day of week. Concentrations tell you which sessions carry the edge and which dilute it.
- Record the ADR of the test period next to the results. Edges tied to a volatility regime need that context.
- Automate the report. If re-running at a new cost level takes one command, you will actually do it.
Quick checklist
- Are the rules written so that two people would take identical trades?
- Is the data in New York time, cleaned of gaps and spikes, with the source recorded?
- Is out-of-sample data set aside and unopened?
- Have I hand-checked the first ten trades against the chart?
- Have I run three cost scenarios, including swap for overnight holds?
- Have I read the equity curve, expectancy, profit factor, drawdown and streak together?
- Does the out-of-sample result have the same sign?
- Is the decision written down and dated?
Frequently asked questions
Where can I get free historical gold data?
Most trading platforms let you export historical candles for XAUUSD, often several years of H1 and M15 data and more of D1. Several public data providers also offer free daily gold prices. Check the licence, note that free intraday data often has gaps, and record the source and download date in your test notes.
Do I need to know how to code to backtest?
No. A spreadsheet can handle a simple rules-based strategy on daily or hourly candles. Code becomes useful when the rules involve intraday session logic, many parameters, or more than a few thousand candles. Platform strategy testers are a middle ground, with their own limits.
How many trades do I need in a backtest?
Enough that the result is unlikely to be luck. A common rule of thumb is at least 100 trades, and more for strategies with low win rates. Equally important is covering different market regimes: trending, ranging, high and low volatility. No verified statistic says exactly how many trades is enough.
How do I account for spread and slippage in a gold backtest?
Subtract a realistic round-trip cost from every trade. For gold on M15 or H1 that might be $0.30 to $0.60 per ounce in normal hours and more around news. Run the test at two or three cost levels and see how sensitive the result is. If the edge disappears at a plausible cost, it was not an edge.
What is out-of-sample testing?
Splitting the data into a part you use to develop and tune the rules (in-sample) and a part you touch only once at the end (out-of-sample). If the strategy only works in-sample, you have fitted the rules to that data. The existing backtesting lesson covers this in depth.
My backtest shows a 70% win rate. Is that good?
On its own it means nothing. A 70% win rate with an average loss three times the average win loses money. Look at expectancy, the profit factor, the maximum drawdown and the longest losing streak together, and treat any single impressive number with suspicion.
Can I trust a backtest from a strategy tester or EA seller?
Not without the details: data source, costs modelled, in-sample versus out-of-sample periods, and the trade list. The EA and bot checklist on this site lists the questions to ask. A backtest you did not run yourself is a marketing document until proven otherwise.
Check your understanding
Five questions drawn from this lesson. Score 4 of 5 or better to mark it complete and count it toward your XAUUSD Foundations certificate.
You passed this lesson on . Retake it any time; your best result is kept.
Your answers and progress are saved in this browser only (nothing is sent to us). Clearing site data resets them.
Sources and further reading
- CME Group — Gold futures overview — Contract specifications and trading hours for COMEX gold
- CFTC — Commitments of Traders — Weekly futures positioning, including gold
- Investor.gov — Stop order — How stop orders work, and their limits in fast markets
External sites are independent of XAUUSD Global. Check dates and data yourself.