Giter VIP home page Giter VIP logo

tastytrade-rs's Introduction

tastytrade-rs

Rust library for stock market trading through tastytrade's API. Very much work in progress. See tastytrade-cli for an example of a more involved application using this library.

Example

    let tasty = TastyTrade::login("username", "password", false)
        .await
        .unwrap();

    let account = tasty.account("ABC12345")
        .await
        .unwrap()
        .unwrap();
    println!("{:#?}", account.balance().await);
    println!("{:#?}", account.positions().await);
    println!("{:#?}", account.live_orders().await);

    let order_leg = OrderLegBuilder::default()
        .instrument_type(InstrumentType::Equity)
        .symbol("AAPL")
        .quantity(1u64)
        .action(Action::BuyToOpen)
        .build()
        .unwrap();

    let order = OrderBuilder::default()
        .time_in_force(TimeInForce::GTC)
        .order_type(OrderType::Limit)
        .price(dec!(170.0))
        .price_effect(PriceEffect::Debit)
        .legs(vec![order_leg])
        .build()
        .unwrap();

    let dry_result = account.dry_run(&order).await;

    println!("{dry_result:#?}");
    // Outputs:
    // DryRunResult {
    //     order: DryRunRecord {
    //         account_number: AccountNumber(
    //             "ABC12345",
    //         ),
    //         time_in_force: GTC,
    //         order_type: Limit,
    //         size: 1,
    //         underlying_symbol: Symbol(
    //             "AAPL",
    //         ),
    //         price: 170.0,
    //         price_effect: Debit,
    //         status: Received,
    //         cancellable: true,
    //         editable: true,
    //         edited: false,
    //         legs: [
    //             OrderLeg {
    //                 instrument_type: Equity,
    //                 symbol: Symbol(
    //                     "AAPL",
    //                 ),
    //                 quantity: 1,
    //                 action: BuyToOpen,
    //             },
    //         ],
    //     },
    //     warnings: [],
    //     buying_power_effect: BuyingPowerEffect {
    //         change_in_margin_requirement: 85.0,
    //         change_in_margin_requirement_effect: Debit,
    //         change_in_buying_power: 85.001,
    //         change_in_buying_power_effect: Debit,
    //         current_buying_power: 562.5,
    //         current_buying_power_effect: Credit,
    //         impact: 85.001,
    //         effect: Debit,
    //     },
    //     fee_calculation: FeeCalculation {
    //         total_fees: 0.001,
    //         total_fees_effect: Debit,
    //     },
    // },

tastytrade-rs's People

Contributors

mchsl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

tastytrade-rs's Issues

DXLink to DXFeed

I think the Tastytrade API migration from DXFeed to DXLink has affected the streamer URL endpoint.

There was an issue with deserializing the get request to "/quote-streamer-tokens", because Tastytrade (or dxfeed?) changed their endpoint response for the streamer URL. I modified the QuoteStreamerTokens struct as such:

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct QuoteStreamerTokens {
    pub token: String,
    pub dxlink_url: String,
    pub websocket_url: String,
    pub level: String,
}

So that it could read the GET response correctly (specifically taking dxlink-url value which is now "wss://tasty-live-ws.dxfeed.com/realtime").

The original "websocket-url" does not work anymore; I checked the unofficial python SDK for the tastytrade API to get the DXLinkStreamer URL (people using that library have had to switch from DXFeed to DXStreamer).

Then inside QuoteStreamer, I modify the host to take tokens.dxlink_url:

impl QuoteStreamer {
    pub async fn connect(tasty: &TastyTrade) -> Result<QuoteStreamer> {
        let tokens = tasty.quote_streamer_tokens().await?;
        let mut this = Self {
            host: tokens.dxlink_url,
            token: tokens.token,

            connection: std::ptr::null_mut(),
            subscriptions: HashMap::new(),
            next_sub_id: 0,
        };

And I changed the Event type listening to be TRADE instead of TIME_AND_SALE (as DXLink only has Trade events, no t&s). But now when it attempts to connect to dxfeed using their provided dxlink-url, it just hangs at:

        assert_eq!(SUCCESS, unsafe {
            dxfeed::dxf_create_connection_auth_bearer(
                c_host.as_ptr(), // const char* address,
                c_token.as_ptr(),
                Some(Self::termination_listener), // dxf_conn_termination_notifier_t notifier,
                Some(Self::sub_listener), // dxf_conn_status_notifier_t conn_status_notifier,
                None,                     // dxf_socket_thread_creation_notifier_t stcn,
                None,                     // dxf_socket_thread_destruction_notifier_t stdn,
                std::ptr::null_mut(),     // void* user_data,
                &mut this.connection,     // OUT dxf_connection_t* connection);
            )
        });
        println!("Connected to {}", this.host);

The connection never succeeds (or fails). I also tried using the "websocket-url" from the get response but that also hangs.

Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.