Skip to main content

Backtester for IMC Prosperity 4 algorithms (based on Jasper van Merle's Prosperity 3 Backtester)

Project description

IMC Prosperity 4 Backtester

PyPI Version

This repository contains a backtester for IMC Prosperity 4 algorithms. The output it generates closely matches the format of the output generated by the official submission environment.

Tip: Want to visualize your backtest results? See the Visualizer section below!

Usage

Basic usage:

# Install the latest version of the backtester
$ pip install -U prosperity4btx

# Run the backtester on an algorithm using all data from round 0
$ prosperity4btx <path to algorithm file> 0

Run pip install -U prosperity4btx again when you want to update the backtester to the latest version. You must do this after every round's data becomes available for the backtester to work on new data.

Some more usage examples:

# Backtest on all days from round 1
$ prosperity4btx example/starter.py 1

# Backtest on round 1 day 0
$ prosperity4btx example/starter.py 1-0

# Backtest on round 1 day -1 and round 1 day 0
$ prosperity4btx example/starter.py 1--1 1-0

# Backtest on all days from rounds 1 and 2
$ prosperity4btx example/starter.py 1 2

# You get the idea

# Merge profit and loss across days
$ prosperity4btx example/starter.py 1 --merge-pnl

# Write algorithm output to custom file
$ prosperity4btx example/starter.py 1 --out example.log

# Skip saving the output log to a file
$ prosperity4btx example/starter.py 1 --no-out

# Backtest on custom data
# Requires the value passed to `--data` to be a path to a directory that is similar in structure to https://github.com/Xeeshan85/imc-prosperity-4-backtester/tree/main/prosperity4bt/resources
$ prosperity4btx example/starter.py 1 --data prosperity4bt/resources

# Print trader's output to stdout while running
# This may be helpful when debugging a broken trader
$ prosperity4btx example/starter.py 1 --print

Order Matching

Orders placed by Trader.run at a given timestamp are matched against the order depths and market trades of that timestamp's state. Order depths take priority, if an order can be filled completely using volume in the relevant order depth, market trades are not considered. If not, the backtester matches your order against the timestamp's market trades. In this case the backtester assumes that for each trade, the buyer and the seller of the trade are willing to trade with you instead at the trade's price and volume. Market trades are matched at the price of your orders, e.g. if you place a sell order for €9 and there is a market trade for €10, the sell order is matched at €9 (even though there is a buyer willing to pay €10, this appears to be consistent with what the official Prosperity environment does).

Matching orders against market trades can be configured through the --match-trades option:

  • --match-trades all (default): match market trades with prices equal to or worse than your quotes.
  • --match-trades worse: match market trades with prices worse than your quotes, inspired by team Linear Utility's Prosperity 2 write-up.
  • --match-trades none: do not match market trades against orders.

Limits are enforced before orders are matched to order depths. If for a product your position would exceed the limit, assuming all your orders would get filled, all your orders for that product get canceled.

Visualizer

You can visualize your backtest results interactively using jmerle's IMC Prosperity 3 Visualizer. This allows you to inspect trades, positions, and market data in detail.

Setup:

  1. Open the visualizer web interface

  2. Follow the instructions to copy the Logger class into your trading algorithm

  3. Import and use the logger in your Trader.run() method.

  4. Generate backtest output:

    prosperity4btx main.py 0 --out output.log
    

    You can also use --merge-pnl to merge profit/loss across multiple days, or skip the --out flag to save to the default backtests folder.

  5. Upload your generated output.log file

  6. Explore your backtest results interactively!

Data Files

Data for the following rounds is included:

  • Round 0: Tutorial round with prices and anonymized trades data on EMERALDS and TOMATOES. This is the introductory round to learn the basics of the algorithm.

Product Limits for Prosperity 4:

  • EMERALDS: 80
  • TOMATOES: 80
  • INTARIAN_PEPPER_ROOT: 80
  • ASH_COATED_OSMIUM: 80

Conversions are not supported in this version.

Environment Variables

During backtests two environment variables are set for the trader to know the round and day it's being backtested on. The environment variable named PROSPERITY4BT_ROUND contains the round number and PROSPERITY4BT_DAY contains the day number. Note that these environment variables do not exist in the official submission environment, so make sure the code you submit doesn't require them to be defined.

Development

Follow these steps if you want to make changes to the backtester:

  1. Install uv.
  2. Clone (or fork and clone) this repository.
  3. Open a terminal in your clone of the repository.
  4. Create a venv with uv venv and activate it.
  5. Run uv sync.
  6. Any changes you make are now automatically taken into account the next time you run prosperity4btx inside the venv.

Acknowledgments

This backtester is a port of jmerle's Prosperity 3 backtester adapted for Prosperity 4. Special thanks to Jesper Merle for the original implementation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prosperity4btx-1.0.2.tar.gz (988.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prosperity4btx-1.0.2-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file prosperity4btx-1.0.2.tar.gz.

File metadata

  • Download URL: prosperity4btx-1.0.2.tar.gz
  • Upload date:
  • Size: 988.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for prosperity4btx-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6c4ec4eaf353a84aaa7ce31c010d26d6a866fbc7b29bbb0f371c72f4946eee71
MD5 cecbba34ecabf8a6e7d7194a212e6aa7
BLAKE2b-256 fdc470a5e116f898193bc3d9878fd3b72fd6b7494537fd512de72cf081215a57

See more details on using hashes here.

File details

Details for the file prosperity4btx-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: prosperity4btx-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for prosperity4btx-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b2da1c1b3aa4441c6f4bb6d9e80398d00dd3bf80b226149acccbcbbe5c85aaa
MD5 cf35b6cbd1c4e2fb3d51217aa46b98c4
BLAKE2b-256 57cdeab4df727020f9d59167e215084577cee3fec71ba870f9e477c30a1eb577

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page