WebSocket#
- class yfinance.WebSocket(url: str = 'wss://streamer.finance.yahoo.com/?version=2', verbose=True)#
Synchronous WebSocket client for streaming real time pricing data.
Initialize the WebSocket client.
- Parameters:
url (str) – The WebSocket server URL. Defaults to Yahoo Finance’s WebSocket URL.
verbose (bool) – Flag to enable or disable print statements. Defaults to True.
Methods
- __init__(url: str = 'wss://streamer.finance.yahoo.com/?version=2', verbose=True)
Initialize the WebSocket client.
- Parameters:
url (str) – The WebSocket server URL. Defaults to Yahoo Finance’s WebSocket URL.
verbose (bool) – Flag to enable or disable print statements. Defaults to True.
- close()
Close the WebSocket connection.
- listen(message_handler: Callable[[dict], None] | None = None)
Start listening to messages from the WebSocket server.
- Parameters:
message_handler (Optional[Callable[[dict], None]]) – Optional function to handle received messages.
- subscribe(symbols: str | List[str])
Subscribe to a stock symbol or a list of stock symbols.
- Parameters:
symbols (str | List[str]) – Stock symbol(s) to subscribe to.
- unsubscribe(symbols: str | List[str])
Unsubscribe from a stock symbol or a list of stock symbols.
- Parameters:
symbols (str | List[str]) – Stock symbol(s) to unsubscribe from.
- close()#
Close the WebSocket connection.
- listen(message_handler: Callable[[dict], None] | None = None)#
Start listening to messages from the WebSocket server.
- Parameters:
message_handler (Optional[Callable[[dict], None]]) – Optional function to handle received messages.
- subscribe(symbols: str | List[str])#
Subscribe to a stock symbol or a list of stock symbols.
- Parameters:
symbols (str | List[str]) – Stock symbol(s) to subscribe to.
- unsubscribe(symbols: str | List[str])#
Unsubscribe from a stock symbol or a list of stock symbols.
- Parameters:
symbols (str | List[str]) – Stock symbol(s) to unsubscribe from.