MarketDataProvider is a modular Python package for retrieving financial market data from multiple sources (brokers). It supports real and mock brokers, making it ideal for both production and testing environments.
- Pluggable Broker Architecture: Easily switch between real data sources (e.g., Yahoo Finance) and mock/test brokers.
- Unified API: Access price, profile, history, fundamentals, financials, ratios, recommendations, news, and indicators through a single provider interface.
- Extensible: Add new brokers by implementing the
BrokerAPIprotocol or inheriting fromBaseBroker. - Mock Support: Use
MockBrokerfor testing without external dependencies. - Structured Data: Uses Pydantic models for type safety and serialization.
pip install -e .or from PyPI (when published):
pip install marketdataproviderfrom marketdataprovider import MarketDataProvider, YFinanceBroker, MockBroker
symbol = "AAPL"
# Real broker (Yahoo Finance)
provider = MarketDataProvider(YFinanceBroker())
print(provider.get_price(symbol))
print(provider.get_profile(symbol))
# Mock broker (for testing)
mock_provider = MarketDataProvider(MockBroker())
print(mock_provider.get_price(symbol))
print(mock_provider.get_profile(symbol))Run the included demo to see both brokers in action:
python marketdataprovider/demo_brokers.py- Inherit from
BaseBrokeror implement theBrokerAPIprotocol. - Override required methods (e.g.,
get_price,get_profile, etc.). - Register your broker in your application or demo.
Example:
from marketdataprovider.base.broker import BaseBroker
class MyCustomBroker(BaseBroker):
def get_price(self, symbol: str) -> str:
# Your implementation here
return "..."
# Implement other methods as neededget_price(symbol: str) -> strget_profile(symbol: str) -> strget_history(symbol: str, period: str = "1mo", interval: str = "1d") -> strget_fundamentals(symbol: str) -> strget_financials(symbol: str) -> strget_ratios(symbol: str) -> strget_recommendations(symbol: str) -> strget_news(symbol: str, num_stories: int = 3) -> strget_indicators(symbol: str, period: str = "3mo") -> str
- YFinanceBroker: Real data from Yahoo Finance.
- MockBroker: Returns mock data for all methods.
- Python 3.12+
- yfinance
- pydantic
Niels Weistra