Market Simulation

Overview

Market Simulation

My role: Architecture to deployed system.

Challenge: Provide a true view on trade strategy performance using historical data, instead of widely available "sum of percent gain/loss"

Solution: Built and deployed an end-to-end backtesting system (API, web UI, charting) to test trading strategies against actual historical market data using configurable capital allocation strategies

Impact: Enabled realistic assessment of trading and capital allocation strategies under real market and capital constraints.

Technologies: AWS (EC2), GitHub, Python, Flask, WireGuard

Misleading Returns

Many backtesting tools present performance with charts that suggest enormous returns. Many times, the performance is presented with charts that show the "capital" as it grows rapidly. In fact, many such charts are built using flawed math:

Start at 100
+ 2.1%
- 1.2%
+ 1.0%
+ 0.5%
End with 102.4 (+2.4%)

These charts do not take into account capital, fees and compounding, and reflect the unrealistic scenario that the user could take all possible trades with 100% of the capital.

Realistic Simulation

In reality, only a portion of the capital would be allocated to any given trade, and overall performance will be an outcome of the combination of the trading strategy and the capital allocation strategy. In addition, exchange fees would likely render trades like + 0.5% as an overall loss.

The backtesting simulation I developed used a two-stage process:

  1. Replay the trading strategy against the historical data to identify the events
  2. Simulate the actual change in capital based on the capital allocation strategy

By splitting the process into two, the longer first step could be executed once for a new trading strategy and then various capital allocation strategies and exchange fee rates could be assessed quickly.

The system allows drilling down into each transaction, assessing the behaviour of the trading strategy in detail, as well as analysing the overall returns.

Delivery and Technical Approach

Data Sources

Historical market data could be ingested from external APIs, or reused from data collected by the multi-exchange trading system (described separately).

Two-Stage Backtest

The system replays historical data in compressed time through the same decision algorithms used (or envisioned) for live trading, producing a stream of candidate trade events without capital allocation.

Each trade event captured the decision window and outcome (entry, hold period, exit/cash-in), along with the context needed for later allocation.

Capital Allocation

Users define capital allocation strategies with constraints (minimum/maximum allocation as a percentage and absolute value) and prioritisation rules (e.g., per trading pair). The simulator then applies allocation against an initial capital amount and runs through the precomputed trade events, tracking capital evolution, fees, and per-trade outcomes.

Deployment and UI

The system ran on AWS EC2 with a web UI where users could load trading strategies and capital allocation strategies, trigger data downloads, execute simulations, and drill down into individual events and outcomes.