Expand description
§BTS: BackTest Strategy for Trading Algorithms
BTS is a high-performance Rust library for backtesting trading strategies on candlestick (OHLCV) data. It is designed for speed, flexibility, and accuracy, making it ideal for both retail traders and algorithmic trading developers.
§Why BTS?
- Optimized for Performance: Uses O(1) operations on orders/positions, and parallel processing for optimization tasks.
- Technical Analysis Ready: Seamlessly integrates with popular indicators crates for 100+ indicators (EMA, MACD, RSI, etc.).
- Risk Management: Supports stop-loss, take-profit, and trailing stops with configurable rules.
- Realistic Simulations: Models slippage, fees, and latency for accurate backtesting.
- Extensible: Add custom indicators, strategies, or data sources with minimal effort.
§Core Components
| Component | Description |
|---|---|
Metrics | Calculates performance metrics: P&L, drawdown, Sharpe ratio, win rate, and more. |
Optimizer | Calculates bests parameters (indicators, RR, etc…). |
Draw | Draw candlestick data, balance and metrics to a SVG or PNG file. |
Backtest | The engine that simulates strategy execution over historical data. |
§Features
§1. Technical Indicators
- Compatible with indicators crates like the
tacrate for 100+ additional indicators.
§2. Order Types & Exit Rules
| Order Type | Description |
|---|---|
| Market Order | Executes immediately at the current price. |
| Limit Order | Executes only at a specified price or better. |
| Take-Profit | Closes the position when a target price is reached. |
| Stop-Loss | Closes the position to limit losses. |
| Trailing Stop | Dynamically adjusts the stop price based on market movements. |
| Take-Profit + Stop-Loss | Combines both rules for risk management. |
§3. Performance Metrics
| Metric | Description |
|---|---|
| Max Drawdown | Largest peak-to-trough decline in account balance (%). |
| Profit Factor | Ratio of gross profits to gross losses. |
| Sharpe Ratio | Risk-adjusted return (higher = better). |
| Win Rate | Percentage of winning trades. |
§4. Optimization Tools
- Parallelize: Optimize strategy parameters (e.g., EMA periods) using multi-threading.
§Getting Started
§1. Add BTS to your project:
[dependencies]
bts_rs = "1.0.11"
ta = "0.5.0" # Optional: For technical analysis indicators§2. Run a Simple Backtest:
use std::sync::Arc;
use bts_rs::prelude::*;
use chrono::{DateTime, Duration};
let candle = CandleBuilder::builder()
.open(100.0)
.high(110.0)
.low(95.0)
.close(105.0)
.volume(1.0)
.bid(0.5)
.open_time(DateTime::default())
.close_time(DateTime::default() + Duration::days(1))
.build()
.unwrap();
// Initialize backtest with \$10,000
let mut backtest = Backtest::new(Arc::from_iter(vec![candle]), 10_000.0, None).unwrap();
// Execute a market buy order
backtest
.run(|bt, candle| {
let order: Order = (OrderType::Market(102.0), 1.0, OrderSide::Buy).into();
bt.place_order(candle, order).unwrap();
// Close the position at \$104.0
if let Some(position) = bt.positions().last().cloned() {
bt.close_position(candle, &position, 104.0, true).unwrap();
}
Ok(())
})
.unwrap();
// Print performance metrics
#[cfg(feature = "metrics")]
{
let metrics = Metrics::from(&backtest);
println!("{}", metrics);
}§Output:
=== Backtest Metrics ===
Initial Balance: 10000.00
Final Balance: 10018.00
Profit & Loss (P&L): 0.00
Fees paid: 0.00
Max Drawdown: 0.20%
Profit Factor: 2.00
Sharpe Ratio: 1.50
Win Rate: 100.00%§Use Cases
- Retail Traders: Test manual strategies before risking real capital.
- Algo Developers: Build and optimize automated trading systems.
- Quant Researchers: Backtest statistical arbitrage or machine learning models.
- Educational: Teach trading concepts with a hands-on tool.
§Integrations
| Crate | Purpose |
|---|---|
rayon | Parallel processing for optimization. |
serde | Serialize/deserialize backtest results. |
plotters | Visualize market candlesticks data, equity curves and indicators. |
§Error Handling
BTS uses custom error types to handle:
- Insufficient balance.
- Invalid order types.
- Missing data (e.g., candles, positions) and more.
Example:
use std::sync::Arc;
use bts_rs::prelude::*;
use chrono::{DateTime, Duration};
let candle = CandleBuilder::builder()
.open(100.0)
.high(110.0)
.low(95.0)
.close(105.0)
.volume(1.0)
.bid(0.5)
.open_time(DateTime::default())
.close_time(DateTime::default() + Duration::days(1))
.build()
.unwrap();
// Initialize backtest with \$10,000
let mut backtest = Backtest::new(Arc::from_iter(vec![candle]), 10_000.0, None).unwrap();
// Execute a market buy order
backtest
.run(|bt, candle| {
let order: Order = (OrderType::Market(102.0), 1.0, OrderSide::Buy).into();
match bt.place_order(candle, order) {
Ok(_) => println!("Order in the pool!"),
Err(_) => eprintln!("Error to place an order")
}
Ok(())
})
.unwrap();§Contributing
Contributions are welcome! See CONTRIBUTING.md.
§License
The project is licensed under the MIT.
Modules§
- draws
- Module for visualizing backtest results and candle charts. Module for visualizing backtest results and candle charts.
- engine
- Core trading engine components: orders, positions, wallet, and backtest logic. Core trading engine components.
- errors
- Error types for the library. Error types for the BTS library.
- metrics
- Performance metrics: drawdown, Sharpe ratio, win rate, etc. Performance metrics for backtesting.
- optimizer
- Strategy parameter optimization. Strategy parameter optimization.
- prelude
- Re-exports of commonly used types and traits for convenience.
Traits§
- Percent
Calculus - Trait for performing percentage-based calculations (human readable).