Data API

Unified Data Manager

Routes data requests to appropriate sources: - Historical data: Parquet files (fast, local) - Real-time data: Polygon API - Sentiment: Alpha Vantage API - Fundamentals: yfinance API

Implements smart routing and fallback strategies.

class quantlab.data.data_manager.DataManager(config, db, parquet)[source]

Bases: object

Unified data manager with smart routing

Features: - Automatic source selection (Parquet vs API) - Caching with TTL - Fallback strategies - Greeks calculation

__init__(config, db, parquet)[source]

Initialize data manager

Parameters:
  • config (Config) – Configuration object

  • db (DatabaseManager) – Database manager

  • parquet (ParquetReader) – Parquet reader

get_stock_price(ticker, date=None, use_cache=True)[source]

Get stock price data

Strategy: - If date is None (real-time): Use Polygon API - If date is provided: Use Parquet (historical) - Cache results in database

Parameters:
  • ticker (str) – Stock ticker symbol

  • date (Optional[date]) – Optional date (None = real-time)

  • use_cache (bool) – Whether to use cached data

Return type:

Optional[TickerSnapshot]

Returns:

TickerSnapshot or None

get_intraday_prices(ticker, interval='1min', from_date=None, to_date=None, limit=50000, include_extended_hours=False)[source]

Get intraday price data at minute/hour intervals

Parameters:
  • ticker (str) – Stock ticker symbol

  • interval (str) – Time interval (‘1min’, ‘5min’, ‘15min’, ‘30min’, ‘1hour’)

  • from_date (Optional[str]) – Start date in YYYY-MM-DD format (default: today)

  • to_date (Optional[str]) – End date in YYYY-MM-DD format (default: today)

  • limit (int) – Maximum number of bars (default: 50000)

  • include_extended_hours (bool) – If True, include pre-market and after-hours data. If False (default), only include regular market hours (9:30 AM - 4 PM ET)

Returns:

date, open, high, low, close, volume, vwap or None if no data available

Return type:

pandas DataFrame with columns

Example

>>> # Get today's 5-minute data (regular hours only)
>>> df = data_manager.get_intraday_prices("AAPL", interval="5min")
>>> # Get 1 year of 5-minute data including pre/post market
>>> df = data_manager.get_intraday_prices("AAPL", interval="5min",
...     from_date="2024-01-01", to_date="2025-01-01", include_extended_hours=True)
get_options_chain(ticker, expiration_date=None, option_type=None, min_itm_pct=5.0, max_itm_pct=20.0, use_cache=True)[source]

Get options chain with calculated advanced Greeks

Parameters:
  • ticker (str) – Underlying ticker symbol

  • expiration_date (Optional[date]) – Optional specific expiration

  • option_type (Optional[str]) – Optional ‘call’ or ‘put’ filter

  • min_itm_pct (float) – Minimum ITM percentage

  • max_itm_pct (float) – Maximum ITM percentage

  • use_cache (bool) – Whether to use cached data

Return type:

List[OptionContract]

Returns:

List of OptionContract objects with Greeks

get_fundamentals(ticker, use_cache=True)[source]

Get fundamental data from yfinance

Parameters:
  • ticker (str) – Stock ticker symbol

  • use_cache (bool) – Whether to use cached data

Return type:

Optional[FundamentalData]

Returns:

FundamentalData or None

get_sentiment(tickers, use_cache=True)[source]

Get news sentiment from Alpha Vantage

Parameters:
  • tickers (List[str]) – List of ticker symbols

  • use_cache (bool) – Whether to use cached data

Return type:

Optional[SentimentData]

Returns:

SentimentData or None

get_vix()[source]

Get VIX data from yfinance

Return type:

Optional[Dict[str, float]]

Returns:

Dictionary with VIX metrics or None

get_technical_indicators(ticker, days=200, verify_calculations=False)[source]

Get technical indicators using hybrid approach: - Primary: Polygon API for SMA, EMA, MACD, RSI (faster, pre-calculated) - Fallback: Calculate from Parquet historical data - Always calculate: Bollinger Bands, ATR, Stochastic, OBV, ADX (not in Polygon)

Parameters:
  • ticker (str) – Stock ticker symbol

  • days (int) – Number of days of historical data for fallback/calculated indicators (default: 200)

  • verify_calculations (bool) – If True, compare calculated vs API values for verification

Return type:

Optional[Dict[str, Any]]

Returns:

Dictionary with all technical indicators or None

API client wrappers for external data sources

Provides unified interface to: - Polygon.io (real-time quotes, options chains) - Alpha Vantage (news sentiment, treasury rates) - yfinance (fundamentals, analyst data)

class quantlab.data.api_clients.RateLimiter(requests_per_minute)[source]

Bases: object

Simple rate limiter for API calls

__init__(requests_per_minute)[source]

Initialize rate limiter

Parameters:

requests_per_minute (int) – Maximum requests allowed per minute

