Skip to content

Commit

Permalink
Add WebSocket documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvan2006 committed Jan 18, 2025
1 parent f203339 commit 43a9a1c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Yahoo! finance API is intended for personal use only.**
- `Ticker`: single ticker data
- `Tickers`: multiple tickers' data
- `download`: download market data for multiple tickers
- `WebSocket` and `AsyncWebSocket`: live streaming data
- `Market`: get infomation about a market
- `Search`: quotes and news from search
- `Sector` and `Industry`: sector and industry information
Expand Down
14 changes: 14 additions & 0 deletions doc/source/reference/examples/live_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import asyncio
import yfinance as yf

# define your message callback
def message_handler(message):
print("Received message:", message)

async def main():
client = yf.AsyncWebSocket()
await client.subscribe(["AAPL", "BTC-USD", "ETH-USD"]) # Subscribe to symbols
await client.unsubscribe("ETH-USD") # Unsubscribe from symbols
await client.listen()

asyncio.run(main())
9 changes: 9 additions & 0 deletions doc/source/reference/examples/live_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import yfinance as yf

# define your message callback
def message_handler(message):
print("Received message:", message)

client = yf.WebSocket()
client.subscribe(["AAPL", "BTC-USD"]) # Subscribe to symbols
client.listen(message_handler)
3 changes: 3 additions & 0 deletions doc/source/reference/examples/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

# analysis
dat.analyst_price_targets

# websocket
dat.live()
5 changes: 4 additions & 1 deletion doc/source/reference/examples/tickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# access each ticker using (example)
tickers.tickers['MSFT'].info
tickers.tickers['AAPL'].history(period="1mo")
tickers.tickers['GOOG'].actions
tickers.tickers['GOOG'].actions

# websocket
tickers.live()
3 changes: 3 additions & 0 deletions doc/source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The following are the publicly available classes, and functions exposed by the `
- :attr:`Tickers <yfinance.Tickers>`: Class for handling multiple tickers.
- :attr:`MarketSummary <yfinance.MarketSummary>`: Class for accessing market summary.
- :attr:`Search <yfinance.Search>`: Class for accessing search results.
- :class:`WebSocket <yfinance.WebSocket>`: Class for synchronously streaming live market data.
- :class:`AsyncWebSocket <yfinance.AsyncWebSocket>`: Class for asynchronously streaming live market data.
- :attr:`Sector <yfinance.Sector>`: Domain class for accessing sector information.
- :attr:`Industry <yfinance.Industry>`: Domain class for accessing industry information.
- :attr:`download <yfinance.download>`: Function to download market data for multiple tickers.
Expand All @@ -37,6 +39,7 @@ The following are the publicly available classes, and functions exposed by the `
yfinance.analysis
yfinance.marketsummary
yfinance.search
yfinance.websocket
yfinance.sector_industry
yfinance.screener
yfinance.functions
Expand Down
36 changes: 36 additions & 0 deletions doc/source/reference/yfinance.websocket.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=====================
WebSocket
=====================

.. currentmodule:: yfinance

The `WebSocket` module allows you to stream live price data from Yahoo Finance using both synchronous and asynchronous clients.

Classes
------------

.. autosummary::
:toctree: api/

WebSocket
AsyncWebSocket

Synchronous WebSocket
----------------------

The `WebSocket` class provides a synchronous interface for subscribing to price updates.

Sample Code:

.. literalinclude:: examples/live_sync.py
:language: python

Asynchronous WebSocket
-----------------------

The `AsyncWebSocket` class provides an asynchronous interface for subscribing to price updates.

Sample Code:

.. literalinclude:: examples/live_async.py
:language: python

0 comments on commit 43a9a1c

Please sign in to comment.