A Rust client library for accessing the ThetaData REST API v3.
- Simple async API for ThetaData REST endpoints
- Configurable base URL (defaults to
http://localhost:25503/v3) - Built-in error handling and type safety
- Support for historical stock, options, and index data
- Comprehensive data types for market data
Add this to your Cargo.toml:
[dependencies]
theta-data-rs = "0.1.0"use theta_data_rs::{
ThetaDataClient,
api::{OptionEodQuery, ExpirationQuery, StrikeQuery, RightQuery}
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client with default configuration
let client = ThetaDataClient::new()?;
// Query options EOD data for AAPL
let options_data = client.query_option_eod_history(OptionEodQuery {
symbol: "AAPL".to_string(),
start_date: "2024-01-01".parse()?,
end_date: "2024-01-31".parse()?,
expiration: ExpirationQuery::All,
strike: StrikeQuery::All,
right: RightQuery::Both,
}).await?;
println!("Retrieved {} option EOD records", options_data.len());
// Print the first record as an example
if let Some(first_option) = options_data.first() {
println!("First option: {} {} {} strike={} open={} close={} volume={}",
first_option.symbol,
first_option.expiration,
first_option.right,
first_option.strike,
first_option.open,
first_option.close,
first_option.volume);
}
Ok(())
}By default, the client connects to http://localhost:25503/v3:
let client = ThetaDataClient::new()?;You can customize the base URL and timeout:
use theta_data_rs::{ThetaDataClient, Config};
let config = Config::new("http://your-server:8080/v3")
.with_timeout(60); // 60 seconds timeout
let client = ThetaDataClient::with_config(config)?;Query end-of-day options data with flexible filtering:
use theta_data_rs::api::{
OptionEodQuery, ExpirationQuery, StrikeQuery, RightQuery
};
// Get all options for AAPL
let options_data = client.query_option_eod_history(OptionEodQuery {
symbol: "AAPL".to_string(),
start_date: "2024-01-01".parse()?,
end_date: "2024-01-31".parse()?,
expiration: ExpirationQuery::All,
strike: StrikeQuery::All,
right: RightQuery::Both,
}).await?;
// Filter by specific expiration date
let options_data = client.query_option_eod_history(OptionEodQuery {
symbol: "AAPL".to_string(),
start_date: "2024-01-01".parse()?,
end_date: "2024-01-31".parse()?,
expiration: ExpirationQuery::Date("2024-02-16".parse()?),
strike: StrikeQuery::All,
right: RightQuery::Call, // Only call options
}).await?;
// Filter by specific strike price
let options_data = client.query_option_eod_history(OptionEodQuery {
symbol: "AAPL".to_string(),
start_date: "2024-01-01".parse()?,
end_date: "2024-01-31".parse()?,
expiration: ExpirationQuery::All,
strike: StrikeQuery::Value(rust_decimal::Decimal::from(150)), // $150 strike
right: RightQuery::Put, // Only put options
}).await?;use theta_data_rs::api::StockQuery;
let stock_data = client.query_stock_eod_history(StockQuery {
symbol: "AAPL".to_string(),
start_date: "2023-01-01".parse()?,
end_date: "2023-12-31".parse()?,
}).await?;use theta_data_rs::api::IndexQuery;
let index_data = client.query_index_eod_history(IndexQuery {
symbol: "SPX".to_string(),
start_date: "2023-01-01".parse()?,
end_date: "2023-12-31".parse()?,
}).await?;The library provides typed structs for all API responses:
OptionEodData- End-of-day options data including OHLC, volume, and bid/ask
StockEodData- End-of-day stock data including OHLC, volume, and bid/ask
IndexEodData- End-of-day index data including OHLC, volume, and bid/ask
Exchange- Enum representing different market exchangesThetaDataError- Error types for API operations
The library uses a custom ThetaDataError enum that wraps common error types:
use theta_data_rs::{ThetaDataError, Result, api::OptionEodQuery};
match client.query_option_eod_history(OptionEodQuery {
symbol: "INVALID".to_string(),
start_date: "2024-01-01".parse()?,
end_date: "2024-01-31".parse()?,
expiration: theta_data_rs::api::ExpirationQuery::All,
strike: theta_data_rs::api::StrikeQuery::All,
right: theta_data_rs::api::RightQuery::Both,
}).await {
Ok(data) => println!("Options data: {} records", data.len()),
Err(ThetaDataError::Http(e)) => println!("HTTP error: {}", e),
Err(ThetaDataError::Api { message }) => println!("API error: {}", message),
Err(e) => println!("Other error: {}", e),
}Check the examples/ directory for more comprehensive usage examples:
cargo run --example basic_usage- ThetaData server running locally or accessible via network
Licensed under either of
Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.