tastyware / tastytrade

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

TastytradeError('Connection timed out') #137

Closed joshuakoh7 closed 2 months ago

joshuakoh7 commented 2 months ago

Hey all,

Just came across this lib today, able to retrieve account information with the test code but having issues with data. Not entirely sure how to debug since there's no real information in the logs to work on.

>>> async def main(session):
...     async with DXFeedStreamer(session) as streamer:
...         await streamer.subscribe(EventType.QUOTE, subs_list)
...         quote = await streamer.get_event(EventType.QUOTE)
...         print(quote)
>>> asyncio.run(main(session))
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<DXFeedStreamer._connect() done, defined at C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\tastytrade\streamer.py:317> exception=TimeoutError()>
Traceback (most recent call last):
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\client.py", line 651, in __await_impl__
    _transport, _protocol = await self._create_connection()
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1036, in create_connection
    infos = await self._ensure_resolved(
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1418, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 863, in getaddrinfo
    return await self.run_in_executor(
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\tastytrade\streamer.py", line 324, in _connect
    async with websockets.connect(  # type: ignore
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\client.py", line 629, in __aenter__
    return await self
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\client.py", line 646, in __await_impl_timeout__
    async with asyncio_timeout(self.open_timeout):
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\async_timeout.py", line 169, in __aexit__
    self._do_exit(exc_type)
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\websockets\legacy\async_timeout.py", line 252, in _do_exit
    raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "<stdin>", line 2, in main
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python310\lib\site-packages\tastytrade\streamer.py", line 657, in __aenter__
    raise TastytradeError('Connection timed out')
tastytrade.utils.TastytradeError: Connection timed out
Graeme22 commented 2 months ago

Hello! I wonder if this is related to #135... Are you using Python 3.12 by chance? Otherwise, could you list your OS, Python version, environment, etc. Also, are you using a certification session or a production session?

kamkudla commented 2 months ago

I'm also running in to this issue and it just started happening a few days ago, there were no changes to my system or environment.

DXFeedStreamer subscribing to Time and Sales Python 3.11.7 in a venv tastytrade 7.0 MacOS 14.3.1

  File "/Users/kmaku/dev/tradewaves/data_processing/./tradewaves_ws_dc.py", line 176, in start_data_streaming
    async with DXFeedStreamer(session) as streamer:
  File "/Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/tastytrade/streamer.py", line 293, in __aenter__
    raise TastytradeError('Connection timed out')
tastytrade.utils.TastytradeError: Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/kmaku/dev/tradewaves/data_processing/./tradewaves_ws_dc.py", line 206, in main
    await asyncio.gather(
  File "/Users/kmaku/dev/tradewaves/data_processing/./tradewaves_ws_dc.py", line 199, in start_data_streaming
    await asyncio.sleep(5)
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/tasks.py", line 649, in sleep
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kmaku/dev/tradewaves/data_processing/./tradewaves_ws_dc.py", line 218, in <module>
    asyncio.run(main())
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/runners.py", line 123, in run
    raise KeyboardInterrupt()
KeyboardInterrupt
Task exception was never retrieved
future: <Task finished name='Task-6' coro=<DXFeedStreamer._connect() done, defined at /Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/tastytrade/streamer.py:317> exception=gaierror(8, 'nodename nor servname provided, or not known')>
Traceback (most recent call last):
  File "/Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/tastytrade/streamer.py", line 324, in _connect
    async with websockets.connect(  # type: ignore
  File "/Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/websockets/legacy/client.py", line 629, in __aenter__
    return await self
           ^^^^^^^^^^
  File "/Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/websockets/legacy/client.py", line 647, in __await_impl_timeout__
    return await self.__await_impl__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/dev/tradewaves/.venv/lib/python3.11/site-packages/websockets/legacy/client.py", line 651, in __await_impl__
    _transport, _protocol = await self._create_connection()
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/base_events.py", line 1045, in create_connection
    infos = await self._ensure_resolved(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/base_events.py", line 1419, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/asyncio/base_events.py", line 867, in getaddrinfo
    return await self.run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kmaku/.pyenv/versions/3.11.7/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Graeme22 commented 2 months ago

Uh oh! Could someone having this issue run it with the logging level set to DEBUG for the tastytrade module?

joshuakoh7 commented 2 months ago

I think what has happened is they've killed the DNS record for tasty-live-web.dxfeed.com. If someone has the old IP address it was mapped to maybe they could try doing a manual connection to the IP directly.

Graeme22 commented 2 months ago

I think what has happened is they've killed the DNS record for tasty-live-web.dxfeed.com. If someone has the old IP address it was mapped to maybe they could try doing a manual connection to the IP directly.

That wouldn't explain why it's working normally for others!

Graeme22 commented 2 months ago

Uh oh! Could someone having this issue run it with the logging level set to DEBUG for the tastytrade module?

Can someone having this issue do this please?

paul-schouten commented 2 months ago

I resolved this by using DXLINK streamer instead. I believe DXFEED has been deprecated. Suggest you change the example on the front page.

Although I don't know why it would work for some people and not others???

Graeme22 commented 2 months ago

I resolved this by using DXLINK streamer instead. I believe DXFEED has been deprecated. Suggest you change the example on the front page.

Although I don't know why it would work for some people and not others???

Ha! That's what I get for not paying close enough attention to the code. I was using DXLinkStreamer also. Looks like the DXFeedStreamer might be dead now... However it doesn't really have any advantages so not a big deal. I'll make sure to update that in the next release.

joshuakoh7 commented 2 months ago

I resolved this by using DXLINK streamer instead. I believe DXFEED has been deprecated. Suggest you change the example on the front page. Although I don't know why it would work for some people and not others???

Ha! That's what I get for not paying close enough attention to the code. I was using DXLinkStreamer also. Looks like the DXFeedStreamer might be dead now... However it doesn't really have any advantages so not a big deal. I'll make sure to update that in the next release.

I've managed to get it working with DXLinkStreamer. I know this probably needs a separate issue but since we're talking about the differences, I'm running into a maximum buffer limit subscribing to ~2000 TAS symbols at one go with DXLink. Chunking it does work but trying to subscribe to the entire SPX chain (~22k options) often results in a dropped connection. With the old streamer I could send in chunks of 10000 with no issue.

Edit: Also getting a lot of "Connection timed out" with DXLinkStreamer

kamkudla commented 2 months ago

We are able to subscribe to Time and Sales with DXLink? I haven't been able to get it working so that's why I was using DXFeed...

Graeme22 commented 2 months ago

We are able to subscribe to Time and Sales with DXLink? I haven't been able to get it working so that's why I was using DXFeed...

I believe it's not supported. However, it appears Tasty never intended for it to be supported in the first place, so it was kind of a "secret bonus" of reverse-engineering the streamer.

Graeme22 commented 2 months ago

I resolved this by using DXLINK streamer instead. I believe DXFEED has been deprecated. Suggest you change the example on the front page. Although I don't know why it would work for some people and not others???

Ha! That's what I get for not paying close enough attention to the code. I was using DXLinkStreamer also. Looks like the DXFeedStreamer might be dead now... However it doesn't really have any advantages so not a big deal. I'll make sure to update that in the next release.

I've managed to get it working with DXLinkStreamer. I know this probably needs a separate issue but since we're talking about the differences, I'm running into a maximum buffer limit subscribing to ~2000 TAS symbols at one go with DXLink. Chunking it does work but trying to subscribe to the entire SPX chain (~22k options) often results in a dropped connection. With the old streamer I could send in chunks of 10000 with no issue.

Edit: Also getting a lot of "Connection timed out" with DXLinkStreamer

I may look at automatically chunking large lists.

kamkudla commented 2 months ago

I'm getting connection time out now with DXLinkStreamer

Graeme22 commented 2 months ago

I'm getting connection time out now with DXLinkStreamer

All of the time? Some of the time?

joshuakoh7 commented 2 months ago

I resolved this by using DXLINK streamer instead. I believe DXFEED has been deprecated. Suggest you change the example on the front page. Although I don't know why it would work for some people and not others???

Ha! That's what I get for not paying close enough attention to the code. I was using DXLinkStreamer also. Looks like the DXFeedStreamer might be dead now... However it doesn't really have any advantages so not a big deal. I'll make sure to update that in the next release.

I've managed to get it working with DXLinkStreamer. I know this probably needs a separate issue but since we're talking about the differences, I'm running into a maximum buffer limit subscribing to ~2000 TAS symbols at one go with DXLink. Chunking it does work but trying to subscribe to the entire SPX chain (~22k options) often results in a dropped connection. With the old streamer I could send in chunks of 10000 with no issue. Edit: Also getting a lot of "Connection timed out" with DXLinkStreamer

I may look at automatically chunking large lists.

Do you have anything for increasing the buffer size?

I'm getting connection time out now with DXLinkStreamer

All of the time? Some of the time?

I'm actually getting it a lot when trying to create a streamer.

hjnoble3 commented 2 months ago

I'm getting connection time out now with DXLinkStreamer

All of the time? Some of the time?

I'm actually getting it a lot when trying to create a streamer.

Same for me. I am trying to subscribe to SUMMARY with chunks of 1000 and time out a lot. I can sometimes wait a few minutes and try again and it will not time out. It is not being consistent.

joshuakoh7 commented 2 months ago

I'm getting connection time out now with DXLinkStreamer

All of the time? Some of the time?

I'm actually getting it a lot when trying to create a streamer.

Same for me. I am trying to subscribe to SUMMARY with chunks of 1000 and time out a lot. I can sometimes wait a few minutes and try again and it will not time out. It is not being consistent.

I'm not even getting to the subscription stage, it just times out before the streamer is created. I have to put the streamer create function on loop that sleeps every 5mins until it finally connects.

Also, seems like DXLink sometimes sends output as lists and sometimes as dicts which makes it annoying to parse

DEBUG:tastytrade:received: {'type': 'FEED_DATA', 'channel': 13, 'data': [{'eventType': 'TimeAndSale', 'eventSymbol': '.SPXW240419C5275', 'eventTime': 0, 'eventFlags': 0, 'index': 7358642686745640961, 'time': 1713317513178, 'timeNanoPart': 0, 'sequence': 1, 'exchangeCode': 'C', 'price': 0.3, 'size': 1.0, 'bidPrice': 0.2, 'askPrice': 0.35, 'exchangeSaleConditions': '3I', 'tradeThroughExempt': '', 'aggressorSide': 'BUY', 'spreadLeg': False, 'extendedTradingHours': True, 'validTick': True, 'type': 'NEW'}]}
DEBUG:tastytrade:received: {'type': 'FEED_DATA', 'channel': 15, 'data': ['Trade', ['Trade', 'NVDA', 0, 1713377100640, 0, 584790, 'L', 852.06, -22.09, 100.0, 19830, 34088998.0, 29472454675.7632, 'ZERO_UP', False, 'Trade', 'AAPL', 0, 1713377100657, 0, 426815, 'L', 168.885, -0.495, 100.0, 19830, 31850211.0, 5400944185.88718, 'ZERO_DOWN', False, 'Trade', 'TSLA', 0, 1713377099998, 0, 689399, 'L', 157.775, 0.665, 500.0, 19830, 60222143.0, 9386244850.5495, 'ZERO_DOWN', False, 'Trade', 'MSFT', 0, 1713377100620, 0, 215573, 'Q', 412.98, -1.6, 100.0, 19830, 9711225.0, 4031105555.8232, 'ZERO_DOWN', False, 'Trade', 'SPY', 0, 1713377099481, 0, 346099, 'L', 502.47, -1.06, 712.0, 19830, 45318968.0, 22816502225.832, 'ZERO_DOWN', False, 'Trade', 'META', 0, 1713377100620, 0, 149849, 'Q', 495.58, -4.18, 100.0, 19830, 7999534.0, 3967715702.62752, 'ZERO_DOWN', False, 'Trade', 'GOOG', 0, 1713377100621, 0, 118747, 'L', 157.52, 1.52, 194.0, 19830, 9330702.0, 1468783457.394126, 'ZERO_DOWN', False, 'Trade', 'GOOGL', 0, 1713377099927, 0, 166088, 'L', 156.03, 1.63, 100.0, 19830, 12670950.0, 1975650405.943568, 'ZERO_UP', False, 'Trade', 'AMD', 0, 1713377100656, 0, 402782, 'L', 156.7248, -6.7352, 200.0, 19830, 48364953.0, 7722181499.2476, 'ZERO_UP', False, 'Trade', 'AMZN', 0, 1713377099928, 0, 255792, 'Q', 181.69, -1.63, 100.0, 19830, 19982760.0, 3652950476.27515, 'ZERO_DOWN', False, 'Trade', 'QQQ', 0, 1713377098929, 0, 288968, 'Q', 427.84, -3.26, 300.0, 19830, 34342842.0, 14758102590.3245, 'ZERO_UP', False]]}
Graeme22 commented 2 months ago

Do you have anything for increasing the buffer size?

Chunking has been and will continue to be the way to do this. I might automate that process but I don't see a need to increase the size.

I'm actually getting it a lot when trying to create a streamer.

I'm getting it too now, time to figure out what's going on!

Graeme22 commented 2 months ago

Alright making some progress... on the docs page for the streamer, the path to get the streamer token has changed. Looks like the old one is still working. but maybe not consistently. Let's see if that fixes the problem...

Graeme22 commented 2 months ago

The new endpoint doesn't even return data for the DXFeed streamer so that pretty much confirms they've axed it.

Graeme22 commented 2 months ago

Pinging @joshuakoh7 @kamkudla @hjnoble3 ! Can you please test if #140 fixes the problem?

kamkudla commented 2 months ago

Pinging @joshuakoh7 @kamkudla @hjnoble3 ! Can you please test if #140 fixes the problem?

I can confirm that the problem is fixed. I have only tried Trade events so far. Nice work 👍🏼