code wiki / (root) / nx_fin_backtest.nx

nx_fin_backtest.nx

buildroot/runtime/nx_fin_backtest.nx

5421 B103 linesdepth 3pulls 3 transitivereach 5 importersview sourcekind librarytopic fin
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_fin_marketdata.nx nx_fin_backtest.nx nx_fin_backtest_gate.nx nx_fin_marketdata_fetch.nx nx_fin_paper.nx nx_fin_paper_gate.nx nx_fin_strategy_gate.nx

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

12const BT_MAGIC_10000: i64 = 10000
13const BT_MAGIC_1000000000: i64 = 1000000000
16const BT_TRADES: i64 = 0
17const BT_WINS: i64 = 1
18const BT_LOSSES: i64 = 2
19const BT_GROSS_WIN: i64 = 3 // cents
20const BT_GROSS_LOSS:i64 = 4 // cents (positive)
21const BT_NET: i64 = 5 // cents
22const BT_EXPECTANCY:i64 = 6 // cents per trade
23const BT_WINRATE: i64 = 7 // basis points
24const BT_PF: i64 = 8 // profit factor, basis points (10000 = 1.0x)
25const BT_MAXDD: i64 = 9 // basis points (peak-to-trough of marked equity)
26const BT_RET: i64 = 10 // basis points (total return on start equity)
27const BT_FINAL_EQ: i64 = 11 // cents

functions

31func bt_position_size(cash_cents: i64, price_cents: i64, adv_cents: i64, participation_bps: i64) -> i64
called by 2: bt_runmain calls 1: md_capacity
43func bt_run(bars: *i64, nbars: i64, signals: *i64, start_equity: i64,
called by 3: mainpp_oos_runmain calls 1: bt_position_size