Building a Better Market Microstructure Analyzer: Backtests, Reproducibility, and Honest Limits

Building a Better Market Microstructure Analyzer: Backtests, Reproducibility, and Honest Limits

●1 ●3 ●5
calendar_today • schedule4 min read

In my first post, I introduced a project that processes five-level order-book snapshots and displays measures such as spread, order-flow imbalance (OFI), and VWAP.

After publishing it, I wanted to make it more than a basic demo. Adding another indicator would have been easy to describe, but it would not have answered the question I had started asking about the project: how much can I trust its outputs?

I went back to the backtesting methods, the data generator, and the parts of the pipeline that connect one calculation to the next. I also made the project’s licence and limitations easier to find. Here is what changed and what I learned.

A backtest needs to account for the whole trade

The project includes a directional backtester based on OFI. The initial version could produce a trade log and performance figures, but some of its accounting was too basic. In particular, transaction costs needed to be reconciled across entry and exit, and drawdown needed a meaningful starting capital value.

I corrected those calculations so that trade-level profit and loss can be checked against the change in final equity. I also removed an annualised return statistic that did not make sense for a sequence of synthetic ticks and replaced it with an explicitly unannualised completed-trade measure.

This may sound less exciting than adding a new strategy. It was more useful. If the accounting is inconsistent, a convincing-looking chart is not evidence of anything.

Testing one period is not enough

A backtest can look good because its parameters happen to suit the data used to choose them. To make that harder, I added a walk-forward evaluation workflow with chronological splits. Parameters are selected using earlier observations and assessed on later held-out observations. The workflow also checks what happens under harsher transaction costs and whether results are stable across folds.

That is a better test of the evaluation method than running one strategy over an entire dataset and reporting its best outcome. But there is an important boundary here: the project’s validated data workflow is still synthetic. Passing a walk-forward check on generated data does not establish that OFI predicts returns in Indian equities or that a strategy would be profitable in a live market.

I wanted the project to make that distinction clear. A backtester can be implemented and internally consistent without being a validated trading strategy.

Reproducing the same experiment

Earlier, the synthetic feed used global randomness and wall-clock time. That made it difficult to investigate a changed result: had the code changed, or had the input simply changed?

The generator now uses a local random-number generator, an explicit seed, a fixed clock when requested, and a defined tick count. Running the same configuration produces the same input records and analytics output. This lets me compare code changes against a stable example rather than a moving target.

The repository also has automated tests for the analytics engine and connected workflows. Its validation guide describes what those checks cover and, just as importantly, what they do not cover.

Fixing the stream, not only the formulas

Some problems only appeared when components ran together. The analytics engine initially failed on its first snapshot because an anomaly-detector argument did not match its constructor. Internal code also disagreed about whether a snapshot’s time field was called ts or timestamp. Tick classification returned text labels where signed-volume calculations needed numerical values.

I fixed these connections and added tests that exercise more than an isolated formula. I also changed the rolling OFI windows to use queues and running sums rather than scanning the retained history on each tick. Anomaly calculations similarly maintain rolling sums instead of repeatedly traversing their windows.

The lesson for me was that “real-time” is not just about displaying a changing chart. The calculations need to remain manageable as more observations arrive, and the entire path from input to output needs to work.

Naming results accurately

Reviewing the project also meant revisiting the labels on some outputs. A trade/quote variance diagnostic had been described as a classical Hasbrouck information-share estimate, although the project’s single-venue setup does not implement that method. It is now described as the diagnostic it actually calculates.

Similarly, the execution replay had called a change in the last fill price “market impact.” Because the replay uses a static order book, it cannot show the causal effect an order would have on the market. The output now calls this last-fill slippage, and the replay-VWAP comparison carries its look-ahead limitation.

I would rather have a narrower, accurate description of a result than a more impressive label that the code cannot defend.

What remains unfinished

There is no live exchange feed in the validated workflow. I do not currently have an Angel One account connected to the project, and the SmartAPI adapter in the repository remains a stub. The interactive demo replays simulated data; it does not show live NSE order books.

Before making claims about live performance or a trading signal, I would need appropriate real data and further validation. Live ingestion would also require careful handling of reconnects, gaps, rate limits, and market sessions.

I revisited the project’s licence as part of making the public repository clearer; it now displays its AGPL-3.0 terms alongside the code and documentation. The repository contains the current implementation, validation notes, and instructions for reproducing the synthetic-data workflow.

The project is stronger now because I can explain what runs, what has been tested, and what I still cannot claim. That is the standard I want to carry into the next stage of the work.

Want more #python & #opensource deep dives?

Join 6,182+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 1 of 1 in My projects

2 Comments

1 vote
0

More Posts

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

Comparison: Universal Import vs. Plaid/Yodlee

Pocket Portfolio - Mar 12

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

Stop trusting verdicts your policy no longer supports

VEACESLAV - Dec 1
chevron_left
444 Points • 9 Badges
1Posts
1Comments
3Connections
IIT Bombay | Building low-latency trading systems and market microstructure tools | C++ · Python · C#

Related Jobs

View all jobs →

Commenters (This Week)

14 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!