alpaca-trade-api-python icon indicating copy to clipboard operation
alpaca-trade-api-python copied to clipboard

Fix issue where self._streams set is used as channels but cannot be s…

Open briangu opened this issue 3 years ago • 1 comments

Fix issue where self._streams set is used as channels but cannot be serialized.

When the websocket restarts due to closure, self._streams is used as channels but since it is a set, it cannot be serialized:

at alpaca-trade-api-python/alpaca_trade_api/stream2.py:100> exception=TypeError('Object of type set is not JSON serializable')> Traceback (most recent call last): File "/scm/alpaca-trade-api-python/alpaca_trade_api/stream2.py", line 108, in _ensure_ws await self.subscribe(self._streams) File "/scm/alpaca-trade-api-python/alpaca_trade_api/stream2.py", line 124, in subscribe await self._ws.send(json.dumps({ File "/usr/local/lib/python3.9/json/init.py", line 231, in dumps return _default_encoder.encode(obj) File "/usr/local/lib/python3.9/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/local/lib/python3.9/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/local/lib/python3.9/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type set is not JSON serializable

briangu avatar Mar 12 '21 14:03 briangu

CAN WE GET THIS MERGED!!!

This is because Alpaca API gets disconnected midway. So the API tries to reconnect. But when it does so, the channels are not reinitialized, hence it remains a set that is not JSON serializable. Hence, in alpaca_trade_api > stream2.py change 'streams': channels to 'streams': list(channels) in line 132. That should solve the reconnecting to api and 1006 problem.

nickvertucci avatar Mar 24 '21 12:03 nickvertucci