Skip to main content

Module engine

Module engine 

Source
Expand description

The event-driven backtest loop.

Look-ahead bias is structurally prevented by default: signal-driven orders are decided on a bar’s close and fill on the next bar’s open. An opt-in fill_timing: "close" instead fills market orders on the signalling bar’s own close (close-to-close, deliberately optimistic). Stop-loss and take-profit are price levels checked intrabar against each bar’s OHLC and fill at the level (conservative: the stop is assumed hit before the target when a bar’s range brackets both). Equity is marked to market at every close.

Supports long and short positions; market, limit and stop entry orders (a limit/stop rests at a percent offset from the signal close and fills when a later bar reaches it, otherwise it keeps working); maker/taker fees (resting limit fills pay maker, market/stop fills pay taker) and fixed-bps, order-book-spread or volume-impact slippage; and leverage / position sizing (fixed fraction / cash / quantity, risk-per-trade and vol-target, capped by max_leverage and max_position_pct; without max_leverage the cap is 1x equity — no leverage by default). Execution latency (latency_bars) delays every fill, and volume-participation partial fills (partial_fills + max_participation) cap an entry to a fraction of the bar’s volume. Perpetual funding (costs.funding) is charged each bar to an open position from the derivatives feed, and a leveraged position is liquidated intrabar at its bankruptcy price when risk.liquidation is set.

Structs§

Feeds
The optional non-OHLCV feeds for one bar: a reference-series close (pairwise), a derivatives tick (derivatives) and an order-book snapshot (order-book). Absent feeds are None; indicators that need a missing feed yield nothing.
StreamingBacktest
A streaming backtest: feed bars one at a time with StreamingBacktest::step, then StreamingBacktest::finish. The historical runner is exactly this fed from a slice, so backtest and live share one code path — point step at a live feed and the same engine becomes the live bot.

Constants§

DEFAULT_CAPITAL
Default starting capital for the runner.

Functions§

run
Run a backtest of spec over candles with the default capital.
run_stream
Run a backtest over a candle stream, invoking on_bar with the streaming state after each bar — the streaming entry point for a live tail or for emitting the equity curve incrementally.
run_with_capital
Run a backtest with explicit starting capital.
run_with_cross_section
Run a backtest with a per-bar market cross-section for breadth indicators (advance/decline, McClellan, TRIN, …). sections must be the same length as candles.
run_with_deriv
Run a backtest with a per-bar derivatives feed for derivatives indicators (funding, open interest, long/short ratio, …). derivs must be the same length as candles.
run_with_orderbook
Run a backtest with a per-bar order-book feed for order-book indicators (imbalance, microprice, quoted spread, …). books must be the same length as candles.
run_with_ref
Run a backtest with a reference price series for pairwise indicators. The reference candle at each index supplies the second input (its close) to pairwise indicators such as correlation, beta or spread. reference must be the same length as candles.
run_with_trades
Run a backtest with a per-bar trade feed for trade-flow indicators (CVD, trade imbalance, VPIN, signed volume, …). trades[i] is the list of trades that printed within bar i; the outer length must match candles.