Option Market Data Websocket Stream

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()

I thought the URL was:

wss://stream.data.alpaca.markets/v1beta1/{feed}

Source:

Thank you!

But I got an error: [{‘T’: ‘error’, ‘code’: 400, ‘msg’: ‘invalid syntax’}]

when I use url:

“wss://stream.data.alpaca.markets/v1beta1/indicative”

I post a message like:

{
            "action": "subscribe",
            "trades": ["SPY240522C00528000"],
}

Have you met similar error? Thank you!

Best

try to put SPY240524C00528000 - it is ending with 24 -friday, the 24th - expiration date.

still not work, same error.

Hi, it’s been months so maybe you got it working with the updated api.

Here is what I did in python:

from alpaca.data.live.option import OptionDataStream
from alpaca.data.enums import OptionsFeed
from config import *

Replace with actual API keys

api_key = API_KEY
api_secret = API_SECRET

Initialize client

option_stream = OptionDataStream(api_key, api_secret, feed=OptionsFeed.INDICATIVE)

async def option_stream_handler(msg):
print(msg)

symbols = [
‘QQQ240916P00466000’, # Example put option – underlying ticker won’t work
‘QQQ240916C00475000’ # Example call option – underlying ticker won’t work
]

option_stream.subscribe_quotes(option_stream_handler, *symbols)
option_stream.subscribe_trades(option_stream_handler, *symbols)

option_stream.run()

Python console output:

symbol=‘QQQ240916C00475000’ timestamp=datetime.datetime(2024, 9, 13, 20, 0, 20, 895589, tzinfo=datetime.timezone.utc) exchange=‘W’ price=2.0 size=4.0 id=None conditions=‘I’ tape=None
symbol=‘QQQ240916C00475000’ timestamp=datetime.datetime(2024, 9, 13, 20, 0, 20, 895680, tzinfo=datetime.timezone.utc) exchange=‘J’ price=2.0 size=1.0 id=None conditions=‘I’ tape=None
symbol=‘QQQ240916P00466000’ timestamp=datetime.datetime(2024, 9, 13, 20, 0, 23, 734783, tzinfo=datetime.timezone.utc) exchange=‘B’ price=0.22 size=1.0 id=None conditions=‘S’ tape=None
symbol=‘QQQ240916P00466000’ timestamp=datetime.datetime(2024, 9, 13, 20, 0, 24, 190436, tzinfo=datetime.timezone.utc) exchange=‘H’ price=0.22 size=1.0 id=None conditions=‘I’ tape=None