TradingStream

class alpaca.trading.stream.TradingStream(api_key: str, secret_key: str, paper: bool = True, raw_data: bool = False, url_override: str = None, websocket_params: Dict | None = None)

This is a WebSocket client which allows you to streaming data from your trading account.

Learn more here: https://alpaca.markets/docs/api-references/trading-api/streaming/

If using paper keys, make sure to set paper to True when instantiating the client.

async close() → None

Closes the websocket connection.

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_trade_updates(handler: Callable)

Subscribes to trade updates for your trading account.

Parameters:

handler (Callable) – The async handler that will receive trade update data.

Returns:

None