The Backtest system is a comprehensive framework for simulating trading algorithms using historical market data. It supports traditional algorithmic trading strategies as well as reinforcement learning approaches, with powerful configuration options, performance metrics, and visualization capabilities.
Backtest configurations are defined in JSON files with two main sections:
backtest: Defines simulation parametersalgorithm(legacy): Defines one algorithm and its parametersalgorithms(new): Defines multiple algorithms to run simultaneously
Example:
{
"backtest": {
"startDate": "20250407 9:00:00",
"endDate": "20250407 12:00:00",
"instrument": "btceur_kraken",
"delayOrderMs": 0,
"feesCommissionsIncluded": false,
"multithreadConfiguration": "single_thread"
},
"algorithm": {
"algorithmName": "AvellanedaStoikov_test",
"parameters": {
"riskAversion": 0.00006,
"quantity": 0.001,
"firstHour": 7.0,
"lastHour": 19.0
// other algorithm-specific parameters
}
}
}Multiple algorithms example:
{
"backtest": {
"startDate": "20250407 9:00:00",
"endDate": "20250407 12:00:00",
"delayOrderMs": 0,
"feesCommissionsIncluded": false,
"multithreadConfiguration": "single_thread"
},
"algorithms": [
{
"algorithmName": "AvellanedaStoikov_test",
"parameters": {
"instrumentPks": ["btceur_kraken"],
"quantity": 0.001
}
},
{
"algorithmName": "ConstantSpread_test",
"parameters": {
"instrumentPks": ["btceur_kraken"],
"quantity": 0.001
}
}
]
}startDate/endDate: Time range for simulation (format: "YYYYMMDD HH:MM:SS")instrument: Trading instrument identifier (e.g., "btceur_kraken")delayOrderMs: Simulated order processing delay in millisecondsfeesCommissionsIncluded: Whether to include trading fees in P&L calculationsmultithreadConfiguration: Threading model ("single_thread" or "multi_thread")bucleRun: Run in loop mode (for continuous backtesting)
algorithmName: Name of algorithm to instantiateparameters: Map of algorithm-specific parameters- Common parameters include:
quantity: Trading sizefirstHour/lastHour: Operating hours (UTC)ui: Enable visualization (1=on, 0=off)
- Common parameters include:
App.javaloads the JSON configuration- Configuration is parsed into
InputConfigurationobjects BacktestConfigurationis created with algorithm and market data settings- The backtest engine is initialized with the configuration
- Historical market data is loaded from parquet files (from
DATA_PATH) - Data is fed to the algorithm in chronological order
- Algorithm processes market events and generates orders
- Simulated execution is applied and execution reports returned
- P&L and positions are tracked throughout the simulation
- When
endDateis reached, backtest is marked as complete - Summary statistics are calculated and displayed
- Trade data and performance metrics are saved to output files
- Optional visualization of results is presented
RL algorithms require additional parameters:
dummyAgent: Enable dummy agent mode for testing (1=on, 0=off)reinforcementLearningActionType: "discrete" or "continuous"rlHost/rlPort: ZeroMQ connection parametersstateColumnsFilter: Features to include in state representationactionColumns: Number of action dimensions
The backtest communicates with RL agents through ZeroMQ:
- Agent receives states from the environment
- Agent sends actions to the environment
- Environment returns rewards and next states
- Process continues until terminal state is reached
For testing, the framework includes a DummyRlAgent that:
- Connects to the backtest via ZeroMQ
- Generates random actions
- Receives state/reward data
- Simulates an RL agent without actual learning
- Trade execution details
- Position updates
- P&L snapshots
- Market data statistics
- Trade tables: CSV files with all executed trades
- P&L snapshots: Performance at different time points
- Summary statistics: Overall performance metrics
When enabled (ui: 1):
- Price charts with trade markers
- P&L evolution over time
- Position changes
- Custom metrics visualization
java -jar backtest.jar path/to/config.json- Extend the
Algorithmclass - Implement required methods like
onDepthUpdate,onTradeUpdate - Define algorithm parameters
- Create configuration JSON with algorithm name and parameters
- Add it to TradingAlgorithmsProvider.java
Results are saved to the OUTPUT_PATH directory:
trades_table_{algorithmName}_{instrument}.csv: All trades- Summary statistics are printed to console and logs
- Configure backtest with RL algorithm and parameters
- Connect RL agent to backtest through ZeroMQ
- Run backtest with
dummyAgent: 0 - Agent receives states and rewards, learns policy
- Save trained model
- Load trained model
- Configure backtest with same parameters
- Run backtest with loaded model
- Analyze performance metrics
- The backtest uses a simulated clock that advances with market data timestamps
- Position tracking is automatic but can be overridden
- Market data is immutable once loaded
- Order execution is simulated based on available liquidity
- Fees and commissions are optional and configurable
- Trading hours can be restricted using
firstHourandlastHour