Real-Time Data¶
StockDataStream¶
- class alpaca.data.live.stock.StockDataStream(api_key: str, secret_key: str, raw_data: bool = False, feed: DataFeed = DataFeed.IEX, websocket_params: Dict | None = None, url_override: str | None = None, data_timeout: float | None = None)¶
A WebSocket client for streaming live stock data.
- async close() None¶
Closes the websocket connection.
- register_trade_cancels(handler: Callable[[TradeCancel | Dict], Awaitable[None]]) None¶
Register a trade cancel handler. You can only subscribe to trade cancels by subscribing to the underlying trades.
- Parameters:
handler (Callable[[Union[TradeCancel, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
- register_trade_corrections(handler: Callable[[TradeCorrection | Dict], Awaitable[None]]) None¶
Register a trade correction handler. You can only subscribe to trade corrections by subscribing to the underlying trades.
- Parameters:
handler (Callable[[Union[TradeCorrection, Dict]]) – The coroutine callback function to handle the incoming data.
- run() None¶
Starts up the websocket connection’s event loop
- stop() None¶
Stops the websocket connection.
Safe to call from any thread, including from a handler running on the stream’s own loop: it never blocks. It stops the run from the latest
_run_forever()call even if that run hasn’t begun yet. Called before the stream has ever run, it keeps the first run from starting; called between runs, it is a no-op, since the stream is already stopped.
- async stop_ws() None¶
Signals the websocket connection should close.
- subscribe_bars(handler: Callable[[Bar | Dict], Awaitable[None]], *symbols: str) None¶
Subscribe to minute bars
- Parameters:
handler (Callable[[Union[Trade, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- subscribe_daily_bars(handler: Callable[[Bar | Dict], Awaitable[None]], *symbols: str) None¶
Subscribe to daily bars
- Parameters:
handler (Callable[[Union[Bar, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- subscribe_quotes(handler: Callable[[Quote | Dict], Awaitable[None]], *symbols: str) None¶
Subscribe to quotes
- Parameters:
handler (Callable[[Union[Trade, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- subscribe_trades(handler: Callable[[Trade | Dict], Awaitable[None]], *symbols: str) None¶
Subscribe to trades.
- Parameters:
handler (Callable[[Union[Trade, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- subscribe_trading_statuses(handler: Callable[[TradingStatus | Dict], Awaitable[None]], *symbols) None¶
Subscribe to trading statuses (halts, resumes)
- Parameters:
handler (Callable[[Union[TradingStatus, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- subscribe_updated_bars(handler: Callable[[Bar | Dict], Awaitable[None]], *symbols: str) None¶
Subscribe to updated minute bars
- Parameters:
handler (Callable[[Union[Bar, Dict]], Awaitable[None]]) – The coroutine callback function to handle the incoming data.
*symbols – List of ticker symbols to subscribe to. “*” for everything.
- unsubscribe_bars(*symbols: str) None¶
Unsubscribe from minute bars
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.
- unsubscribe_daily_bars(*symbols: str) None¶
Unsubscribe from daily bars
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.
- unsubscribe_quotes(*symbols: str) None¶
Unsubscribe from quotes
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.
- unsubscribe_trades(*symbols: str) None¶
Unsubscribe from trades
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.
- unsubscribe_trading_statuses(*symbols: str) None¶
Unsubscribe from trading statuses
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.
- unsubscribe_updated_bars(*symbols: str) None¶
Unsubscribe from updated bars
- Parameters:
*symbols (str) – List of ticker symbols to unsubscribe from. “*” for everything.