alpacahq/alpaca-backtrader-api

bug in handling multiple websocket streams via alpaca proxy

KlausGlueckert opened this issue · 1 comments

with multiple assets a bt strategy does not start reliably every time. sometimes it works, sometimes not.

the only explanation for me seems, that if there are no quotes from all assets in the minute of launch it fails when it tries to resample all assets in sync to 1-minute resolution.

root@512449f08c47:~/notebooks/alpaca_test# python bt_test3.py 2>&1 
_time=2021-01-29 17:34:06,264 | connected to: wss://paper-api.alpaca.markets/stream
_time=2021-01-29 17:34:06,395 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29 17:34:06,580 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29 17:34:06,668 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29 17:34:06,817 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29T17:34:06.955730Z | event=notify_data | name=AMZN | status=DELAYED
_time=2021-01-29T17:34:06.955790Z | event=notify_data | name=AAPL | status=DELAYED
_time=2021-01-29T17:34:06.955820Z | event=notify_data | name=MSFT | status=DELAYED
_time=2021-01-29T17:34:06.960927Z | event=notify_data | name=FB | status=DELAYED
_time=2021-01-29T17:34:06.960956Z | event=notify_data | name=SPY | status=DELAYED
_time=2021-01-29T17:34:06.960979Z | event=notify_data | name=TSLA | status=DELAYED
_time=2021-01-29 17:34:06,971 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29 17:34:07,138 | connected to: ws://alpaca_proxy:8765/stream
_time=2021-01-29T17:35:04.708150Z | event=notify_data | name=AMZN | status=LIVE
_time=2021-01-29T17:35:05.379489Z | event=notify_data | name=FB | status=LIVE
_time=2021-01-29T17:35:05.379543Z | event=notify_data | name=SPY | status=LIVE
_time=2021-01-29T17:35:05.379573Z | event=notify_data | name=TSLA | status=LIVE
_time=2021-01-29 17:36:00,052 | Error in data transfer
Traceback (most recent call last):
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/websockets/protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/websockets/protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/websockets/protocol.py", line 995, in read_data_frame
    await self.pong(frame.data)
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/websockets/protocol.py", line 766, in pong
    await self.ensure_open()
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/websockets/protocol.py", line 803, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
_time=2021-01-29 17:36:00,124 | code = 1006 (connection closed abnormally [internal]), no reason
Exception in thread Thread-10:
Traceback (most recent call last):
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 712, in _t_streaming_prices
    streamer.run()
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/alpaca_backtrader_api/alpacastore.py", line 145, in run
    self.conn.run(channels)
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/site-packages/alpaca_trade_api/stream2.py", line 298, in run
    loop.run_until_complete(self.consume())
  File "/root/miniconda/envs/rapids-core-0.17/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
asyncio.exceptions.CancelledError

I still encountered the same issue using 1-min live data feeds. The connections were not restarted every time.

Error:
"alpaca_trade_api.stream websocket error, restarting connection: code = 1006 (connection closed abnormally [internal]), no reason"