areed1192 / td-ameritrade-python-api

Unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard API and allows users to build data pipelines for the Streaming API.
MIT License
683 stars 251 forks source link

Streaming client experiencing websocket connection termination before getting results #143

Open DewinGoh opened 3 years ago

DewinGoh commented 3 years ago

Hi, I've been trying out this library (great work by the way!). Have been running into a wall with this particular issue where the streaming client seems to get terminated on TD Ameritrade's side. Wondering if anyone has encountered this or if there is anything I should have done along the API setup that I missed out.

Probably hitting into this line: https://github.com/areed1192/td-ameritrade-python-api/blob/master/td/stream.py#L741

Logs:

Connection established. Streaming will begin shortly.

================================================================================
CLOSING PROCESS INITIATED:
================================================================================
WebSocket Closed: True
Event Loop Closed: True
================================================================================

Traceback (most recent call last):
  File "td_connect.py", line 25, in <module>
    td_stream_session.stream()
  File "/usr/local/lib/python3.8/site-packages/td/stream.py", line 534, in stream
    self.loop.run_until_complete(self._connect())
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 614, in run_until_complete
    raise RuntimeError('Event loop stopped before Future completed.')
RuntimeError: Event loop stopped before Future completed.
cjmully commented 3 years ago

I'm currently working on this issue as well. It seems to be a problem with using ipython in an IDE that is also running asyncio. Try adding this at the beginning to allow nested asyncio loops.

import nest_asyncio nest_asyncio.apply()