Crate tradestation

Source
Expand description

TradeStation Rust Client

Crates.io Total Downloads docs.rs

An ergonomic Rust client for the TradeStation API empowering you to build fast, scalable, and production ready trading systems and applications.

§Features

  • 🧮 Accounting: Monitor your risk, positions, balances, order history, and more across multiple accounts.
  • 📈 Market Data: Easily fetch and stream real time and historic market data on thousands of assets and derivatives.
  • ⚡ Execution: Lightning fast trade execution allowing you to place, update, and cancel orders with all kinds of custom configuration.
  • 🧪 Testing: Supports mock testing so you can seamlessly build out testing environments to ensure your trading systems or applications work as expected.

§Install

Use cargo CLI:

cargo install tradestation

Or manually add it into your Cargo.toml:

[dependencies]
tradestation = "0.0.5"

§Usage

For more thorough information, read the docs.

Simple example for streaming bars of trading activity:

use tradestation::{
    responses::MarketData::StreamBarsResp,
    token::{Scope, Token},
    ClientBuilder, Error,
    MarketData::{self, BarUnit},
};

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Build the TradeStation Client
    let mut client = ClientBuilder::new()?
        .credentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")?
        .token(Token {
            access_token: "YOUR_ACCESS_TOKEN".into(),
            refresh_token: "YOUR_REFRESH_TOKEN".into(),
            id_token: "YOUR_ID_TOKEN".into(),
            token_type: String::from("Bearer"),
            scope: vec![
                Scope::OpenId,
                Scope::Profile,
                Scope::ReadAccount,
                Scope::OfflineAccess,
                Scope::MarketData,
            ],
            expires_in: 1200,
        })?
        .build()
        .await?;

    // Build a query to stream Crude Oil Futures
    let stream_bars_query = MarketData::StreamBarsQueryBuilder::new()
        .symbol("CLX25")
        .unit(BarUnit::Minute)
        .interval(240)
        .build()?;

    // Stream bars of trading activity based on the query built above
    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(())
}

§Contributing

There are many ways to contribute like reporting issues, writing documentation, building out new features and abstractions, refactoring to improve on current abstractions, or fixing bugs.

Keep an eye out on open issues :)

Re-exports§

pub use client::Client;
pub use client::ClientBuilder;
pub use error::Error;
pub use market_data as MarketData;
pub use token::Scope;
pub use token::Token;
pub use execution::Route;

Modules§

accounting
client
error
execution
market_data
responses
token