I can connect to the trade_updates channel and receive streaming updates. However, after some time I get disconnected and stop receiving updates of my trades. I can see the Exception in the logs, but up to now I haven’t been able to catch it in try..except
block.
How could I catch this disconnect?
I start listening to channel by doing:
conn.run(
['trade_updates'] + tcs + qcs
)
But a try…except block doesn’t catch the disconnection as suggested in the Momentum example script, with relevant code:
# Handle failed websocket connections by reconnecting
def run_ws(conn, channels):
try:
conn.run(channels)
except Exception as e:
print(e)
conn.close
run_ws(conn, channels)
The first exception I see on logs is:
Task exception was never retrieved
future: <Task finished coro=<StreamConn._consume_msg() done, defined at /mnt/home/an/trader/venv/lib/python3.6/site-packages/alpaca_trade_api/stream2.py:46> exception=ConnectTimeout(MaxRetryError("HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/orders (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7fe18a88a898>, 'Connection to paper-api.alpaca.markets timed out. (connect timeout=2)'))",),)>
Traceback (most recent call last):
File "/mnt/home/an/trader/venv/lib/python3.6/site-packages/urllib3/connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/mnt/home/an/trader/venv/lib/python3.6/site-packages/urllib3/util/connection.py", line 80, in create_connection
raise err
File "/mnt/home/an/trader/venv/lib/python3.6/site-packages/urllib3/util/connection.py", line 70, in create_connection
sock.connect(sa)
socket.timeout: timed out