Created
December 23, 2024 20:14
-
-
Save dboonstra/f081e3d439bf3559af6e2ede22b3481f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
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
Sample run: