Quant notes · private sheet · backtest.py

Backtester structure sheet

The five functions in the positions of the structure diagram, each with its code and the output it produces on the six-day example (closes 100, 102, 101, 104, 103, 105; signal 0, +1, +1, −1, −1, +1; no carry; no volatility target). Docstrings are omitted from the code; line numbers are those of the file. Dashed arrows: a function call, caller to callee. Solid arrows: data passed. The backtest column shows the results table built line by line. All numbers are computed in the page from the six closes.

realised_vollines 21–23
in: daily returnsout: volatility, per day

output on the six days

Not called in this example: vol_target=None, so line 55 does not run. With a target it would return one annualised volatility per day, computed from returns up to that day.

callsvolatility
backtestlines 26–80
in: prices, signalout: results table, one row per day

results table, built line by line

Each row is one column of the table as the line that creates it leaves it. Read top to bottom: every row uses only rows above it. Line 62 (orange) is the one-bar lag.

linecolumnday 1day 2day 3day 4day 5day 6
results table
yearlylines 107–112
in: results tableout: return and drawdown per calendar year

output on the six days

yearReturnMaxDD

The six days fall in one calendar year, so one row.

results table
statslines 83–104
in: results tableout: eleven summary statistics

output on the six days

CAGR and TurnoverPerYear annualise six days by 365/6; they are shown as the code computes them.

↑ calls, once per setting and window · ↓ results table
calls stats
walk_forwardlines 116–157
in: prices, rule, parameter gridout: out-of-sample results (concatenated test windows), selection log

what it produces

Windows with the defaults, train 3 years, test 1 year, on 7 years of daily data (illustrative layout, not a computed result):

window 1
window 2
window 3
window 4
oos

Pink: training window (every grid entry is run; best Sharpe wins). Orange: test window (the chosen entry only). The orange windows, concatenated, are the out-of-sample results.

Selection log, one row per window (structure; values come from a real run):

train_starttest_starttest_endchosentrain_sharpetest_sharpe
datedatedate{"lookback": …}numbernumber
A large and repeated gap between train_sharpe and test_sharpe indicates parameters fitted to noise.

Sources. xau-btc-models/src/backtest.py, 157 lines, as read on 30 October 2026; the six-day example and the selection-log layout follow the project's Code Walkthrough doc. All numbers are computed in the page from the six invented closes; no market data is used. Companion to Backtester implementation; part of quant-notes.