nx_fin_backtest.nx
buildroot/runtime/nx_fin_backtest.nx
about
nx_fin_backtest.nx -- R2 of the trading stack: the TRUTH ENGINE. Given OHLCV bars (from nx_fin_marketdata) +
a pre-computed long/flat SIGNAL per bar (produced by an R3 strategy from data up to that bar -> NO look-ahead
by construction), simulate the trades point-in-time with realistic COST (slippage_bps + commission) and a
LIQUIDITY cap (md_capacity of the name's ADV), mark-to-market every bar, and emit the HONEST risk metrics:
expectancy, win-rate, profit-factor, MAX DRAWDOWN, return. This is the machine that proves or KILLS an edge
before a dollar is risked -- the antidote to the "20-30%/trade, no losses" fantasy. Pure, deterministic,
i64 fixed-point (cents / basis-points), NO floats. Composes R1 (md_capacity, MD_FIELDS). Single-responsibility:
it SIMULATES a signal; it does not GENERATE one (that is R3) and does not SIZE a portfolio (that is R4).
license_tier: ORIGINAL
dependencies 2 imports · 5 importers
imports: nx_syscalls.nxnx_fin_marketdata.nx
imported by: nx_fin_backtest_gate.nxnx_fin_marketdata_fetch.nxnx_fin_paper.nxnx_fin_paper_gate.nxnx_fin_strategy_gate.nx
structs
| none |
consts
| 12 | const BT_MAGIC_10000: i64 = 10000 |
| 13 | const BT_MAGIC_1000000000: i64 = 1000000000 |
| 16 | const BT_TRADES: i64 = 0 |
| 17 | const BT_WINS: i64 = 1 |
| 18 | const BT_LOSSES: i64 = 2 |
| 19 | const BT_GROSS_WIN: i64 = 3 // cents |
| 20 | const BT_GROSS_LOSS:i64 = 4 // cents (positive) |
| 21 | const BT_NET: i64 = 5 // cents |
| 22 | const BT_EXPECTANCY:i64 = 6 // cents per trade |
| 23 | const BT_WINRATE: i64 = 7 // basis points |
| 24 | const BT_PF: i64 = 8 // profit factor, basis points (10000 = 1.0x) |
| 25 | const BT_MAXDD: i64 = 9 // basis points (peak-to-trough of marked equity) |
| 26 | const BT_RET: i64 = 10 // basis points (total return on start equity) |
| 27 | const BT_FINAL_EQ: i64 = 11 // cents |
functions
| 31 | func bt_position_size(cash_cents: i64, price_cents: i64, adv_cents: i64, participation_bps: i64) -> i64 |
| 43 | func bt_run(bars: *i64, nbars: i64, signals: *i64, start_equity: i64, |