Streaming connection lost 1006 no reason


I have connected to the Alpaca streaming data API as shown here:

I’ve used the Python SDK this way:

feed = 'sip'
stream = Stream(ALPACA_API_KEY,

for symbol, handler in zip(symbols, handlers):
    stream.subscribe_trades(handler.handle, symbol)

Every handler I attached reads the data from the message received and saves it as a property of a class, this is how the method looks like in my handler class:

    async def handle(self, msg):
        symbol = msg['S']
        if symbol == self.symbol:
            self._quote_time = datetime.datetime.fromtimestamp(
            self._last = msg['p']
            self._volume = msg['s']

            print(self.symbol, self.quote_time, self.last, self.volume)

If I leave the stream on, at some point I’m getting a “connection error 1006 no reason”

There is no algorithm running on the handler right now, if I add in time.sleep(2) after the printing to simulate a trading algorithm doing its work, I get the 1006 sooner.

The same problem was occuring when I used TD Ameritrade’s free streaming API.

How can I consume this market data without getting disconnected?


I am getting 1006 once in a while (several times a day, too)… Just adjust your program to automatically reconnect on 1006. Even if they fix this, your program can break at any time and should be able to reconnect automatically anyway

How would you implement such a function?