# Option Market Data Websocket Stream

**URL:** <https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841>\
**Category:** Alpaca Market Data\
**Created:** [March 13, 2024, 7:20pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841 "2024-03-13T19:20:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neweig](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@neweig](https://forum.alpaca.markets/u/neweig)\
**Post date:** [March 13, 2024, 7:20pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/1 "2024-03-13T19:20:12Z")

</div>

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!

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

```

---

<div class="post-metadata">

**Author:** ![phoenix](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/phoenix/32/2433_2.png) [@phoenix](https://forum.alpaca.markets/u/phoenix)\
**Post date:** [April 25, 2024, 4:39pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/2 "2024-04-25T16:39:48Z")

</div>

I thought the URL was:

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

Source:

> **[Real-time Option Data](https://docs.alpaca.markets/docs/real-time-option-data)**
>
> This API provides option market data on a websocket stream. This helps receive the most up to date market information that could help your trading strategy to act upon certain market movements. If you wish to access the latest pricing data, using the...

---

<div class="post-metadata">

**Author:** ![neweig](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@neweig](https://forum.alpaca.markets/u/neweig)\
**Post date:** [May 20, 2024, 7:48pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/3 "2024-05-20T19:48:21Z")

</div>

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:

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

```

Have you met similar error? Thank you!

Best

---

<div class="post-metadata">

**Author:** ![Denis\_Sichik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/denis_sichik/32/1472_2.png) [@Denis\_Sichik](https://forum.alpaca.markets/u/Denis_Sichik)\
**Post date:** [May 21, 2024, 1:50am UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/4 "2024-05-21T01:50:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neweig](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@neweig](https://forum.alpaca.markets/u/neweig)\
**Post date:** [May 21, 2024, 4:12pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/5 "2024-05-21T16:12:11Z")

</div>

> [@Denis\_Sichik](#):
>
> SPY240524C00528000

still not work, same error.

---

<div class="post-metadata">

**Author:** ![covariancematrix](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/covariancematrix/32/2215_2.png) [@covariancematrix](https://forum.alpaca.markets/u/covariancematrix)\
**Post date:** [September 13, 2024, 8:28pm UTC](https://forum.alpaca.markets/t/option-market-data-websocket-stream/13841/6 "2024-09-13T20:28:14Z")

</div>

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
