Skip to content

Backtest Engine

Sergey Gerasimov edited this page Oct 22, 2026 · 1 revision

Backtest Engine

Execution model

The simulation runs a Backtrader Cerebro loop with a private portfolio ledger. A controller receives each bar, builds the synthetic option chain, manages expiries and assignments, and records the state of the book.

The engine is deterministic: the same data, configuration, and seed produce the same result.


Synthetic option chain

For every date, the engine:

  1. Computes 30-day realized volatility from log returns.
  2. Converts it to a synthetic implied volatility with a configurable multiplier.
  3. Prices calls and puts with Black-Scholes.
  4. Computes Delta, Gamma, Theta, Vega, and Rho.
  5. Selects strikes around the configured offset and expiry.

There are no bid-ask spreads or a volatility surface across strikes. This is a teaching model, not an execution model.


Portfolio ledger

Field Meaning
cash Settled cash after premiums and fees
spot_value Mark-to-market value of spot holdings
option_value Mark-to-market value of open options
futures_value Basis/futures leg mark
nav Total net asset value
drawdown Current drawdown from peak
iv Synthetic implied volatility
contracts Open option contracts
benchmark Buy-and-hold benchmark index

The ledger records every trade with kind, strike, and expiry metadata so the arena can attribute breaches and assignments.


Metrics

  • Total return and CAGR
  • Maximum drawdown
  • Sharpe and Sortino ratios
  • Win rate and profit factor
  • Calmar
  • Premium collected and fees paid
  • Cycle P&L and equity index against buy & hold

Extreme or undefined ratios are clamped or reported as zero rather than producing infinities.


Events and the AI bridge

The controller emits:

  • STRIKE_BREACH when spot moves beyond a short option strike;
  • ASSIGNMENT when an expiring short option finishes in the money.

With an AIAgentBridge, events are normalized and retained; if an asyncio loop is attached, the bridge schedules the coroutine with asyncio.run_coroutine_threadsafe. See AI Agents.


Failure behavior

If Backtrader cannot run, a direct controller loop produces the same ledger and NAV. The UI reports data-source problems as friendly messages rather than technical tracebacks.


Key limits

  • American early exercise is not modelled.
  • No bid-ask spread, open interest, or borrow costs.
  • The basis leg uses an idealized convergence hedge.
  • Dividends, taxes, and exchange contract specifications are simplified.

See Strategies, Data Pipeline, and Testing & CI.

Clone this wiki locally