tradestation-rs

Crates.iotradestation-rs
lib.rstradestation-rs
version0.1.2
created_at2024-09-27 23:51:39.276768+00
updated_at2024-12-29 05:16:25.145419+00
descriptionHigh level, fully featured, and ergonomic Rust client for the TradeStation API.
homepagehttps://crates.io/crates/tradestation-rs
repositoryhttps://github.com/convexity-guild/tradestation-rs
max_upload_size
id1389598
size406,290
Convexity Guild (convexity-guild)

documentation

https://docs.rs/tradestation-rs/latest/tradestation-rs/index.html

README

TradeStation Rust Client

An ergonomic Rust client for the TradeStation API.

This is a fork of the tradestation crate, but more opinionated and providing higher level abstractions.

Install

Use cargo CLI:

cargo install tradestation-rs

Or manually add it into your Cargo.toml:

[dependencies]
tradestation-rs = "0.1.2"

Usage

For more thorough information, read the docs.

Simple example for streaming bars of trading activity:

use tradestation_rs::{
    responses::MarketData::StreamBarsResp,
    ClientBuilder, Error,
    MarketData::{self, BarUnit},
};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let mut client = ClientBuilder::new()?
        .set_credentials("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")?
        .authorize("YOUR_AUTHORIZATION_CODE")
        .await?
        .build()
        .await?;
    println!("Your TradeStation API Bearer Token: {:?}", client.token);

    let stream_bars_query = MarketData::StreamBarsQueryBuilder::new()
        .set_symbol("CLX24")
        .set_unit(BarUnit::Minute)
        .set_interval("240")
        .build()?;

    let streamed_bars = client
        .stream_bars(&stream_bars_query, |stream_data| {
            match stream_data {
                StreamBarsResp::Bar(bar) => {
                    // Do something with the bars like making a chart
                    println!("{bar:?}")
                }
                StreamBarsResp::Heartbeat(heartbeat) => {
                    if heartbeat.heartbeat > 10 {
                        return Err(Error::StopStream);
                    }
                }
                StreamBarsResp::Status(status) => {
                    println!("{status:?}");
                }
                StreamBarsResp::Error(err) => {
                    println!("{err:?}");
                }
            }

            Ok(())
        })
        .await?;

    // All the bars collected during the stream
    println!("{streamed_bars:?}");

    Ok(())
}
Commit count: 86

cargo fmt