10 releases (1 stable)
Uses new Rust 2024
| 1.0.0 | Feb 3, 2026 |
|---|---|
| 0.1.8 | Oct 4, 2023 |
| 0.1.7 | Sep 13, 2023 |
| 0.1.5 | Aug 28, 2023 |
#1 in #gym
1MB
23K
SLoC
Chapaty
Chapaty is a high-performance, async-first Rust library for training and evaluating reinforcement learning agents in financial environments. Inspired by OpenAI Gymnasium, Chapaty brings the rigor of standardized simulation interfaces to real-world financial markets.
Getting Started
Chapaty supports two primary workflows: High-Performance Backtesting for evaluating strategy grids, and Standard Reinforcement Learning for training agents step-by-step.
1. High-Performance Backtesting (1M+ Agents)
For massive grid searches, Chapaty leverages rayon to evaluate millions of agents in parallel, automatically tracking the top performers without memory overhead.
Run this example: examples/news_breakout_grid.rs
use std::path::Path;
use chapaty::prelude::*;
use polars::prelude::CsvWriterOptions;
#[tokio::main]
async fn main() -> Result<()> {
// 1. Initialize the environment (configure data sources, date ranges, filters)
// See the full example file for the 'environment()' helper implementation.
let mut env = environment().await?;
// 2. Generate the Agent Grid
// Creates a lazy parallel iterator of 1,000,000+ distinct parameter combinations.
// This allows efficient streaming without loading all agents into RAM.
let (count, agents) = news_breakout_grid();
println!("Evaluating {count} agents...");
// 3. Execute Parallel Evaluation
// Chapaty manages the batching and threading, retaining the Top-100 agents
let leaderboard = env.evaluate_agents(agents, 100, count as u64)?;
// 4. Export the Leaderboard
// Results are saved as a structured CSV dataset.
leaderboard.to_csv(Path::new("examples/reports/news_breakout"), &CsvWriterOptions::default())?;
Ok(())
}
2. Custom Reinforcement Learning (Gym Interface)
For researchers needing fine-grained control over the observation-action loop, Chapaty implements a standard API similar to OpenAI Gym.
use std::path::Path;
use chapaty::prelude::*;
#[tokio::main]
async fn main() -> ChapatyResult<()> {
// Initialize the environment
let mut env = chapaty::make(EnvPreset::BtcUsdtEod).await?;
// Reset the environment to generate the first observation
let (mut obs, mut reward, mut outcome) = env.reset()?;
while !outcome.is_done() {
// this is where you would insert your policy
let actions = obs.action_space().sample()?;
// step (transition) through the environment with the actions
// receiving the next observation, reward and if the episode has terminated
(obs, reward, outcome) = env.step(actions)?;
// If the episode has ended then we can reset to start a new episode
if outcome.is_terminal() {
// optionally use the final observation and outcome to bootstrap reward
drop(obs);
(obs, reward, outcome) = env.reset()?;
}
}
// Explicitly drop 'obs' to release the borrow on 'env'.
// We cannot call 'env.journal()' while 'obs' is still active.
drop(obs);
// Extract the trading journal (ledger) for post-simulation analysis.
let journal = env.journal()?;
// Save the journal to a directory (the filename is handled internally).
journal.to_csv(Path::new("chapaty/reports"), None, None)?;
Ok(())
}
Note: Environments are async because they stream large financial datasets from cloud storage (e.g. GCS, BigQuery, S3, etc.).
For practical, ready-to-run agents, check out the examples to get started quickly.
Related Projects
| Project | Description |
|---|---|
| Gymnasium | RL API standard for Python environments |
| DeepMind Control Suite | Physics-based simulation and RL environments |
| Burn | Deep learning framework in Rust |
Chapaty Platform
To access hosted market data, simply log in at chapaty.com to obtain an API key. End-of-day OHLCV data is free to use. Prefer your own data? You can bring your own market data and start using Chapaty at no cost.
Disclaimer
Trading and investing involve substantial risk. You may lose some or all of your capital.
Chapaty is an open-source software project provided for research and educational purposes only. It does not constitute financial, investment, legal, or trading advice.
This software is provided “AS IS”, without warranties or conditions of any kind, express or implied, as stated in the Apache License, Version 2.0. The software may contain bugs, errors, or inaccuracies.
In no event shall the authors or contributors be liable for any direct or indirect losses, damages, or consequences, including but not limited to financial losses, arising from the use of this software.
By using Chapaty, you acknowledge that you are solely responsible for any trading decisions, strategies, or outcomes.
Dependencies
~72–96MB
~1.5M SLoC