Skip to main content

Module engine

Module engine 

Source
Expand description

The replay engine.

Single-threaded loop: feeds candles to the brain in order, builds an order from each non-Hold decision, applies slippage and fees, and updates a synthetic per-symbol position. On position-reducing fills (closes or flips) a TradeOutcome is emitted into the result.

§Perp funding

With a FundingModel configured (off by default), settlements in the window (previous_candle_time, candle_time] are booked against each symbol’s position as it stood entering the candle, before any bracket fill or decision on that candle: cashflow = −sign(qty) × rate × |qty| × contract_value × last_mark (positive rate → longs pay, shorts receive). Cashflows land in cash immediately (so the equity curve carries them) and accrue against the open position; when the position closes the accrual is attributed to the emitted TradeOutcomes pro-rata by closed quantity. Funding does not feed the session-PnL risk gate (parity with live, which gates on trade gross − fee); it does flow into the circuit breaker’s win/loss classification via TradeOutcome::net_pnl.

§Fill models

BacktestConfig::fill_model selects the fill semantics. The default (FillModel::TakerAtClose) keeps the historical behaviour: everything fills (or is dropped) on the decision candle, and protective brackets fill at their fixed level. Opting in to FillModel::Resting gives honest resting-order semantics — non-marketable limits rest across candles (fill on cross at the limit, at the open on a gap through), stops fill at the level or worse, standalone stop/TP protective orders work without the other leg, and same-candle ambiguity resolves pessimistically. The complete convention list lives on FillModel.

§Multi-symbol replay

Each candle is tagged with a symbol. The engine maintains independent Position state per symbol but a single shared cash balance. When Backtest::with_candles is used, every candle is assumed to be for the (single) symbol on the config. For multi-symbol runs use Backtest::with_symbol_candles.

Structs§

Backtest
The replay engine itself. Configure via BacktestConfig, attach a Brain and one or more candle series, then .run().await for the result.