Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: WebSocket Client for Live Data Streaming #2201

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

dhruvan2006
Copy link
Contributor

Changes

  • Introduced a new WebSocket class to stream live market data.
  • Added ProtoBuf files (pricing.proto and pricing_pb2.py) for data de serialization.
  • Updated requirements.txt and setup.py to include new dependencies.
  • Added documentation for usage

Usage

import yfinance as yf

def on_price_update(data):
    print(data)

# Create WebSocket client
ws = yf.WebSocket(on_price_update)

# Subscribe to symbols
ws.subscribe(["AAPL", "BTC-USD"])

@dhruvan2006
Copy link
Contributor Author

@ValueRaider any opinions on this websocket? Is it suitable for this package?

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jan 6, 2025

I'm focusing on the Screener stuff right now. Can someone else try and feedback?

Previous discussions: #319 #290

Copy link
Contributor

@R5dan R5dan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a new function in WebSocket for initialization.
Also I think its worth considering adding an async option

Comment on lines +99 to +102
if self.is_running:
self.logger.warning("WebSocket is already running.")
print("WebSocket is already running.")
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not "unsubscribe" from everything and then resubscribe to everything + this new ticker.

This would be easier for the user as this is what they ultimately will end up doing

Comment on lines +105 to +115
try:
self.ws = create_connection(self.uri)
self.is_running = True

subscribe_message = {"subscribe": symbols}
self.ws.send(json.dumps(subscribe_message))
self.logger.info("Subscribed to: %s", symbols)

self.listen()
except Exception as e:
self.logger.error("Failed to subscribe to symbols: %s", e, exc_info=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the user call listen, have this function return itself and just update the tickers.

@@ -39,7 +39,8 @@ Yahoo! finance API is intended for personal use only.**
- `Ticker`: single ticker data
- `Tickers`: multiple tickers' data
- `download`: download market data for multiple tickers
- `Market`: get infomation about a market
- `WebSocket`: live streaming data for multiple tickers
- `Market`: get information about a market
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants