# EMA 9/20 crossover on XAUUSD, 15-minute and 1-hour bars - frozen specification (RIP Backtest protocol v1.0 plus the 8 Oct 2026 rules)

Specification written and **frozen 2026-10-08 (approved by the owner the same evening), before any EMA 9/20 value, signal or trade was computed on gold.** Any later change of a rule is documented and treated as a separate test; bugs may be fixed.

## What was looked at before, and why this test was chosen
- The earlier tests on this same gold series: daily golden cross (4 gold variants), 1-hour golden cross (12 gold variants; one survivor, long only with no stop), 30-minute opening-range breakout (12 variants), and unprotocolled tests of other indicators on XAUUSD H1 (no edge). At least 28 protocolled variants were already tried on this series.
- The EMA 9/21 test on ES, NQ and GC futures (same day): gold long only with no stop came closest.
- **Selection bias, stated plainly:** this test was chosen after seeing that trend-following rules did best on gold. That is allowed but it weakens any positive result, and the article will say so.
- No EMA 9/20 number on gold was looked at.

## What is tested
The **EMA 9 crossing EMA 20** on **XAUUSD**, on **15-minute (M15) and 1-hour (H1) bars**, all sessions, long only and long and short, with and without a stop and targets. One article, two timeframes. Other pairs, timeframes and filters are not tested here and will not be tried after seeing the result.

## Instrument and data
- **Instrument:** XAUUSD on the IC Markets MetaTrader 4 demo server (ICMarketsSC-Demo06): a spot gold CFD quoted in US dollars per ounce, not a futures contract. Results are per ounce.
- **Data:** the terminal's 1-minute history (`XAUUSD1.hst`), bid prices, **from the first bar of 2004 to 2026-10-07 16:57 New York**: the same part of the file as the published gold studies (SHA-256 of that part in `mt4_prefix_hashes.json`). The file keeps growing; the loader cuts it at that bar. (Correction before the run: the draft said 17:00; the published studies read the file when its last bar was 16:57, and the minutes 16:58 and 16:59 were appended later.)
- **Time zone:** the server clock is New York + 7 hours all year (it follows US daylight saving), so New York time = server time - 7 hours, and bar time = close time. **Checked before the run from the data:** the weekly open and the daily break must sit at the same New York time on both sides of every US and European clock change; if they do not, the run stops and the conversion is fixed first.
- **Bars:** M15 bars close at :00, :15, :30, :45 New York time and contain the 1-minute bars with close times in (t - 15 min, t]; H1 bars close on the hour, (t - 60 min, t]. A bar is built from the minutes that exist; **incomplete bars are kept as they are** (no filling, no synthetic prices). Positions are held over weekends.

## Data audit (before the run)
The gaps inside days were never audited for this file. Before anything is run, every gap between consecutive 1-minute bars longer than 30 minutes is listed and classed:
- **normal close:** the daily break around 17:00-18:00 New York, the weekend (Friday close to Sunday open), and days with no trading on the broker's holiday calendar (Christmas, New Year) where the gap starts and ends at the usual times;
- **suspect:** a gap inside a session, a late open or an early end at an unusual time, or a weekday with no or almost no bars (the 34 whole-day holes already known are in this class);
- **unclear:** anything the rules above cannot place. With no second gold source for 2004-2019, unclear gaps are reported as unclear, not as normal.
The list is published (`data_holes_xau.json`), with the share of minutes present per year (the early years are expected to be thin). **Treatment:** prices are never filled; the EMAs continue across a gap as across a weekend; a position stays open across it and its stop and target are checked from the first bar after the gap (a level gapped through fills at that bar's open). **Rule 3:** trades open across a suspect or unclear gap are counted, and every gate and verdict is recomputed without them (gate 4 from the run); a changed verdict becomes Inconclusive.

## Rules
- **Averages:** EMA(n) of the bar closes, seeded with the simple average of the first n closes, then ema[k] = a x close[k] + (1 - a) x ema[k-1], a = 2 / (n + 1).
- **Signals** at the close of bar k (the bar is complete only when its close time has passed): golden if EMA9[k] > EMA20[k] and EMA9[k-1] <= EMA20[k-1]; death if EMA9[k] < EMA20[k] and EMA9[k-1] >= EMA20[k-1]; differences below 1e-10 count as exact ties.
- **Execution:** at the open of the next available 1-minute bar after the signal bar (after a gap, the first bar after it). A golden cross opens a long; in the long-short versions a death cross opens a short. After a stop or target the position stays flat until the next signal.
- **Exit:** the opposite signal, executed the same way (in long-short it also opens the new position). A position open on the last bar is closed at its close and flagged.
- **R = 1.5 x ATR(14)** of the bars of that timeframe at the signal bar. Stop 1 R; targets 1, 2, 3 R from the fill. Inside each 1-minute bar: **if the stop and the target are both touched, the stop is taken**; the stop can be hit in the entry minute itself; a level gapped through fills at the open of that minute.
- **24 variants (the only ones tested):** 2 timeframes (M15, H1) x long only (L) and long and short (LS) x six exits: **N** (no stop, no target), **S** (stop 1 R only), **T1, T2, T3** (stop and one target), **T123** (a third at each target, the exact average of T1, T2, T3). Named `GX-M15-L-N` ... `GX-H1-LS-T123`.

## Costs
- **Baseline:** $0.30 per ounce per round trip for spread and commission, as in every gold test on the site; **slippage zero**, which is optimistic and stated as such. No financing or swap in the baseline (comparable with the earlier gold tests).
- **Sensitivity A, 3% a year:** 3% a year of the entry price, per calendar day held, charged to longs and shorts alike (as in the 1-hour golden cross test).
- **Sensitivity B, "scenario based on the current IC Markets swap":** the rates of the XAUUSD contract specification shown in the terminal on **2026-10-08 19:08 (Bucharest time)**: swap type **in points**, **swap long -56.282, swap short +39.136**, triple swap on **Wednesday**; digits 2 (1 point = $0.01), contract size 100 oz. Per ounce and per night: long **-$0.56282** (a charge), short **+$0.39136** (a credit). One rollover at each 17:00 New York (00:00 server time) from Monday to Friday that a position is held through; the Wednesday rollover counts three times (it covers the weekend), so a full week costs seven nights. Broker holiday adjustments are not modelled. Each trade's swap in dollars per ounce is divided by the trade's R. These are today's rates applied to 22 years (at about $4,100 an ounce they are roughly -5% a year for longs and +3.5% for shorts, far from the rates of 2004-2021): **not a historical swap**.
- For the baseline and each scenario: net R, development and full-sample t-statistics, and whether any gate or verdict changes. The verdict is that of the baseline; the scenarios are reported next to it.

## Periods and data use
- **Periods (by entry date):** development 2004 .. 2012; validation 2013 .. 2019; holdout 2020 .. 2026-10-07 16:57.
- **None of these periods is fresh.** All three were used by the earlier gold tests listed above, so the holdout is not a new holdout and is reported as "already used". The only fresh data is after 2026-10-09 (kept for the forward test).

## Gates and verdicts
Six gates per variant: validation profit factor >= 1.2; at least 50 trades in each period; development t >= **2.5** (24 variants are tried); above the 95th percentile of 500 runs of the matched control; positive without the 10 best trades; holdout net positive (flagged used). Fewer than 30 trades: Inconclusive; otherwise Buried or Survivor.

## Matched control
Circularly shifted state-series control: the state is +1 while EMA9 is above EMA20 and -1 while below (previous state kept on a tie); the real state series of each timeframe is shifted by a random offset of at least 5% of its length and applied to the real prices with the same rules and costs, R recomputed at the shifted signal time. 500 runs, seeds 1000 + run, the same shifts for every variant of a timeframe. Statistic: average net R per trade.

## Engine change and self-test (before the run)
The shared engine gets one additive option: bars of another length than one hour. Then:
1. **Reproduction:** the published 1-hour golden cross on gold is re-run with the changed engine on the same part of the file; its summary and trade lists must be identical to the published ones. If not, nothing else is run.
2. The engine's own self-test passes again.
3. The new self-test: a hand-worked example of the EMA and the signals; hand-built cases of a stop and a target in the same minute (stop taken), a stop in the entry minute, a level gapped through, and a signal before a gap executed at the first bar after it; the M15 bars checked against a naive aggregation; the engine's EMA9, EMA20 and signals matched to a naive recomputation on the real bars of both timeframes; an independent re-implementation of the trade simulation matched trade by trade for all 24 variants; the shifted-state control with shift 0 reproducing the real signals. No result is printed.

## Before the article
The results go to the owner first: every variant, drawdown on closed trades and including open positions, the hole sensitivity, and the three cost scenarios. The article is built only after that.

## Always reported
Win rate with its 95% Wilson interval, how trades ended, average hours held, long and short separately, gross and cost per trade in R, drawdown on closed trades and including open losses, buy and hold in the same dollars, the cost scenarios, and the sign of every result in R and dollars.
