Spaces:
Sleeping
Sleeping
File size: 12,824 Bytes
72fc0ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | """
Real Data API Router - Uses Alpha Vantage + Yahoo Finance
New endpoints that integrate with the Sprint 1-2 data infrastructure:
- Real-time quotes from Alpha Vantage/Yahoo Finance
- Historical data with automatic fallback
- Redis caching with smart TTL
- Database persistence (PostgreSQL + TimescaleDB)
- Provider health monitoring
"""
import logging
from typing import List, Optional, Dict, Any
from datetime import datetime
from fastapi import APIRouter, HTTPException, Query, Depends
from pydantic import BaseModel, Field
from quantlib_pro.data.data_router import DataProviderRouter
from quantlib_pro.data.cache_manager import CacheManager
from quantlib_pro.data.database import get_postgres_session, get_timescale_session
from quantlib_pro.data.models import User, Portfolio, Holding, Price
logger = logging.getLogger(__name__)
realdata_router = APIRouter(
prefix="/realdata",
tags=["Real-Time Market Data"],
responses={
429: {"description": "Rate limit exceeded"},
500: {"description": "Internal server error"},
503: {"description": "Market data service unavailable"}
}
)
# Initialize global data router and cache (reuse connections)
_data_router: Optional[DataProviderRouter] = None
_cache_manager: Optional[CacheManager] = None
def get_data_router() -> DataProviderRouter:
"""Dependency for data router."""
global _data_router
if _data_router is None:
_data_router = DataProviderRouter()
return _data_router
def get_cache() -> CacheManager:
"""Dependency for cache manager."""
global _cache_manager
if _cache_manager is None:
_cache_manager = CacheManager()
return _cache_manager
# =============================================================================
# Models
# =============================================================================
class QuoteResponse(BaseModel):
symbol: str
price: float
volume: int
timestamp: str
change: float
change_percent: float
provider: str
cached: bool
class HistoricalDataRequest(BaseModel):
symbol: str
period: str = Field(default="1y", description="1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, max")
adjusted: bool = Field(default=True, description="Adjust for dividends and splits")
class HistoricalDataResponse(BaseModel):
symbol: str
period: str
data_points: int
data: List[Dict[str, Any]]
provider: str
cached: bool
class BatchQuoteRequest(BaseModel):
symbols: List[str] = Field(..., min_length=1, max_length=50)
class BatchQuoteResponse(BaseModel):
quotes: Dict[str, QuoteResponse]
failed_symbols: List[str]
total_time_ms: float
class ProviderHealthResponse(BaseModel):
alpha_vantage: str
yahoo_finance: str
cache_connected: bool
cache_keys: int
cache_hit_rate: float
class CacheStatsResponse(BaseModel):
connected: bool
keys: int
memory_used: str
hit_rate: float
uptime_seconds: int
# =============================================================================
# Endpoints: Real-Time Quotes
# =============================================================================
@realdata_router.get("/quote/{symbol}", response_model=QuoteResponse)
async def get_real_quote(
symbol: str,
use_cache: bool = Query(True, description="Use Redis cache"),
router: DataProviderRouter = Depends(get_data_router),
cache: CacheManager = Depends(get_cache)
):
"""
Get real-time quote for a symbol.
- Tries cache first (TTL: 60s)
- Falls back to Alpha Vantage β Yahoo Finance
- Returns provider name and cache status
"""
try:
cached = False
# Try cache first
if use_cache:
cached_quote = cache.get("quote", symbol)
if cached_quote:
cached_quote["cached"] = True
return QuoteResponse(**cached_quote)
# Fetch from API
quote = router.get_quote(symbol)
# Cache it
if use_cache:
cache.set("quote", quote, symbol)
return QuoteResponse(
symbol=quote["symbol"],
price=quote["price"],
volume=quote["volume"],
timestamp=quote["timestamp"],
change=quote.get("change", 0),
change_percent=float(quote.get("change_percent", 0)),
provider=quote["_provider"],
cached=cached
)
except Exception as e:
logger.error(f"Failed to get quote for {symbol}: {e}")
raise HTTPException(status_code=500, detail=f"Failed to fetch quote: {str(e)}")
@realdata_router.post("/quote/batch", response_model=BatchQuoteResponse)
async def get_batch_quotes(
request: BatchQuoteRequest,
use_cache: bool = Query(True),
router: DataProviderRouter = Depends(get_data_router),
cache: CacheManager = Depends(get_cache)
):
"""
Get real-time quotes for multiple symbols.
- Processes symbols in parallel
- Returns partial results if some symbols fail
"""
import time
start_time = time.time()
try:
quotes = {}
failed = []
for symbol in request.symbols:
try:
# Try cache first
cached_quote = None
if use_cache:
cached_quote = cache.get("quote", symbol)
if cached_quote:
quotes[symbol] = QuoteResponse(**{**cached_quote, "cached": True})
else:
# Fetch from API
quote = router.get_quote(symbol)
# Cache it
if use_cache:
cache.set("quote", quote, symbol)
quotes[symbol] = QuoteResponse(
symbol=quote["symbol"],
price=quote["price"],
volume=quote["volume"],
timestamp=quote["timestamp"],
change=quote.get("change", 0),
change_percent=float(quote.get("change_percent", 0)),
provider=quote["_provider"],
cached=False
)
except Exception as e:
logger.warning(f"Failed to get quote for {symbol}: {e}")
failed.append(symbol)
elapsed_ms = (time.time() - start_time) * 1000
return BatchQuoteResponse(
quotes=quotes,
failed_symbols=failed,
total_time_ms=elapsed_ms
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Batch quote failed: {str(e)}")
# =============================================================================
# Endpoints: Historical Data
# =============================================================================
@realdata_router.post("/historical", response_model=HistoricalDataResponse)
async def get_historical_data(
request: HistoricalDataRequest,
use_cache: bool = Query(True),
store_db: bool = Query(False, description="Store data in TimescaleDB"),
router: DataProviderRouter = Depends(get_data_router),
cache: CacheManager = Depends(get_cache)
):
"""
Get historical OHLCV data for a symbol.
- Tries cache first (TTL: 24h)
- Falls back to Alpha Vantage β Yahoo Finance
- Optionally stores data in TimescaleDB hypertable
"""
try:
cached = False
# Try cache first
if use_cache:
cached_data = cache.get("historical", request.symbol, request.period)
if cached_data:
return HistoricalDataResponse(
symbol=cached_data["symbol"],
period=request.period,
data_points=len(cached_data["data"]),
data=cached_data["data"],
provider=cached_data.get("_provider", "cache"),
cached=True
)
# Fetch from API
historical = router.get_historical(
request.symbol,
period=request.period,
adjusted=request.adjusted
)
# Cache it
if use_cache:
cache.set("historical", historical, request.symbol, request.period)
# Store in TimescaleDB
if store_db:
try:
with get_timescale_session() as session:
for day in historical["data"]:
price = Price(
time=day["date"],
ticker=request.symbol,
open=day["open"],
high=day["high"],
low=day["low"],
close=day["close"],
volume=day["volume"],
adjusted_close=day.get("adjusted_close", day["close"])
)
session.merge(price) # Update if exists
session.commit()
logger.info(f"Stored {len(historical['data'])} price records for {request.symbol}")
except Exception as e:
logger.warning(f"Failed to store in TimescaleDB: {e}")
return HistoricalDataResponse(
symbol=historical["symbol"],
period=request.period,
data_points=len(historical["data"]),
data=historical["data"],
provider=historical["_provider"],
cached=False
)
except Exception as e:
logger.error(f"Failed to get historical data for {request.symbol}: {e}")
raise HTTPException(status_code=500, detail=f"Failed to fetch historical data: {str(e)}")
# =============================================================================
# Endpoints: System Status
# =============================================================================
@realdata_router.get("/health", response_model=ProviderHealthResponse)
async def get_provider_health(
router: DataProviderRouter = Depends(get_data_router),
cache: CacheManager = Depends(get_cache)
):
"""
Check health of data providers and cache.
- Provider status (active, rate_limited, failed, unavailable)
- Cache connection status
- Cache hit rate
"""
try:
provider_status = router.get_provider_status()
cache_stats = cache.get_stats()
return ProviderHealthResponse(
alpha_vantage=provider_status.get("alpha_vantage", "unavailable"),
yahoo_finance=provider_status.get("yahoo_finance", "active"),
cache_connected=cache_stats.get("connected", False),
cache_keys=cache_stats.get("keys", 0),
cache_hit_rate=cache_stats.get("hit_rate", 0.0)
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Health check failed: {str(e)}")
@realdata_router.get("/cache/stats", response_model=CacheStatsResponse)
async def get_cache_stats(cache: CacheManager = Depends(get_cache)):
"""
Get detailed cache statistics.
- Total keys
- Memory usage
- Hit rate
- Uptime
"""
try:
stats = cache.get_stats()
return CacheStatsResponse(**stats)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get cache stats: {str(e)}")
@realdata_router.post("/cache/clear/{pattern}")
async def clear_cache_pattern(
pattern: str,
cache: CacheManager = Depends(get_cache)
):
"""
Clear cache keys matching a pattern.
Examples:
- /cache/clear/quote:* β Clear all quotes
- /cache/clear/*:AAPL* β Clear all AAPL data
- /cache/clear/historical:* β Clear all historical data
"""
try:
deleted = cache.invalidate_pattern(pattern)
return {
"pattern": pattern,
"deleted_keys": deleted,
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to clear cache: {str(e)}")
@realdata_router.post("/cache/clear-symbol/{symbol}")
async def clear_symbol_cache(
symbol: str,
cache: CacheManager = Depends(get_cache)
):
"""
Clear all cached data for a specific symbol.
"""
try:
deleted = cache.invalidate_symbol(symbol)
return {
"symbol": symbol,
"deleted_keys": deleted,
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to clear symbol cache: {str(e)}")
|