Unofficial rust client for connecting to Rithmic's R | Protocol API.
rithmic protocol version: 0.84.0.0
Not all functionality has been implemented, but this is currently being used to trade live capital through Rithmic.
Only order_plant, ticker_plant, pnl_plant, history_plant are provided. Each plant uses the actor pattern so you'll want to start a plant, and communicate / call commands with it using it's handle. The crate is setup to be used with tokio channels.
The history_plant supports loading both historical tick data and time bar data (1-second, 1-minute, 5-minute, daily, and weekly bars).
You can install it from crates.io
$ cargo add rithmic-rs
Or manually add it to your Cargo.toml file.
[dependencies]
rithmic-rs = "0.5.1"
Version 0.5.0 introduces breaking changes for improved stability and error handling:
- Plant constructors changed from
new()toconnect()with explicit connection strategies - New unified
RithmicConfigAPI replaces separateAccountInfotypes - Heartbeat errors and forced logout events now delivered through subscription channel
📖 See MIGRATION_0.5.0.md for step-by-step migration guide with code examples.
Also see CHANGELOG.md for complete list of changes.
Rithmic supports three types of account environments: RithmicEnv::Demo for paper trading, RithmicEnv::Live for funded accounts, and RithmicEnv::Test for the test environment before app approval.
Configure your connection using the builder pattern:
use rithmic_rs::{RithmicConfig, RithmicEnv};
let config = RithmicConfig::builder()
.user("your_username".to_string())
.password("your_password".to_string())
.system_name("Rithmic Paper Trading".to_string())
.env(RithmicEnv::Demo)
.build()?;Alternatively, you can load from environment variables using from_env():
let config = RithmicConfig::from_env(RithmicEnv::Demo)?;Required environment variables for from_env():
# For Demo environment
RITHMIC_DEMO_USER=your_username
RITHMIC_DEMO_PW=your_password
# For Live environment
RITHMIC_LIVE_USER=your_username
RITHMIC_LIVE_PW=your_password
# For Test environment
RITHMIC_TEST_USER=your_username
RITHMIC_TEST_PW=your_passwordNote: The
dotenvdependency will become optional in version 0.6.0. The builder pattern is recommended for new projects.
The library provides three connection strategies:
Simple: Single connection attempt (recommended default, fast-fail)Retry: Indefinite retries with exponential backoff capped at 60 secondsAlternateWithRetry: Alternates between primary and beta URLs with retries
To use this crate, create a configuration and connect to a plant with your chosen strategy. Each plant uses the actor pattern and spawns a task that listens to commands via a handle. Plants like the ticker plant also include a broadcast channel for real-time updates.
use rithmic_rs::{RithmicConfig, RithmicEnv, ConnectStrategy, RithmicTickerPlant};
async fn stream_live_ticks() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from environment variables
let config = RithmicConfig::from_env(RithmicEnv::Demo)?;
// Connect with retry strategy (indefinite retries with 60s max backoff)
let ticker_plant = RithmicTickerPlant::connect(&config, ConnectStrategy::Retry).await?;
let handle = ticker_plant.get_handle();
// Login and subscribe
handle.login().await?;
handle.subscribe("ESU5", "CME").await?;
// Process real-time updates
loop {
match handle.subscription_receiver.recv().await {
Ok(update) => {
// IMPORTANT: Check for connection health issues
if let Some(error) = &update.error {
eprintln!("Error from {}: {}", update.source, error);
// Handle heartbeat errors - may indicate connection degradation
if matches!(update.message, RithmicMessage::ResponseHeartbeat(_)) {
eprintln!("Heartbeat error - connection may be degraded");
// Implement reconnection logic here
break;
}
}
// Handle forced logout events
if matches!(update.message, RithmicMessage::ForcedLogout(_)) {
eprintln!("Forced logout - must reconnect");
break;
}
// Handle connection errors (new in 0.5.1)
if matches!(update.message, RithmicMessage::ConnectionError) {
eprintln!("Connection error from {}: {}",
update.source,
update.error.unwrap_or_else(|| "Unknown error".to_string())
);
// Plant has stopped, channel will close
// Implement reconnection logic here
break;
}
// Process market data
match update.message {
RithmicMessage::LastTrade(trade) => {
println!("Trade: {} @ {}", trade.trade_size.unwrap(), trade.trade_price.unwrap());
}
RithmicMessage::BestBidOffer(bbo) => {
println!("BBO: {}@{} / {}@{}",
bbo.bid_size.unwrap(), bbo.bid_price.unwrap(),
bbo.ask_price.unwrap(), bbo.ask_size.unwrap());
}
_ => {}
}
}
Err(e) => {
eprintln!("Channel error: {}", e);
break;
}
}
}
Ok(())
}The repository includes several examples to help you get started:
Environment Variables
Before running examples, copy .env.blank to .env and fill in your credentials:
cp examples/.env.blank .env
# Edit .env with your Rithmic credentialscargo run --example connect# Load historical tick data
cargo run --example load_historical_ticks
# Load historical time bars (1-minute, 5-minute, daily)
cargo run --example load_historical_barsContributions encouraged and welcomed!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.