A simple, asynchronous SDK for Tastytrade's public API. This will allow you to create trading algorithms for whatever strategies you may have quickly and painlessly in Python.
Features
Up to 10x less code than using the API directly
Powerful websocket implementation for account alerts and data streaming
100% typed, with Pydantic models for all JSON responses from the API
95%+ unit test coverage
Comprehensive documentation
Utility functions for timezone calculations, futures monthly expiration dates, and more
Proprietary paper API for reliable testing
[!TIP]
Want to see the SDK in action? Check out tastytrade-cli, a CLI for Tastytrade that showcases many of the SDK's features.
Getting started
You can install via pip/uv:
$ pip install tastytrade
You can also spin up a new project template with agent integration, helpful utilities and documentation:
$ uvx --from tastytrade-cli bootstrap my_project
Creating a session
A session object is required to authenticate your requests to the Tastytrade API. See here for information on how to set up an OAuth application.
from tastytrade import Session
session = Session('client_secret', 'refresh_token')
Using the streamer
The streamer is a websocket connection to dxfeed (the Tastytrade data provider) that allows you to subscribe to real-time data for quotes, greeks, and more.
from tastytrade import DXLinkStreamer
from tastytrade.dxfeed import Quote
async with DXLinkStreamer(session) as streamer:
subs_list = ['SPY'] # list of symbols to subscribe to
await streamer.subscribe(Quote, subs_list)
# fetch a single quote
quote = await streamer.get_event(Quote)
print(quote)
# or multiple quotes...
async for quote in streamer.listen(Quote):
print(quote)
from decimal import Decimal
from tastytrade import Account
from tastytrade.instruments import Equity
from tastytrade.order import LimitOrder, OrderAction
account = await Account.get(session, '5WX01234')
symbol = await Equity.get(session, 'USO')
leg = symbol.build_leg(5, OrderAction.BUY_TO_OPEN) # buy to open 5 shares
order = LimitOrder(
legs=[leg], # you can have multiple legs in an order
price=Decimal('-10') # limit price, $10/share debit for a total value of $50
)
response = await account.place_order(session, order, dry_run=True) # a test order
print(response)