Skip to content

seurimas/theta-data-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThetaData Rust API Client

A Rust client library for accessing the ThetaData REST API v3.

Features

  • 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

Installation

Add this to your Cargo.toml:

[dependencies]
theta-data-rs = "0.1.0"

Quick Start

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(())
}

Configuration

Default Configuration

By default, the client connects to http://localhost:25503/v3:

let client = ThetaDataClient::new()?;

Custom Configuration

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)?;

API Methods

Options Historical Data

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?;

Stock Historical Data

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?;

Index Historical Data

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?;

Data Types

The library provides typed structs for all API responses:

Options Data

  • OptionEodData - End-of-day options data including OHLC, volume, and bid/ask

Stock Data

  • StockEodData - End-of-day stock data including OHLC, volume, and bid/ask

Index Data

  • IndexEodData - End-of-day index data including OHLC, volume, and bid/ask

Common Types

  • Exchange - Enum representing different market exchanges
  • ThetaDataError - Error types for API operations

Error Handling

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),
}

Examples

Check the examples/ directory for more comprehensive usage examples:

cargo run --example basic_usage

Requirements

  • ThetaData server running locally or accessible via network

License

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.

About

A simple rust crate for accessing ThetaData.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages