#trading #quant #api-client

tradestation

An ergonomic Rust client for the TradeStation API

4 releases

0.0.4 Nov 10, 2024
0.0.3 Nov 2, 2024
0.0.2 Oct 8, 2024
0.0.1 Sep 28, 2024

#77 in Finance

Download history 127/week @ 2024-09-23 47/week @ 2024-09-30 194/week @ 2024-10-07 4/week @ 2024-10-14 143/week @ 2024-10-28 18/week @ 2024-11-04 102/week @ 2024-11-11 6/week @ 2024-11-18 12/week @ 2024-12-02

126 downloads per month

MIT license

335KB
3.5K SLoC

TradeStation Rust Client

An ergonomic Rust client for the TradeStation API.

Install

Use cargo CLI:

cargo install tradestation

Or manually add it into your Cargo.toml:

[dependencies]
tradestation = "0.0.4"

Usage

For more thorough information, read the docs.

Simple example for streaming bars of trading activity:

use tradestation::{
    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(())
}

Dependencies

~7–18MB
~234K SLoC