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, ALPACA_SECRET_KEY, data_feed=feed, raw_data=True) for symbol, handler in zip(symbols, handlers): stream.subscribe_trades(handler.handle, symbol) stream.run()
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( msg['t'].seconds) 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?