Crate tastytrade

Crate tastytrade 

Source
Expand description

§tastytrade

tastytrade is a Rust client library for the Tastytrade API, providing programmatic access to trading functionality, market data, and account information.

§Features

  • Authentication with Tastytrade accounts
  • Real-time market data streaming via DxFeed
  • Account and positions information
  • Order management (placing, modifying, canceling)
  • Real-time account streaming for balance updates and order status changes

§Usage

use tastytrade::TastyTrade;
use tastytrade::utils::config::TastyTradeConfig;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Login to Tastytrade
     
    let config = TastyTradeConfig::from_env();
    let tasty = TastyTrade::login(&config).await?;
     
    // Get account information
    let accounts = tasty.accounts().await?;
    for account in accounts {
        println!("Account: {}", account.number().0);
         
        // Get positions
        let positions = account.positions().await?;
        println!("Positions: {}", positions.len());
    }
     
    Ok(())
}

§Real-time Data

The library supports real-time data streaming for both market data and account updates using DXLink:

// Create a quote streamer
use tastytrade::{Symbol, TastyTrade};
use tastytrade::utils::config::TastyTradeConfig;
use tastytrade::dxfeed;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = TastyTradeConfig::from_env();
    let tasty = TastyTrade::login(&config)
           .await
           .unwrap();
    let mut quote_streamer = tasty.create_quote_streamer().await?;
    let mut quote_sub = quote_streamer.create_sub(dxfeed::DXF_ET_QUOTE | dxfeed::DXF_ET_GREEKS);

    // Add symbols to subscribe to
    quote_sub.add_symbols(&[Symbol("AAPL".to_string())]);

    // Listen for events
    if let Ok(dxfeed::Event { sym, data }) = quote_sub.get_event().await {
        match data {
            dxfeed::EventData::Quote(quote) => {
                println!("Quote for {}: {}/{}", sym, quote.bid_price, quote.ask_price);
            }
            _ => {}
        }
    }
    Ok(())
}

§Setup Instructions

  1. Clone the repository:
git clone https://github.com/joaquinbejar/tastytrade
cd tastytrade
  1. Build the project:
make build
  1. Run tests:
make test
  1. Format the code:
make fmt
  1. Run linting:
make lint
  1. Clean the project:
make clean
  1. Run the project:
make run
  1. Fix issues:
make fix
  1. Run pre-push checks:
make pre-push
  1. Generate documentation:
make doc
  1. Publish the package:
make publish
  1. Generate coverage report:
make coverage

§CLI Example

This crate also includes a sample CLI application in the tastytrade-cli directory that demonstrates a portfolio viewer with real-time updates.

§Testing

To run unit tests:

make test

To run tests with coverage:

make coverage

§Contribution and Contact

We welcome contributions to this project! If you would like to contribute, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and ensure that the project still builds and all tests pass.
  4. Commit your changes and push your branch to your forked repository.
  5. Submit a pull request to the main repository.

If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:

Joaquín Béjar García

We appreciate your interest and look forward to your contributions!

Re-exports§

pub use api::accounts;
pub use api::base::TastyResult;
pub use api::client::TastyTrade;

Modules§

api
dxfeed
Internal DXFeed types to replace external dxfeed dependency This module contains the essential types and constants needed for quote streaming
prelude
Prelude
streaming
utils
Utils Module

Structs§

ApiError
Represents an error returned by the Tastytrade API.
BriefPosition
Represents a brief overview of a position.
FullPosition
Represents a full position for an account.
LiveOrderRecord
Represents a live order record.
Order
Represents an order to be placed.
OrderBuilder
Builder for Order.
OrderLeg
Represents a leg of an order.
OrderLegBuilder
Builder for OrderLeg.
Symbol
Represents a trading symbol.

Enums§

Action
Represents an order action type.
DxFeedError
Represents errors that can occur during interactions with DxFeed.
InstrumentType
Represents the different types of financial instruments.
OrderType
Represents the type of order being placed.
PriceEffect
Represents the effect of a price on an account.
QuantityDirection
Represents the direction of a quantity, such as a trade or position.
TastyTradeError
Represents errors that can occur within the Tastytrade API client.
TimeInForce
Represents the time-in-force instruction for an order.

Traits§

AsSymbol
Trait for converting types to Symbol.