feat: market-data-service for SenpAI trading agent
New service: real-time market data collection with unified event model. Architecture: - Domain events: TradeEvent, QuoteEvent, BookL2Event, HeartbeatEvent - Provider interface: MarketDataProvider ABC with connect/subscribe/stream/close - Async EventBus with fan-out to multiple consumers Providers: - BinanceProvider: public WebSocket (trades + bookTicker), no API key needed, auto-reconnect with exponential backoff, heartbeat timeout detection - AlpacaProvider: IEX real-time data + paper trading auth, dry-run mode when no keys configured (heartbeats only) Consumers: - StorageConsumer: SQLite (via SQLAlchemy async) + JSONL append-only log - MetricsConsumer: Prometheus counters, latency histograms, events/sec gauge - PrintConsumer: sampled structured logging (1/100 events) CLI: python -m app run --provider binance --symbols BTCUSDT,ETHUSDT HTTP: /health, /metrics (Prometheus), /latest?symbol=XXX Tests: 19/19 passed (Binance parse, Alpaca parse, bus smoke tests) Config: pydantic-settings + .env, all secrets via environment variables. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
57
services/market-data-service/app/providers/__init__.py
Normal file
57
services/market-data-service/app/providers/__init__.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
Market data provider interface and registry.
|
||||
|
||||
To add a new provider:
|
||||
1. Create providers/your_provider.py
|
||||
2. Subclass MarketDataProvider
|
||||
3. Register in PROVIDER_REGISTRY below
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import AsyncIterator
|
||||
|
||||
from app.domain.events import Event
|
||||
|
||||
|
||||
class MarketDataProvider(ABC):
|
||||
"""
|
||||
Base class for all market-data feed adapters.
|
||||
|
||||
Lifecycle: connect() → subscribe() → stream() → close()
|
||||
"""
|
||||
|
||||
name: str = "unknown"
|
||||
|
||||
@abstractmethod
|
||||
async def connect(self) -> None:
|
||||
"""Establish connection to the data source."""
|
||||
|
||||
@abstractmethod
|
||||
async def subscribe(self, symbols: list[str]) -> None:
|
||||
"""Subscribe to symbols. May be called after reconnect."""
|
||||
|
||||
@abstractmethod
|
||||
async def stream(self) -> AsyncIterator[Event]:
|
||||
"""Yield normalized domain events. Must handle reconnect internally."""
|
||||
yield # type: ignore
|
||||
|
||||
@abstractmethod
|
||||
async def close(self) -> None:
|
||||
"""Graceful shutdown."""
|
||||
|
||||
|
||||
def get_provider(name: str) -> MarketDataProvider:
|
||||
"""Factory: instantiate provider by name."""
|
||||
from app.providers.binance import BinanceProvider
|
||||
from app.providers.alpaca import AlpacaProvider
|
||||
|
||||
registry: dict[str, type[MarketDataProvider]] = {
|
||||
"binance": BinanceProvider,
|
||||
"alpaca": AlpacaProvider,
|
||||
}
|
||||
cls = registry.get(name.lower())
|
||||
if cls is None:
|
||||
available = ", ".join(registry.keys())
|
||||
raise ValueError(f"Unknown provider '{name}'. Available: {available}")
|
||||
return cls()
|
||||
Reference in New Issue
Block a user