This repository contains a Python-based backtester designed in preparation for the IMC Prosperity 4 challenge.
Key Notes:
- Origin: This project is heavily based on jmerle/imc-prosperity-3-backtester, but it has been rewritten to utilize a more Object-Oriented Programming (OOP) style.
- Current Status: The codebase is up to date with the Prosperity 4 tutorial round.
- License: MIT License.
Usage:
Basic usage:
Run the backtester on an algorithm using all data from round 0
$ python -m prosperity4bt <path to algorithm file> 0Run the backtester on an algorithm using all data from round 0, day '-2'
$ python -m prosperity4bt <path to algorithm file> 0--2If you see: No module named 'datamodel', set PYTHONPATH to the folder containing datamodel.py:
$env:PYTHONPATH="<path to>\imc-prosperity-4-backtester\prosperity4bt"Run/Debug from Pycham
Add Run/Debug Configuration:
The architecture of the program is modularized to cleanly separate data loading, simulation execution, and order matching. Below is the structural diagram of the backtester:
Based on the architecture diagram, the system operates through the following primary components and execution steps:
This is the top-level driver of the simulation:
- Initialization: It begins by executing the
Load Algorithm Modulestep to ingest your trading logic. - Iteration: It initializes an empty
results = []list and iterates through a nested loop:for each roundandfor each day. For every day, it executes theRun Testfunction, which calls theTestRunner. It then appends the output to theresultslist. - Completion: Once all rounds and days are processed, it calls
Merge Resultsto combine the data and triggersWrite Output Fileto produce a consolidated log (e.g.,2026-03-01_08-35-51.logcontaining trading results).
The TestRunner is responsible for simulating the market environment for a single day:
- Read Data: It triggers
Read Datawhich calls theBackDataReaderto read the market data from 2 csv files (price and trade). The reader parses the files (e.g.prices_round_1_day_0.csvandtrades_round_1_day_0.csv) and returns aBacktestDataobject. This step yieldsResult - Stage 0. - Timestamp Loop: For each timestamp in the loaded data (
for each timestamp), the runner executes a sequence of events:- Initialize TradeState: Prepares the current state of the market.
- Trade: Creates a
TradingStateobject and passes it into the user'sAlgorithm. - Algorithm Execution: The user's
Algorithmprocesses the state and returns proposed orders and a string asTraderData. Any standard output (stdout) generated by the algorithm is captured aslambda_log. This execution step yieldsResult - Stage 1. - Create Activity Logs: The
ActivityLogCreatorsteps in to record the actions, orders, and market state of the current timestamp. This yieldsResult - Stage 2. - Match Orders: The proposed orders are passed to the
OrderMatchMaker, which simulates the exchange mechanics to fill orders against the historical order book. This yieldsResult - Stage 3.
- Results Aggregation: After the timestamp loop concludes, the overall day's simulation yields
Result - Stage 4, which is returned back to theBackTester.
BackDataReader: Handles the file ingestion of CSV price and trade data into programmatic objects.ActivityLogCreator: Responsible for standardizing and formatting the activity logs for later analysis and debugging.OrderMatchMaker: The internal simulation engine that determines which algorithm orders execute and updating positions.
The backtester relies on a specific set of data models to process market information and log simulation results cleanly.
-
datamodel.py: This file contains the core data models that are shared between theBackTesterand your customAlgorithm. (Please do not change this file). Modifying it may break compatibility with the official Prosperity environment. -
models/directory: The models located within themodelsfolder are specifically defined for the internal operations of theBackTester. -
Input Data Models (
models/input.py): This file defines the models that capture the raw market data from the input files. During the setup phase, data is extracted from the price data files and trade data files:Price Data:
Trade Data:
This raw data is then structured and filled into the
BacktestDatamodel, which acts as the data source for the simulation:Backtest Data:
-
Result Data Models (
models/output.py): Models defined here are responsible for capturing the test result data generated during the simulation.Once the backtest is complete, the system compiles the findings into a
BacktestResultobject:Backtest Result:
Finally, this structured result data is written directly into the standard output log file so you can review your algorithm's performance and activities:
Output Log File:
lambda_log It contains the standard output generated by the algorithm. If you use the Logger class specified in my Visualizer, the data captured will be something like this:







