1006 connection closed abnormally

I am trying to run this example: martingale (https://alpaca.markets/learn/martingale-day-trading-python/)
and I get connection error after a couple of hours. I ran this via the paper account for a few days and I got the same error every day:

WARNING:root:code = 1006 (connection closed abnormally [internal]), no reason
Traceback (most recent call last):
File “view_trades.py”, line 143, in

File “view_trades.py”, line 138, in start_trading
conn.run([f’alpacadatav1/T.{self.symbol}’, ‘trade_updates’])
File “…\anaconda3\envs\py36\lib\site-packages\alpaca_trade_api\stream2.py”, line 276, in run
File “…\anaconda3\envs\py36\lib\asyncio\base_events.py”, line 484, in run_until_complete
return future.result()

1 Like

I have the exact same issue. Did you solve it? Would love to know how to.


No I stopped using stream and I am just using REST now.

i tried REST but ran into too man api calls. I am going to try to figure it out.

I am having the same issues occasionally, but today it was occurring quite frequently. It is not clear if the Python Alpaca API stream subscription is able to recover and maintain the connection.
Any ideas if this is happening in the underlying code?

Interested in same answer.

Streaming data is so important for any auto trading strategy. Surprised that polygon/alpaca is not paying attending to resolving this asap as I see many people having the same issue of socket disconnections.

Atleast, please post any work arounds or reconnection examples.

1 Like

I recently started integrating with Alpaca stream api using the python lib: GitHub - alpacahq/alpaca-trade-api-python: Python client for Alpaca's trade API, and I am also consistently getting this error. I added a reconnection attempt mechanism as suggested in alpaca-trade-api-python/avoid_server_connection_errors.py at master · alpacahq/alpaca-trade-api-python · GitHub, but doesnt seem to solve the issue.

Any idea how to resolve?