Skip to content

Instantly share code, notes, and snippets.

@dboonstra
Created December 23, 2024 20:14
Show Gist options
  • Save dboonstra/f081e3d439bf3559af6e2ede22b3481f to your computer and use it in GitHub Desktop.
Save dboonstra/f081e3d439bf3559af6e2ede22b3481f to your computer and use it in GitHub Desktop.
import asyncio
from decimal import Decimal
from typing import List, Dict
import pandas as pd
from tastytrade import Session, DXLinkStreamer
from tastytrade.dxfeed import Quote
import os
TASTYTRADE_USERNAME = os.environ['TASTY_USER']
TASTYTRADE_PASSWORD = os.environ['TASTY_PASS']
async def process_quotes(streamer: DXLinkStreamer, prices: Dict[str, Decimal]):
async for quote in streamer.listen(Quote):
if quote:
if quote.bid_price is not None and quote.ask_price is not None:
market_price = (quote.bid_price + quote.ask_price) / 2
prices[quote.event_symbol] = market_price
else:
prices[quote.event_symbol] = Decimal('0')
def calculate_strategy_net_credit_debit(df: pd.DataFrame) -> pd.DataFrame:
df['net_value'] = df['market_price'] * df['quantity']
grouped = df.groupby('group_name')['net_value'].sum().reset_index()
return grouped
async def main():
session = Session(TASTYTRADE_USERNAME, TASTYTRADE_PASSWORD)
df = pd.read_csv("watchlist.csv")
df['market_price'] = 0.0
symbol_list = df['streamer_symbol'].tolist()
prices: Dict[str, Decimal] = {}
async with DXLinkStreamer(session) as streamer:
await streamer.subscribe(Quote, symbol_list)
quote_task = asyncio.create_task(process_quotes(streamer, prices))
await asyncio.sleep(1)
while True:
print("Updating Market Prices...")
for index, row in df.iterrows():
symbol = row['streamer_symbol']
if symbol in prices:
df.loc[index, 'market_price'] = float(prices[symbol])
else:
df.loc[index, 'market_price'] = 0.0
net_credit_debit = calculate_strategy_net_credit_debit(df)
print("Net Credit/Debit Per Strategy:")
print(net_credit_debit)
print(df[['group_name','streamer_symbol','quantity','market_price']])
await asyncio.sleep(60)
if __name__ == "__main__":
asyncio.run(main())
@dboonstra
Copy link
Author

dboonstra commented Dec 23, 2024

Sample run:

$ python spread-watch.py 
Updating Market Prices...
Net Credit/Debit Per Strategy:
  group_name  net_value
0      csprd     -0.965
1         ic     -2.940
  group_name streamer_symbol  quantity  market_price
0      csprd  .SPY250117C610        -1         1.930
1      csprd  .SPY250117C615         1         0.965
2         ic  .CAT250110P350         1         2.710
3         ic  .CAT250110P355        -1         3.925
4         ic  .CAT250110C370        -1         5.450
5         ic  .CAT250110C375         1         3.725
Updating Market Prices...
Net Credit/Debit Per Strategy:
  group_name  net_value
0      csprd      -0.96
1         ic      -2.94
  group_name streamer_symbol  quantity  market_price
0      csprd  .SPY250117C610        -1         1.920
1      csprd  .SPY250117C615         1         0.960
2         ic  .CAT250110P350         1         2.785
3         ic  .CAT250110P355        -1         3.950
4         ic  .CAT250110C370        -1         5.450
5         ic  .CAT250110C375         1         3.675

@ionescu77
Copy link

ionescu77 commented Jan 7, 2025

Great stuff.

Just a question about the symbols. I have tried using some option symbols for AAPL and it returns 0.00$ market price.

For example
short AAPL 250117C00250000
long AAPL 250117C00245000

This how they are returned from tasty.

Can you give me a hint?

I managed to find out streamer_symbol by using tasty client.

Option Symbol: AAPL  250117C00245000, Streamer Symbol: .AAPL250117C245
Option Symbol: AAPL  250117C00250000, Streamer Symbol: .AAPL250117C250

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment