tastyware / tastytrade

An unofficial Python SDK for Tastytrade!
MIT License
98 stars 33 forks source link

Example code not working; any updates? #124

Closed Coding4Sec closed 5 months ago

Coding4Sec commented 5 months ago

Hi Tasty Traders.

I really like the idea of a python sdk for the tastytrade api.

What is little frustrating: No example code is working. Even searching the ISSUES gives me code that does not work. Let me give an example: In the readme

Options chain/streaming greeks

from tastytrade.instruments import get_option_chain
from datetime import date

chain = get_option_chain(session, 'SPLG')
subs_list = [chain[date(2023, 6, 16)][0].streamer_symbol]

await streamer.subscribe(EventType.GREEKS, subs_list)
greeks = await streamer.get_event(EventType.GREEKS)
print(greeks)

This throws error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 await streamer.subscribe(EventType.GREEKS, subs_list)
      2 greeks = await streamer.get_event(EventType.GREEKS)
      3 print(greeks)

NameError: name 'streamer' is not defined

It looks like this piece of code is not intended to run on its own?

Next, when looking for a minimum working example, I came across closed Issue #21

from tastytrade.session import Session
from tastytrade.streamer import DataStreamer
from tastytrade.dxfeed.event import EventType
from tastytrade.option import get_option_chain
from datetime import date

session = Session('username', 'password', is_certification=True)
streamer = await DataStreamer.create(session)
quote = await streamer.stream(EventType.QUOTE, ['.SPXW230512P3900'])
chain = await get_option_chain(session, 'SPXW', date(2023, 5, 15))
print(quote, chain.options)

This throws error:

{
    "name": "ImportError",
    "message": "cannot import name 'DataStreamer' from 'tastytrade.streamer' (/Users/crg/VSCode_Python/VENVS/python_3_12/tasty_api/.venv/lib/python3.12/site-packages/tastytrade/streamer.py)",
    "stack": "---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[24], line 2
      1 from tastytrade.session import Session
----> 2 from tastytrade.streamer import DataStreamer

ImportError: cannot import name 'DataStreamer' from 'tastytrade.streamer' (/Users/crg/VSCode_Python/VENVS/python_3_12/tasty_api/.venv/lib/python3.12/site-packages/tastytrade/streamer.py)"
}

I'am not sure how to start using this package when example code is buggy. Can someone provide up-to-date example code that will successfully load option greeks from any contract let's say ticker XSP?

A primer on how the sdk works in principal would be helpful, too.

Regards, Rick

Graeme22 commented 5 months ago

Hi Rick! Sorry getting started hasn't been smooth sailing.

from tastytrade.instruments import get_option_chain
from datetime import date

chain = get_option_chain(session, 'SPLG')
subs_list = [chain[date(2023, 6, 16)][0].streamer_symbol]

await streamer.subscribe(EventType.GREEKS, subs_list)
greeks = await streamer.get_event(EventType.GREEKS)
print(greeks)

The problem here is the streamer hasn't been created. This example was building on the previous one that demonstrated how to create a streamer. You just need to add

async with DXLinkStreamer(session) as streamer: to wrap the code block.

Next, when looking for a minimum working example, I came across closed Issue #21

from tastytrade.session import Session
from tastytrade.streamer import DataStreamer
from tastytrade.dxfeed.event import EventType
from tastytrade.option import get_option_chain
from datetime import date

session = Session('username', 'password', is_certification=True)
streamer = await DataStreamer.create(session)
quote = await streamer.stream(EventType.QUOTE, ['.SPXW230512P3900'])
chain = await get_option_chain(session, 'SPXW', date(2023, 5, 15))
print(quote, chain.options)

The problem here is that some names have been changed since the older version used in this example. DataStreamer is now DXLinkStreamer or DXFeedStreamer and Session is now ProductionSession or CertificationSession. I'm working on bettering some of the documentation now, so I'll definitely visit the README!

The best place you can look for up-to-date examples is in the documentation: https://tastyworks-api.readthedocs.io/en/latest/index.html

I hope this helps and please don't hesitate to reach out if you have more problems!

Coding4Sec commented 5 months ago

Hi Rick! Sorry getting started hasn't been smooth sailing.

from tastytrade.instruments import get_option_chain
from datetime import date

chain = get_option_chain(session, 'SPLG')
subs_list = [chain[date(2023, 6, 16)][0].streamer_symbol]

await streamer.subscribe(EventType.GREEKS, subs_list)
greeks = await streamer.get_event(EventType.GREEKS)
print(greeks)

The problem here is the streamer hasn't been created. This example was building on the previous one that demonstrated how to create a streamer. You just need to add

async with DXLinkStreamer(session) as streamer: to wrap the code block.

Next, when looking for a minimum working example, I came across closed Issue #21

from tastytrade.session import Session
from tastytrade.streamer import DataStreamer
from tastytrade.dxfeed.event import EventType
from tastytrade.option import get_option_chain
from datetime import date

session = Session('username', 'password', is_certification=True)
streamer = await DataStreamer.create(session)
quote = await streamer.stream(EventType.QUOTE, ['.SPXW230512P3900'])
chain = await get_option_chain(session, 'SPXW', date(2023, 5, 15))
print(quote, chain.options)

The problem here is that some names have been changed since the older version used in this example. DataStreamer is now DXLinkStreamer or DXFeedStreamer and Session is now ProductionSession or CertificationSession. I'm working on bettering some of the documentation now, so I'll definitely visit the README!

The best place you can look for up-to-date examples is in the documentation: https://tastyworks-api.readthedocs.io/en/latest/index.html

I hope this helps and please don't hesitate to reach out if you have more problems!

Hi Graeme.

Got my test scripts working now.

The missing streamer object has been the reason ....

Best, Rick