Skip to content

feat: add Yahoo Finance OHLCV data provider#470

Merged
MDUYN merged 9 commits into
devfrom
feature/yahoo-finance-data-provider
Apr 22, 2026
Merged

feat: add Yahoo Finance OHLCV data provider#470
MDUYN merged 9 commits into
devfrom
feature/yahoo-finance-data-provider

Conversation

@MDUYN
Copy link
Copy Markdown
Collaborator

@MDUYN MDUYN commented Apr 22, 2026

Summary

Adds a new YahooOHLCVDataProvider that enables fetching OHLCV data from Yahoo Finance via the yfinance library.

Closes #469

Changes

  • New file: investing_algorithm_framework/infrastructure/data_providers/yahoo.py

    • YahooOHLCVDataProvider class supporting stocks, ETFs, indices, forex, and crypto
    • Uses market="YAHOO" in DataSource to auto-select this provider (same pattern as CCXT)
    • Implements has_data(), get_data(), prepare_backtest_data(), get_backtest_data(), copy()
    • Maps framework TimeFrame values to yfinance intervals (1m, 5m, 1h, 1d, 1W, 1M, etc.)
    • CSV-based storage caching for backtest data
    • Lazy yfinance import with clear error message
  • Modified: __init__.py files to export and register YahooOHLCVDataProvider as a default provider

  • New tests: tests/infrastructure/data_providers/test_yahoo_ohlcv_data_provider.py

    • 14 tests covering has_data, get_data, copy, timeframe mapping, and registration
    • All tests use mocked yfinance (no network calls)

Usage

from investing_algorithm_framework import DataSource

data_source = DataSource(
    identifier="aapl_ohlcv",
    market="YAHOO",
    symbol="AAPL",
    data_type="OHLCV",
    time_frame="1d",
)

Closes #469

MDUYN added 9 commits April 22, 2026 17:06
- Add YahooOHLCVDataProvider class for stocks, ETFs, indices, forex, and crypto
- Support market='YAHOO' in DataSource to auto-select Yahoo provider
- Implement has_data, get_data, prepare_backtest_data, get_backtest_data, copy
- Map framework TimeFrame values to yfinance intervals
- Register as default data provider alongside CCXT
- Add comprehensive unit tests with mocked yfinance
- Add AlphaVantageOHLCVDataProvider (market='ALPHA_VANTAGE') with API key via MarketCredential
- Add PolygonOHLCVDataProvider (market='POLYGON') with API key via MarketCredential
- Both follow same pattern as Yahoo/CCXT: auto-selected based on market value
- Add alpha_vantage and polygon-api-client dependencies
- Register both as default data providers
- Add comprehensive unit tests (32 tests with mocked external calls)
Introduce an intermediate base class OHLCVDataProviderBase that handles
all shared OHLCV data provider logic:
- Constructor, copy(), get_data(), prepare_backtest_data(), get_backtest_data()
- CSV storage helpers, date range resolution, number of data points
- Fixes bug: create_start_date was missing from Yahoo/AV/Polygon providers

Concrete providers now only implement:
- market_name, timeframe_map (class attributes)
- _download_ohlcv() (API-specific download)
- _validate_symbol() (optional, Yahoo only)

Reduces Yahoo/AlphaVantage/Polygon from ~1,851 lines to ~1,033 total (44% reduction).
CCXT intentionally excluded (different pattern: exchange-based detection, pagination, ticker support).
- Remove unused imports (F401) in alpha_vantage, polygon, yahoo, ohlcv_base
- Fix CSVOHLCVDataProvider __init__ docstring placement
- Fix PandasOHLCVDataProvider docstring: 'CSV file' -> 'pandas DataFrame'
- Add custom data providers documentation
- Update data sources docs with market info and links
- Move yfinance, alpha_vantage, polygon-api-client from required to optional deps
- Add poetry extras: [yahoo], [alpha_vantage], [polygon], [all]
- Use lazy imports with placeholder classes that raise helpful ImportError
- Only include available providers in get_default_data_providers()
- Update installation docs with extras install commands
- Add install hints to each provider section in data-sources docs
The metrics module imported yfinance at module level, which broke
the entire framework when yfinance was not installed (now optional).
Move import inside get_risk_free_rate_us() with graceful fallback.
Instead of returning None, fall back to a 4% default rate so
metrics like Sortino/Sharpe ratios work without yfinance installed.
@MDUYN MDUYN merged commit 6d564f3 into dev Apr 22, 2026
6 checks passed
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.

1 participant