Part 7: Backtesting

Backtesting lets you test a trading strategy against historical data to estimate how it would have performed. This part covers building an event-driven backtester that processes data bar-by-bar, simulates order execution with realistic slippage and commission models, and calculates performance metrics. We also introduce walk-forward analysis to guard against overfitting.

Chapters

  1. Event-Driven Engine – Event-driven vs vectorized approaches, execution flow, and basic usage
  2. Execution Models – Slippage models, commission models, and performance metrics
  3. Walk-Forward Analysis – Rolling train/test splits, visualization, and overfitting pitfalls

Execution Flow

The diagram below shows how Puffin’s backtester processes each bar. Orders generated on one bar execute on the next, ensuring the strategy never sees future data.

graph TD
    A[Start: Bar 0] --> B[Get data up to current bar]
    B --> C[Execute pending orders from previous bar]
    C --> D[Strategy generates signals]
    D --> E[Convert signals to orders]
    E --> F[Record portfolio value]
    F --> G{More bars?}
    G -->|Yes| B
    G -->|No| H[Calculate metrics]

    classDef dark fill:#2d5016,stroke:#1a3a1a,color:#e8e0d4
    classDef accent fill:#1a3a5c,stroke:#0d2137,color:#e8e0d4
    classDef decision fill:#6b2d5b,stroke:#4a1a4a,color:#e8e0d4
    classDef endpoint fill:#8b4513,stroke:#5c2d0e,color:#e8e0d4

    class A,H endpoint
    class B,C,F dark
    class D,E accent
    class G decision

Notebook: Run the examples interactively in operational.ipynb

Source Code

Browse the implementation: puffin/backtest/


Table of contents