opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

Backtesting that accounts for feed latency, order latency, and queue position
GitHub RepoImpressions2

Project Description

View on GitHub

The Backtesting Problem Nobody Talks About: Your Orders Don't Fill Instantly

You've built a market making strategy. The backtest looks great—profit curves climbing smoothly, Sharpe ratios that make you smile. Then you run it live and watch the P&L bleed out in real time. What happened? Chances are your backtest assumed your orders filled the moment they hit the exchange. In reality, there's feed latency, order latency, and a queue of other traders ahead of you. HftBacktest is an open-source framework built to close that gap between simulated and real execution.

What It Does

HftBacktest is a high-frequency trading backtesting tool designed for developing market making and HFT strategies. The core idea is simple but demanding: it accounts for feed latency, order latency, and order queue position when simulating fills. That means instead of assuming your limit order gets executed instantly, the simulation models where you sit in the queue and whether you'd actually get filled before the price moves away.

The framework reconstructs the full order book from Level-2 Market-By-Price and Level-3 Market-By-Order data, then replays tick-by-tick events through a Numba JIT-compiled engine. You can customize the time interval or trigger events based on feed and order receipt. The latency models—both for incoming market data and outgoing orders—are pluggable, so you can use provided defaults or write your own. Same goes for the queue position model that determines fill probability.

Under the hood, it's built in Python with Numba for performance, and there's also a Rust version that supports live trading deployment to Binance Futures and Bybit. The Python and Rust implementations share the same algorithmic core, so you can prototype in Python and move to live trading without rewriting your strategy logic.

Why It's Cool

Most backtesting tools take a shortcut. They either assume fills happen instantly (optimistic) or apply a blanket penalty to everything (pessimistic). HftBacktest's approach is more nuanced, and that matters more than you might think.

  • It targets the small edges. The README makes a sharp point: trading is competitive, and the edges you're hunting are often small. An overly conservative backtest hides those edges entirely. An overly optimistic one invents edges that don't exist. HftBacktest tries to simulate reality closely enough that the difference between backtest and live results shrinks to something you can trust.

  • Validation before optimization. The stated philosophy is refreshing: before you worry about overfitting or parameter optimization, you should be able to replay a specific live trading period—say January 2025—and have the backtest closely match what actually happened. That's the sanity check most quant workflows skip. HftBacktest positions accurate backtesting as the foundation, not an afterthought.

  • Queue position modeling is rare. Most retail-focused backtesters ignore queue dynamics entirely. HftBacktest treats your position in the order book queue as a first-class variable, which is critical for market making where your fills depend heavily on how many orders are ahead of you at each price level.

  • Multi-asset, multi-exchange. The framework supports backtesting across multiple assets and exchanges in a single simulation, which matters if you're running strategies that span venues or hedge across instruments.

  • The live trading bridge. The Rust implementation lets you deploy the same algorithm code to live markets on Binance Futures and Bybit. That's a big deal for prototyping—you're not rewriting strategy logic when you move from simulation to production.

How to Try It

Getting started is straightforward. HftBacktest requires Python 3.11 or later, and you can install it via pip:

pip install hftbacktest

Or clone the development version directly:

git clone https://github.com/nkaz001/hftbacktest

You'll need historical order book and trade data in the format the framework expects—the Data documentation covers the specifics. From there, the project's full documentation has tutorials that walk through practical scenarios, including:

  • High-frequency grid trading
  • Market making with order book imbalance as an alpha signal
  • Market making with an APT (arbitrage pricing theory) alpha model
  • Accelerated backtesting techniques
  • A pricing framework for valuing your quotes

The repository is at github.com/nkaz001/hftbacktest, and the tutorials are worth reading even before you install anything—they're a good way to understand what problems the framework is designed to solve.

Final Thoughts

HftBacktest isn't for everyone. If you're swing trading or running strategies on daily bars, you don't need microsecond-level fill modeling. But if you're building market making or HFT strategies where queue position and latency determine whether you make money or lose it, this is the kind of tool that can save you from the most expensive mistake in quant research: a backtest that lies to you. The validation-first philosophy—replay a known period, compare against live results, then trust the framework—is the right way to approach any serious strategy development. Start with the tutorials, get your data pipeline working, and see if your strategy survives contact with reality.

Follow @githubprojects for more developer tools and open source projects.

Back to Projects
Project ID: dcb48799-ee0f-46bc-9e22-65ef4ea68e7aLast updated: September 7, 2026 at 04:56 AM