tastytrade / tastytrade-sdk-python

MIT License
49 stars 13 forks source link

error in market_data.subscribe / __handle_feed_event (TypeError: string indices must be integers) #17

Open cedarburgqt opened 6 months ago

cedarburgqt commented 6 months ago

Hello,

I am encountering an issue when attempting to use the sample code provided at https://tastytrade.github.io/tastytrade-sdk-python/tastytrade_sdk.html. In specific, I've taken the code provided for "Streaming Market Data" and made sure to substitute the credentials with my own. The code in question is below:

from tastytrade_sdk import Tastytrade

tasty = Tastytrade().login(login='trader@email.com', password='password')

# Subscribing to symbols across different instrument types
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()

I receive the following output:

(c:\Projects\tastytest\.conda) PS C:\Projects\tastytest> & c:/Projects/tastytest/.conda/python.exe c:/Projects/tastytest/testfile.py
Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Projects\tastytest\.conda\lib\threading.py", line 980, in _bootstrap_inner
    self.run()
  File "C:\Projects\tastytest\.conda\lib\site-packages\tastytrade_sdk\market_data\subscription.py", line 26, in run
    self.__activity()
  File "C:\Projects\tastytest\.conda\lib\site-packages\tastytrade_sdk\market_data\subscription.py", line 113, in __receive
    self.__handle_feed_event(event)
  File "C:\Projects\tastytest\.conda\lib\site-packages\tastytrade_sdk\market_data\subscription.py", line 118, in __handle_feed_event
    event_type = event['eventType']
TypeError: string indices must be integers

I am able to run the "validate" API route without issue, so I don't see this as being an authentication issue. I'm using Python 3.9 within an Anaconda environment on Windows, but also encountered this same problem running on AWS using Lambda. As far as I can tell this issue began occurring on 2024-04-25.

I've spoken with some other members in the development community and was pointed towards the unofficial library, which seems like it has a patch applied that might address this issue: https://github.com/tastyware/tastytrade/commit/c0e24cfe55abc02f56d565f04316fb2a69b4442c

pangyuteng commented 2 months ago

my very superficial understanding of the problem just from looking at tastytrade_sdk/market_data/subscription.py and inspecting the element types is that the data structure for message['data'] likely changed, and subscription.py likely needs to be updated accordingly. more detail provided below.

-- edit: An ughly hacky patch to by pass this error is also provided at the end of this post.

--

Sample values for message['data'] https://github.com/tastytrade/tastytrade-sdk-python/blob/master/src/tastytrade_sdk/market_data/subscription.py#L112 is provided below:

sample 1:

['Quote', ['Quote', 'SPX', 0, 0, 0, 0, '', 5646.29, 'NaN', 0, '', 5756.67, 'NaN', 'Quote', 'SPY', 0, 0, 0, 0, 'Q', 567.7, 'NaN', 0, 'Q', 568.25, 'NaN']]

sample 2:

['Candle', ['Candle', 'SPY', 0, 0, 7416879298617405007, 1726876781000, 650831, 1, 568.15, 568.15, 568.15, 568.15, 253.0, 'NaN', 'NaN', 'NaN', 'NaN', 'NaN']]

sample 3:

['Quote', ['Quote', 'BTC/USD:CXTALP', 0, 0, 0, 0, 'Z', 63076.32, 4.0, 0, 'Z', 63500.0, 0.19370079]]

Given above 3 samples, below code block no longer makes sense:

            for event in message['data']:
                self.__handle_feed_event(event)

since __handle_feed_event expects event to be a dict, but both first (str) and second (list of str,int,float) element type is not dict.

--

ugly hacky patch: 80da3331685ace0ce04438b23512c9890592e136...8e0b937eece3da4370b67ec7016a9e5ee9da8db9

sample output from print using the above huckgly :smile: patch: image

pangyuteng commented 2 months ago

for those visiting this issue and wondering what the returned values mean...

Since src/tastytrade_sdk/market_data/subscription.py#L84 did not call self.__send('FEED_SETUP'... to configure the feed, DXLinkFeed (returns all fields)[https://github.com/tastytrade/tastytrade-api-js/blob/a166b3897c7a0f35c1e95208150131aaebca2e80/README.md?plain=1#L42]!

The name and data type for all fields are available here, sample data and corresponding field names for a Candle eventType also provided in below screenshot.

image