I’m using latest market data api for option data, this is my code for alpaca_trade_api
in python, but I got an error:
data websocket error, restarting connection: server rejected WebSocket connection: HTTP 404
I’m not sure what’s wrong here, or is my subscription incorrect? If I switch to stock websocket stream, I can see live market data.
Thank you!
python
from alpaca_trade_api.stream import Stream
from alpaca_trade_api.common import URL
async def trade_update_handler(t):
symbol = t.symbol
price = Decimal(t.price)
timestamp = t.timestamp
print(symbol, price, timestamp)
async def subscribe_to_tickers(stream, tickers):
for ticker in tickers:
stream.subscribe_trades(trade_update_handler, ticker)
await stream._run_forever()
account = get_market_data_account()
stream = Stream(account.api_key,
account.secret_key,
data_stream_url=URL('wss://stream-options.data.alpaca.markets/v1beta1'),
data_feed='indicative')
if loop.is_running():
asyncio.ensure_future(subscribe_to_tickers(stream, tickers))
else:
loop.run_until_complete(subscribe_to_tickers(stream, tickers))
loop = asyncio.get_event_loop()