Skip to content
All library documents

Event-Driven Tick Replay for Multi-Asset C++ Backtests

Article Quant Q&A · Author: Theodore

Summary

This note describes a way to backtest C++ trading strategies across multiple instruments without relying on an online platform. Store each instrument’s historical ticks in timestamp order, using separate binary files or another suitable format. Load the first tick from each data stream into a priority queue ordered by time, then repeatedly process the earliest tick and replace it with the next tick from that stream. This merges the streams into one chronological event sequence while keeping only a small amount of tick data in memory.

The same queue can model execution delay: when a strategy creates an order, schedule an order event at a later timestamp and simulate its execution when that event is processed. The document presents this as a personal system design rather than a tested software package or a complete backtesting specification. It does not detail transaction costs, partial fills, market impact, data quality, or how to choose a realistic delay, so those aspects would need separate treatment.

Key ideas

  • Store each instrument’s historical data in timestamp order.
  • Use a priority queue to process ticks from multiple instruments chronologically.
  • After processing a tick, load the next tick from the same instrument’s data stream.
  • Represent execution delay by scheduling an order event for a later time.
  • The described design does not specify costs, fill models, or market impact.

Tags

Full text
# Backtesting C++ Algorithms


# Backtesting C++ Algorithms












What software is available for backtesting trading algorithms written in the C++ programming language? I would not like to use an online service such as Quantopian or Quantconnect (they don’t offer C++ support regardless).

Are there any libraries or applications that can be used for this?

## Answer by Enrico Detoma (score 3, accepted)

https://quant.stackexchange.com/a/40023

I developed my own backtesting system for multiple tickers in C++, maybe some of these ideas may help.

Storage: I store every ticker's data into a binary file (one file per ticker), ordered by timestamp. Any other kind of storage (CSV files, SQLITE database), ordered by timestamp will do, for the purpose of backtesting.

Backtesting: I create a priority queue in memory (priority given by timestamp), then I read a single tick from each of the ticker's files and put them into the priority queue, then I start consuming the ticks from the priority queue, lowest timestamp first. Every time I consume a tick, I read another tick from the file from which that tick came out, and put it into the priority queue. In this way I can also simulate a delay for orders going to the market: every time the strategy I'm backtesting generates an order, I put an "order tick" with a delayed timestamp into the priority queue, and I simulate actual execution only when that order tick is consumed from the queue.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.