Backtesting and Optimization

Dual Backtesting Engines

Technical analysis of the Fast Vector Backtester and Event-Driven DepthSight Backtester — their architectures, trade simulation strategies, KPI calculations, and when to use each.

⏱️ 8 min read📊 Level: Intermediate

DepthSight integrates two separate backtesting engines designed to solve different phases of strategy development: the Fast Vector Backtester (for high-speed testing and genetic parameter optimization) and the Event-Driven DepthSight Backtester (for realistic live simulation with L2 order book precision).


Fast Vector Backtester

The FastVectorBacktester (ot_module/fast_vector_backtester.py, ~7,290 lines) is built using vectorized operations via pandas, numpy, and optional Numba. It processes entire arrays of historical data at once rather than step-by-step.

Rendering diagram...

Run Cycle

Sources:

Data Preparation (_prepare_data, line 4376)

The preparation phase handles:

  1. Multi-Timeframe Support: Indicators are computed on their specific timeframe (5m, 15m, 1h) and broadcast to the 1m main index via _broadcast_to_1m() (line 4544). The broadcast cache (self.broadcasted_cache, line 860) prevents redundant calculations across multiple strategies sharing the same data.

  2. Dynamic Indicator Extraction: Calls _extract_indicators_from_json() (line 4555) to recursively walk the strategy JSON and extract all required indicator names, periods, and timeframes:

Sources:
  1. Supported Indicator Calculations: EMA, SMA, RSI, NATR, ATR, ADX, MACD, Bollinger Bands, Stochastic — each on the appropriate timeframe DataFrame.

Signal Generation (_generate_signals, line 4666)

The signal pipeline processes conditions through three layers:

Layer 1 — Entry Condition Tree: Uses the unified condition_core evaluation engine. The strategy JSON's entryConditions tree is recursively evaluated using AND/OR logic gates, producing a boolean mask:

Sources:

Layer 2 — Filters: The ilters section evaluates independently. If any filter fails, the candle is rejected regardless of entry conditions. Tracks which specific filter node caused each rejection via _evaluate_condition_tree_with_failures().

Layer 3 — Foundation Weight System (lines 4698–4726): Each condition carries a configurable weight. A otal_weight is calculated across all triggered foundations. Only when otal_weight >= effective_threshold is the entry mask accepted:

Sources:

Layer 4 — Oracle Integration (line 4734): If enabled, the GMM regime signal is overlaid as an additional filter.

Condition Types Supported

The system evaluates ~30 condition types via _evaluate_condition_tree (line 4753):

CategoryConditions
Time Filtersrading_session, ime_filter
Trend Filtersrend_filter, dx_filter, tc_state_filter, correlation
Volatility Filtersolatility_filter,
atr_filter,
el_vol_filter, market_activity
Entry Conditionsma_cross_condition, ollinger_bands_condition, stochastic_condition,
si_condition, macd_condition, rend_direction, ape_condition, alue_comparison, price_vs_level, olume_confirmation, classic_pattern, local_level, significant_level, level_touch_analyzer, olatility_squeeze, price_action_analyzer,
ound_level, open_interest,
eturn_to_level

Trade Simulation (_simulate_trades_vectorized_v2, line 5436)

Despite the "vectorized" name, this method uses a vectorized-sequential hybrid approach — signal detection is vectorized, but each individual trade is simulated sequentially with per-trade state:

Sources:

Key simulation features:

  • Entry: Next candle open + configurable slippage (default 0.06%)
  • Stop Loss: ATR-multiplier, percentage, or fixed price
  • Take Profit: R/R multiplier, ATR-multiplier, fixed price, or percentage
  • Partial Exits: Multiple sorted targets filled independently
  • Grid Management: Initializes grid orders, fills them on subsequent candles
  • DCA: Percentage, ATR-based, or custom-condition step triggers
  • Trailing Stop: Percentage-based ratcheting
  • Breakeven: Via first TP hit or by R/R threshold
  • Phantom Trade Tracking: After BE exit, simulates original TP/SL for missed opportunity analysis
  • Funding Rate PnL: 8-hour funding period tracking between executions

KPI Calculation (_calculate_kpis, line 7068)

Sources:

Returns: otal_trades, otal_pnl_pct, otal_pnl, win_rate, max_dd, profit_factor, sharpe_ratio, sortino_ratio, consistency_score, otal_commission, equity_curve, nalytics_report.

ParameterDefaultDescription
initial_balance100.0Starting capital
commission_pct0.12%Per-trade commission
slippage_pct0.06%Entry/exit slippage assumption
ase_timeframe"1m"Primary candle resolution

DepthSight Backtester (Event-Driven)

The DepthSightBacktester (ot_module/depthsight_backtester.py, ~5,520 lines) uses an event-driven design that mimics the live trading runtime. It processes events sequentially, updating technical state candle-by-candle and evaluating L2 order book snapshots tick-by-tick.

Architecture

Rendering diagram...

Run Cycle (

un_async(), line 3496)

Sources:

L2 Order Book Integration

The event-driven backtester reads compressed .bin.zst order book snapshots via L2HistoricalDataReader (line 255), using LRU caching with msgpack + zstandard compression. It parses depth_p1..p5 / depth_m1..m5 columns into structured bids/asks (BookDepth):

Sources:

Market impact is simulated using simulate_market_order_execution with actual order book snapshots, providing realistic slippage calculations.

ML Integration Features

The event-driven backtester supports optional ML confirmation:

  • ML Inference: Calls ModelPipeline.predict() on extracted features to filter signals.
  • ML Training Mode: Generates training data with y_true labels via Numba-optimized _get_ml_target_label_numba() (line 86).
  • Strategy-Symbol Dynamic Risk: Tracks per-(symbol, strategy) rolling PnL, win rate, and consecutive losses to dynamically adjust risk multipliers during backtest (line 2878).

KPI Calculation

Returns additional metrics beyond the vector backtester:

  • L2 slippage tracking (entry + exit slippage in USD)
  • Average slippage per trade, average total slippage %
  • Equity curve with proper timestamps (daily resampled)
  • ML-specific metrics in ML training mode

Summary Comparison

MetricFast Vector BacktesterEvent-Driven Backtester
SpeedExtremely High (~100k candles/sec)Medium-Low (simulates sequential time)
Data ResolutionCandle OHCLVTick-by-tick & L2 Order Book
Execution EmulationSimplistic (approximate slippage)Precise (slippage, latency, order queue)
L2 Book IntegrationNoYes (.bin.zst snapshots)
ML ConfirmationOracle filter onlyFull ModelPipeline inference
Funding RateSimplified 8h calculationPer-position tracking
DB PersistenceMinimalFull BacktestTrade + BacktestTradeExecution records
Best Used ForGenetic optimization, initial hypothesis testingFinal risk validation, live strategy parity
Code Locationot_module/fast_vector_backtester.pyot_module/depthsight_backtester.py