CTC-Executioner is a tool that provides an on-demand execution/placement strategy for limit orders on crypto currency markets using Reinforcement Learning techniques. The underlying framework provides functionalities which allow to analyse order book data and derive features thereof. Those findings can then be used in order to dynamically update the decision making process of the execution strategy.
The methods being used are based on a research project (master thesis) currently proceeding at TU Delft.
- Python: 3.10 or higher (tested with Python 3.13.7)
- Gymnasium: 1.0.0 or higher (migrated from OpenAI Gym)
pip install -r requirements.txt# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activateYou should see (venv) in your terminal prompt.
Make sure all dependencies are installed:
pip install -r requirements.txtKey dependencies:
stable-baselines3>=2.0.0gymnasium>=1.0.0tensorflow>=2.15.0keras>=3.0.0numpy>=1.24.0pandas>=2.0.0
python agent_sb3.pyThis will:
- Create an artificial orderbook for testing
- Train a DQN model for 100,000 steps
- Save the model to
models/dqn_ctc_executioner_sb3.zip - Test the model for 10 episodes
- Display action plots if configured
python agent_dqn.pyNote: This requires data files. Make sure data/events/ob-1.tsv exists or modify the file path in the script.
python test_basic.pyThis runs basic functionality tests to verify the environment works.
An interactive orderbook viewer is available to explore orderbook states:
# Load from default location (data/events/ob-train.tsv)
python orderbook_ui.py
# Load from a specific file
python orderbook_ui.py data/events/ob-test.tsvFeatures:
- Exchange-style UI: Dark theme with color-coded bids (green) and asks (red)
- Interactive navigation: Use the slider or arrow keys (←/→) to traverse through timestamps
- Real-time display: Shows best bid/ask, spread, mid price, and cumulative depth
- Best levels highlighted: Top of book prices are highlighted for easy identification
The UI displays up to 20 levels on each side and updates in real-time as you navigate through different timestamps, allowing you to see how the orderbook evolves over time.
Edit agent_sb3.py to change:
nrTrain = 100000 # Number of training steps
nrTest = 10 # Number of test episodes
model_name = "dqn_ctc_executioner_sb3" # Model save nameReplace the artificial orderbook with real data:
# Instead of:
orderbook.createArtificial(config)
# Use:
orderbook.loadFromEvents('data/events/ob-train.tsv')The LSTM feature extractor can be modified in the LSTMFeatureExtractor class:
class LSTMFeatureExtractor(nn.Module):
def __init__(self, observation_space, features_dim=512):
# Change features_dim to adjust LSTM hidden size
...Models are automatically saved after training. To load an existing model:
model = load_sb3_model("dqn_ctc_executioner_sb3", env)Models are saved in the models/ directory:
models/dqn_ctc_executioner_sb3.zip- Full model (policy + replay buffer)
Solution:
pip install stable-baselines3Solution: The environment is wrapped. Use env.unwrapped:
unwrapped_env = env.unwrapped if hasattr(env, 'unwrapped') else env
unwrapped_env.setOrderbook(orderbook)Solution: Either:
- Use artificial data (already configured in
agent_sb3.py) - Download/place data files in
data/events/ - Modify the file path in the script
Solution:
- Reduce
nrTrainfor testing - Use GPU if available (Stable-Baselines3 will automatically use it if PyTorch CUDA is installed)
- Reduce
buffer_sizein model creation
When done:
deactivateComprehensive documentation and concepts explained in the academic report
For hands-on documentation and examples see Wiki
Load orderbooks
orderbook = Orderbook()
orderbook.loadFromEvents('data/example-ob-train.tsv')
orderbook.summary()
orderbook.plot(show_bidask=True)
orderbook_test = Orderbook()
orderbook_test.loadFromEvents('data/example-ob-test.tsv')
orderbook_test.summary()Create and configure environments
import gymnasium as gym
import gym_ctc_executioner
env = gym.make("ctc-executioner-v0")
env.setOrderbook(orderbook)
env_test = gym.make("ctc-executioner-v0")
env_test.setOrderbook(orderbook_test)