Project 8: Event-Driven Backtesting Engine

Build an event-driven backtester that simulates realistic trade execution.


Project Overview

Attribute Value
Difficulty Level 3: Advanced
Time Estimate 2-3 weeks
Main Language Python
Alternative Languages Java, C++
Knowledge Area Systems design
Tools CSV data, logging
Main Book “Algorithmic Trading” by Ernest Chan

What you’ll build: A backtesting engine with event queues for market data, signals, orders, and fills.

Why it teaches quant: Event-driven architecture mirrors real trading systems and exposes timing issues.

Core challenges you’ll face:

  • Designing event queues and handlers
  • Modeling slippage and commissions
  • Preventing lookahead bias

Real World Outcome

You will run strategies through a realistic pipeline and get trade logs and performance metrics.

Example Output:

$ python engine.py --strategy sma_crossover
Events processed: 120000
Trades executed: 54
Saved report to reports/sma_report.txt

Verification steps:

  • Confirm order timing matches event flow
  • Validate fills against market data

The Core Question You’re Answering

“How do I simulate the full lifecycle of a trade without cheating?”

This is the bridge from toy backtests to real systems.


Concepts You Must Understand First

Stop and research these before coding:

  1. Event-driven design
    • Why do trading systems use event queues?
    • Book Reference: “Designing Event-Driven Systems” by Ben Stopford, Ch. 1
  2. Order types
    • What is the difference between market and limit orders?
    • Book Reference: “Trading and Exchanges” by Larry Harris, Ch. 6
  3. Slippage and costs
    • How do execution costs distort returns?
    • Book Reference: “Algorithmic Trading” by Ernest Chan, Ch. 5

Questions to Guide Your Design

  1. Event flow
    • What event types will you support?
    • How will you manage the event queue order?
  2. Execution model
    • Will you fill orders at close, open, or midpoint?
    • How will you simulate slippage?

Thinking Exercise

Event Sequence

List the sequence of events from a price update to a filled order in your system.

Questions while working:

  • Where can lookahead bias sneak in?
  • How do you ensure deterministic ordering?

The Interview Questions They’ll Ask

Prepare to answer these:

  1. “What is an event-driven backtester?”
  2. “How do you avoid lookahead bias in event systems?”
  3. “What are common order types and how are they simulated?”
  4. “Why do execution costs matter?”
  5. “What is the difference between signal and order?”

Hints in Layers

Hint 1: Starting Point Start with a minimal event loop: market -> signal -> order -> fill.

Hint 2: Next Level Add logging to verify event order.

Hint 3: Technical Details Simulate slippage as a function of volatility or spread.

Hint 4: Tools/Debugging Replay a small dataset and inspect event logs.


Books That Will Help

Topic Book Chapter
Event-driven design “Designing Event-Driven Systems” by Ben Stopford Ch. 1
Order types “Trading and Exchanges” by Larry Harris Ch. 6
Execution costs “Algorithmic Trading” by Ernest Chan Ch. 5

Implementation Hints

  • Keep each event handler stateless where possible.
  • Use a single clock source to order events.
  • Write unit tests for event ordering.

Learning Milestones

  1. First milestone: You can process an event loop end-to-end.
  2. Second milestone: You can model execution costs.
  3. Final milestone: You can explain how event ordering affects results.