| Crates.io | bts-rs |
| lib.rs | bts-rs |
| version | 1.0.11 |
| created_at | 2025-12-01 23:30:03.665049+00 |
| updated_at | 2025-12-23 22:20:46.224791+00 |
| description | Backtest your trading strategy. |
| homepage | |
| repository | https://github.com/raonagos/bts-rs |
| max_upload_size | |
| id | 1960848 |
| size | 229,287 |
BTS is a Rust library designed for backtesting trading strategies on candlestick data. It enables testing technical indicators, custom strategies, and simulating trading performance on historical or generated data.
use std::sync::Arc;
use bts_rs::prelude::*;
use chrono::{DateTime, Duration};
// Candlestick data
let data = vec![
CandleBuilder::builder().open(100.0).high(110.0).low(95.0).close(105.0).open_time(DateTime::default()).close_time(DateTime::default() + Duration::days(1)).volume(1000.0).build().unwrap(),
CandleBuilder::builder().open(105.0).high(115.0).low(100.0).close(110.0).open_time(DateTime::default()).close_time(DateTime::default() + Duration::days(1)).volume(1000.0).build().unwrap(),
];
let candles = Arc::from_iter(data);
// Initialize backtest
let mut bts = Backtest::new(candles, 1000.0, None).unwrap();
// Custom strategy
bts.run(|bt, candle| {
// Example: Buy if closing price > opening price
if candle.close() > candle.open() {
let order = Order::from((OrderType::Market(candle.close()), 1.0, OrderSide::Buy));
bt.place_order(candle, order)?;
}
Ok(())
}).unwrap();
// Results
println!("Final balance: {}", bts.balance());
println!("Number of positions: {}", bts.positions().count());
println!("Number of events: {}", bts.events().count());
See more examples in examples folder.
The backtesting engine automatically calculates the following metrics:
| Metrics | Description |
|---|---|
| Drawdown | Maximum capital decline |
| Profit Factor | Ratio of gross profits to gross losses |
| Sharpe Ratio | Risk-adjusted return measure |
| Win Rate | Percentage of winning trades |
BTS is compatible with popular indicators crates for technical analysis, allowing you to easily integrate your trading strategy.
BTS provides comprehensive error handling for:
The crate includes three main optional features to extend its functionality:
metrics: Exposes the Metrics struct, enabling calculations of key performance indicators such as max drawdown, Sharpe ratio, profit factor, and win rate.optimizer: Provides tools for parameter optimization, allowing you to find the best strategy parameters (e.g., indicator periods, risk-reward ratios) by testing combinations across historical data.draws: Enables integration with the plotters crate to visualize backtest results, including candlestick charts or performance metrics (requires the metrics feature to be enabled).Add BTS to your Cargo.toml:
[dependencies]
bts_rs = "1.0.11"
Then import and use it in your project:
use bts_rs::prelude::*;
Contributions are welcome! See Contributing file to contribute to this project.
This project is licensed under the MIT License.
Generated by Mistral.ai