Getting Started
Before using this SDK, ensure that you:
- have a Tastytrade account
- have opted into the Tastytrade Open API program
- Have a set of OAuth2 client credentials and a refresh token for your personal OAuth2 app. See Tastytrade OAuth2 for more details.
Install
pip install tastytrade-sdk
Use It
from tastytrade_sdk import Tastytrade
tasty = Tastytrade(
client_id = YOUR_CLIENT_ID,
client_secret = YOUR_CLIENT_SECRET,
refresh_token = YOUR_REFRESH_TOKEN,
)
tasty.api.post('/sessions/validate')
Examples
Streaming Market Data
from tastytrade_sdk import Tastytrade
# Instead of creating a Tastyworks object manually, you can store the details in the following
# environment variables and call this to create the object automatically. Remember, never store
# your client secret or refresh token directly in code or check it into version control.
# To use the sandbox environment instead, use api.cert.tastyworks.com as the API_BASE_URL and
# be sure to use client credentials generated for that environment.
# API_BASE_URL=api.tastyworks.com
# TT_CLIENT_ID=
# TT_CLIENT_SECRET=
# TT_REFRESH_TOKEN=
tasty = Tastytrade.from_env()
# Subscribing to symbols across different instrument types
# Please note: The option symbols here are expired. You need to subscribe to an unexpired symbol to receive quote data
symbols = [
'BTC/USD',
'SPY',
'/ESU3',
'SPY 230630C00255000',
'./ESU3 EW2N3 230714C4310'
]
subscription = tasty.market_data.subscribe(
symbols=symbols,
on_quote=print,
on_candle=print,
on_greeks=print
)
# start streaming
subscription.open()
API Reference
class
Tastytrade:
The SDK's top-level class
Tastytrade( api_base_url: str = 'api.tastytrade.com', client_id: Optional[str] = None, client_secret: Optional[str] = None, refresh_token: Optional[str] = None)
Parameters
- api_base_url: Optionally override the base URL used by the API (when using the sandbox environment, for e.g.)
def
logout(self) -> None:
End the session
Deprecated since version 1.3.0: Use OAuth2 credentials instead.
class
MarketData:
Submodule for streaming market data
def
subscribe( self, symbols: List[str], on_candle: Callable[[dict], NoneType] = None, on_greeks: Callable[[dict], NoneType] = None, on_quote: Callable[[dict], NoneType] = None, aggregation_period: Optional[float] = None, event_fields: Optional[dict[str, list[str]]] = None, **kwargs) -> tastytrade_sdk.Subscription:
Subscribe to live feed data
Parameters
- symbols: Symbols to subscribe to. Can be across multiple instrument types.
- on_candle: Handler for candle events
- on_greeks: Handler for greeks events
- on_quote: Handler for quote events
- aggregation_period: Desired aggregation period of events (in seconds)
- event_fields: If provided, a dict mapping one or more event types to lists of event fields.
The event types recognized are 'Quote', 'Greeks', and 'Candle'. If specified, the server will be
asked to send only these fields in a compact format. Compact events will be translated to the same
dictionary format as full events, though with missing keys.
Example:
{ "Quote": ["eventType", "eventSymbol", "bidPrice", "askPrice", "bidSize", "askSize"] }
class
Subscription:
@singleton
class
Api:
In case an open API feature isn't supported by this SDK yet, use this submodule to make direct requests to the API.
The params argument can either be a Dict[str, Any] or a List[Tuple[str, Any]]
API endpoints that accept multiple symbols in the query string use the symbol[]=SPY&symbol[]=AAPL&... convention,
in which case, params should be passed as a List[Tuple[str, Any]], since duplicate keys are not allowed in
dicts. e.g:
equities = tasty.api.get(
'/instruments/equities',
params=[('symbol[]', 'SPY'), ('symbol[]', 'AAPL')]
)
def
get( self, path: str, params: Union[Dict[str, Any], List[Tuple[str, Any]], NoneType] = None) -> Optional[dict]:
Make a GET request
def
post( self, path: str, params: Union[Dict[str, Any], List[Tuple[str, Any]], NoneType] = None, data: Optional[dict] = None) -> Optional[dict]:
Make a POST request
def
put( self, path: str, params: Union[Dict[str, Any], List[Tuple[str, Any]], NoneType] = None, data: Optional[dict] = None) -> Optional[dict]:
Make a PUT request