wait_if_needed()[source]

Wait if necessary to respect rate limit

class quantlab.data.api_clients.PolygonClient(api_key, rate_limit=10000)[source]

Bases: object

Wrapper for Polygon.io API

Provides: - Real-time stock quotes - Options chains - Historical OHLCV data - Company details - Technical indicators (SMA, EMA, MACD, RSI)

__init__(api_key, rate_limit=10000)[source]

Initialize Polygon client

Parameters:
  • api_key (str) – Polygon API key

  • rate_limit (int) – Requests per minute (default: 10000 for unlimited Starter plan)

get_stock_snapshot(ticker)[source]

Get current stock snapshot

Parameters:

ticker (str) – Stock ticker symbol

Return type:

Optional[Dict[str, Any]]

Returns:

Dictionary with price data or None

get_options_chain(ticker, expiration_date=None, contract_type=None, max_workers=50)[source]

Get options chain for a ticker (parallel fetching)

Parameters:
  • ticker (str) – Underlying ticker symbol

  • expiration_date (Optional[date]) – Optional specific expiration date

  • contract_type (Optional[str]) – Optional ‘call’ or ‘put’ filter

  • max_workers (int) – Number of parallel threads (default: 50 for unlimited API)

Return type:

List[Dict[str, Any]]

Returns:

List of option contracts

get_market_holidays(start_date=None, end_date=None)[source]

Get market holidays from Polygon API

Parameters:
  • start_date (Optional[str]) – Start date in YYYY-MM-DD format (default: current year)

  • end_date (Optional[str]) – End date in YYYY-MM-DD format (default: next year)

Return type:

List[str]

Returns:

List of holiday dates in YYYY-MM-DD format

get_intraday_aggregates(ticker, multiplier=1, timespan='minute', from_date=None, to_date=None, limit=50000)[source]

Get intraday aggregate bars (minute-level data) from Polygon.

Parameters:
  • ticker (str) – Stock ticker symbol

  • multiplier (int) – Size of the timespan multiplier (e.g., 1, 5, 15, 30)

  • timespan (str) – Size of the time window (‘minute’, ‘hour’)

  • from_date (str) – Start date in YYYY-MM-DD format (default: today)

  • to_date (str) – End date in YYYY-MM-DD format (default: today)

  • limit (int) – Maximum number of results (default: 50000, max: 50000)

Return type:

Optional[List[Dict[str, Any]]]

Returns:

List of dictionaries with OHLCV data or None

Example

>>> client.get_intraday_aggregates("AAPL", multiplier=5, timespan="minute")
>>> # Get 1 year of 5-minute data
>>> client.get_intraday_aggregates("AAPL", multiplier=5, from_date="2024-01-01", to_date="2025-01-01")
get_technical_indicators(ticker)[source]

Get technical indicators from Polygon API

Parameters:

ticker (str) – Stock ticker symbol

Return type:

Optional[Dict[str, Any]]

Returns:

Dictionary with SMA, EMA, MACD, RSI or None

class quantlab.data.api_clients.AlphaVantageClient(api_key, rate_limit=5)[source]

Bases: object

Wrapper for Alpha Vantage API

Provides: - News sentiment analysis - Treasury rates - Economic indicators

BASE_URL = 'https://www.alphavantage.co/query'
__init__(api_key, rate_limit=5)[source]

Initialize Alpha Vantage client

Parameters:
  • api_key (str) – Alpha Vantage API key

  • rate_limit (int) – Requests per minute (default: 5 for free tier)

get_treasury_rate(maturity='3month')[source]

Get current Treasury rate

Parameters:

maturity (str) – ‘3month’, ‘2year’, ‘5year’, ‘10year’, ‘30year’

Return type:

Optional[float]

Returns:

Treasury rate as decimal (e.g., 0.0407 for 4.07%) or None

get_news_sentiment(tickers, limit=50)[source]

Get news sentiment for tickers

Parameters:
  • tickers (List[str]) – List of ticker symbols

  • limit (int) – Maximum number of articles

Return type:

Optional[Dict[str, Any]]

Returns:

Dictionary with sentiment data or None

class quantlab.data.api_clients.YFinanceClient[source]

Bases: object

Wrapper for yfinance library

Provides: - VIX data - Fundamental data - Analyst recommendations - Institutional holdings

__init__()[source]

Initialize yfinance client

get_vix()[source]

Get current VIX data

Return type:

Optional[Dict[str, float]]

Returns:

Dictionary with VIX metrics or None

get_fundamentals(ticker)[source]

Get fundamental data for a ticker

Parameters:

ticker (str) – Stock ticker symbol

Return type:

Optional[Dict[str, Any]]

Returns:

Dictionary with fundamental metrics or None

get_institutional_holders(ticker)[source]

Get institutional holders for a ticker

Parameters:

ticker (str) – Stock ticker symbol

Return type:

Optional[List[Dict[str, Any]]]

Returns:

List of institutional holders or None