Skip to content

Commit 9415f4d

Browse files
committed
Add WebSocket documentation
1 parent 4c16994 commit 9415f4d

File tree

7 files changed

+69
-1
lines changed

7 files changed

+69
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Yahoo! finance API is intended for personal use only.**
3939
- `Ticker`: single ticker data
4040
- `Tickers`: multiple tickers' data
4141
- `download`: download market data for multiple tickers
42+
- `WebSocket` and `AsyncWebSocket`: live streaming data
4243
- `Market`: get infomation about a market
4344
- `Search`: quotes and news from search
4445
- `Sector` and `Industry`: sector and industry information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import asyncio
2+
import yfinance as yf
3+
4+
# define your message callback
5+
def message_handler(message):
6+
print("Received message:", message)
7+
8+
async def main():
9+
client = yf.AsyncWebSocket()
10+
await client.subscribe(["AAPL", "BTC-USD"])
11+
await client.listen()
12+
13+
asyncio.run(main())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import yfinance as yf
2+
3+
# define your message callback
4+
def message_handler(message):
5+
print("Received message:", message)
6+
7+
client = yf.WebSocket()
8+
client.subscribe(["AAPL", "BTC-USD"])
9+
client.listen(message_handler)

doc/source/reference/examples/ticker.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020

2121
# analysis
2222
dat.analyst_price_targets
23+
24+
# websocket
25+
dat.live()

doc/source/reference/examples/tickers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
# access each ticker using (example)
66
tickers.tickers['MSFT'].info
77
tickers.tickers['AAPL'].history(period="1mo")
8-
tickers.tickers['GOOG'].actions
8+
tickers.tickers['GOOG'].actions
9+
10+
# websocket
11+
tickers.live()

doc/source/reference/index.rst

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The following are the publicly available classes, and functions exposed by the `
1717
- :attr:`Tickers <yfinance.Tickers>`: Class for handling multiple tickers.
1818
- :attr:`MarketSummary <yfinance.MarketSummary>`: Class for accessing market summary.
1919
- :attr:`Search <yfinance.Search>`: Class for accessing search results.
20+
- :class:`WebSocket <yfinance.WebSocket>`: Class for synchronously streaming live market data.
21+
- :class:`AsyncWebSocket <yfinance.AsyncWebSocket>`: Class for asynchronously streaming live market data.
2022
- :attr:`Sector <yfinance.Sector>`: Domain class for accessing sector information.
2123
- :attr:`Industry <yfinance.Industry>`: Domain class for accessing industry information.
2224
- :attr:`download <yfinance.download>`: Function to download market data for multiple tickers.
@@ -37,6 +39,7 @@ The following are the publicly available classes, and functions exposed by the `
3739
yfinance.analysis
3840
yfinance.marketsummary
3941
yfinance.search
42+
yfinance.websocket
4043
yfinance.sector_industry
4144
yfinance.screener
4245
yfinance.functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=====================
2+
WebSocket
3+
=====================
4+
5+
.. currentmodule:: yfinance
6+
7+
The `WebSocket` module allows you to stream live price data from Yahoo Finance using both synchronous and asynchronous clients.
8+
9+
Classes
10+
------------
11+
12+
.. autosummary::
13+
:toctree: api/
14+
15+
WebSocket
16+
AsyncWebSocket
17+
18+
Synchronous WebSocket
19+
----------------------
20+
21+
The `WebSocket` class provides a synchronous interface for subscribing to price updates.
22+
23+
Sample Code:
24+
25+
.. literalinclude:: examples/live_sync.py
26+
:language: python
27+
28+
Asynchronous WebSocket
29+
-----------------------
30+
31+
The `AsyncWebSocket` class provides an asynchronous interface for subscribing to price updates.
32+
33+
Sample Code:
34+
35+
.. literalinclude:: examples/live_async.py
36+
:language: python

0 commit comments

Comments
 (0)