|
| 1 | +# Copyright 2024 QuantPod Contributors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Alpaca WebSocket market data adapter (tick-level). |
| 6 | +
|
| 7 | +Connects to Alpaca's real-time data stream via WebSocket and converts |
| 8 | +incoming trade/quote messages into Tick objects for the TickExecutor. |
| 9 | +
|
| 10 | +Alpaca provides two streams: |
| 11 | + - iex (free): IEX Exchange data, slight delay, no cost |
| 12 | + - sip (paid): Consolidated tape (SIP), best quality, requires paid subscription |
| 13 | +
|
| 14 | +Docs: https://docs.alpaca.markets/reference/marketdatastreaming |
| 15 | +
|
| 16 | +Prerequisites: |
| 17 | + pip install "websockets>=12" alpaca-py |
| 18 | +
|
| 19 | +Configuration: |
| 20 | + ALPACA_API_KEY — Alpaca API key |
| 21 | + ALPACA_API_SECRET — Alpaca secret key |
| 22 | + ALPACA_DATA_FEED — "iex" (default, free) or "sip" (paid) |
| 23 | +
|
| 24 | +Usage: |
| 25 | + bus = AlpacaWebSocketBus( |
| 26 | + api_key=os.getenv("ALPACA_API_KEY"), |
| 27 | + api_secret=os.getenv("ALPACA_API_SECRET"), |
| 28 | + symbols=["SPY", "QQQ", "AAPL"], |
| 29 | + feed="iex", |
| 30 | + ) |
| 31 | + tick_queue = asyncio.Queue(maxsize=10_000) |
| 32 | +
|
| 33 | + async with asyncio.TaskGroup() as tg: |
| 34 | + tg.create_task(bus.run(tick_queue)) |
| 35 | + tg.create_task(executor.run(tick_queue)) |
| 36 | +""" |
| 37 | + |
| 38 | +from __future__ import annotations |
| 39 | + |
| 40 | +import asyncio |
| 41 | +import json |
| 42 | +import os |
| 43 | +from datetime import datetime, timezone |
| 44 | +from typing import List, Optional |
| 45 | + |
| 46 | +from loguru import logger |
| 47 | + |
| 48 | +from quant_pod.data.market_data_bus import MarketDataBus |
| 49 | +from quant_pod.execution.tick_executor import Tick |
| 50 | + |
| 51 | + |
| 52 | +# Alpaca streaming endpoints |
| 53 | +_FEED_URLS = { |
| 54 | + "iex": "wss://stream.data.alpaca.markets/v2/iex", |
| 55 | + "sip": "wss://stream.data.alpaca.markets/v2/sip", |
| 56 | + "crypto": "wss://stream.data.alpaca.markets/v1beta3/crypto/us", |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +class AlpacaWebSocketBus(MarketDataBus): |
| 61 | + """ |
| 62 | + Connects to Alpaca's real-time WebSocket feed and pushes Tick objects. |
| 63 | +
|
| 64 | + Reconnects automatically with exponential backoff on disconnect. |
| 65 | + Subscribes to both trades (for price) and quotes (for bid/ask spread). |
| 66 | + The latest trade price is used as the tick price; bid/ask are attached |
| 67 | + from the most recent quote for the same symbol. |
| 68 | +
|
| 69 | + Thread-safety: designed for single asyncio task ownership. |
| 70 | + """ |
| 71 | + |
| 72 | + _RECONNECT_BASE_SECONDS = 1.0 |
| 73 | + _RECONNECT_MAX_SECONDS = 60.0 |
| 74 | + |
| 75 | + def __init__( |
| 76 | + self, |
| 77 | + symbols: List[str], |
| 78 | + api_key: Optional[str] = None, |
| 79 | + api_secret: Optional[str] = None, |
| 80 | + feed: str = "iex", |
| 81 | + ): |
| 82 | + super().__init__(symbols) |
| 83 | + self._api_key = api_key or os.getenv("ALPACA_API_KEY", "") |
| 84 | + self._api_secret = api_secret or os.getenv("ALPACA_API_SECRET", "") |
| 85 | + self._feed = feed |
| 86 | + self._url = _FEED_URLS.get(feed, _FEED_URLS["iex"]) |
| 87 | + |
| 88 | + # Latest quote per symbol for bid/ask enrichment of trade ticks |
| 89 | + self._latest_quotes: dict[str, dict] = {} |
| 90 | + |
| 91 | + if not self._api_key or not self._api_secret: |
| 92 | + raise ValueError( |
| 93 | + "ALPACA_API_KEY and ALPACA_API_SECRET must be set to use " |
| 94 | + "AlpacaWebSocketBus. Set them as environment variables." |
| 95 | + ) |
| 96 | + |
| 97 | + async def run(self, tick_queue: asyncio.Queue) -> None: |
| 98 | + """ |
| 99 | + Connect to Alpaca WebSocket, receive messages, and push Ticks. |
| 100 | +
|
| 101 | + Reconnects automatically with exponential backoff. |
| 102 | + """ |
| 103 | + self._running = True |
| 104 | + reconnect_wait = self._RECONNECT_BASE_SECONDS |
| 105 | + |
| 106 | + logger.info( |
| 107 | + f"[AlpacaWS] Starting stream: feed={self._feed} " |
| 108 | + f"symbols={self.symbols}" |
| 109 | + ) |
| 110 | + |
| 111 | + while self._running: |
| 112 | + try: |
| 113 | + await self._connect_and_stream(tick_queue) |
| 114 | + reconnect_wait = self._RECONNECT_BASE_SECONDS # reset on clean disconnect |
| 115 | + except Exception as e: |
| 116 | + if not self._running: |
| 117 | + break |
| 118 | + logger.warning( |
| 119 | + f"[AlpacaWS] Disconnected: {e} — " |
| 120 | + f"reconnecting in {reconnect_wait:.0f}s" |
| 121 | + ) |
| 122 | + await asyncio.sleep(reconnect_wait) |
| 123 | + reconnect_wait = min(reconnect_wait * 2, self._RECONNECT_MAX_SECONDS) |
| 124 | + |
| 125 | + await tick_queue.put(None) # sentinel |
| 126 | + logger.info("[AlpacaWS] Stopped") |
| 127 | + |
| 128 | + async def _connect_and_stream(self, tick_queue: asyncio.Queue) -> None: |
| 129 | + """Single connection lifecycle.""" |
| 130 | + try: |
| 131 | + import websockets # type: ignore[import] |
| 132 | + except ImportError: |
| 133 | + raise ImportError( |
| 134 | + "websockets package required for AlpacaWebSocketBus. " |
| 135 | + "Install with: pip install 'websockets>=12'" |
| 136 | + ) |
| 137 | + |
| 138 | + async with websockets.connect( |
| 139 | + self._url, |
| 140 | + ping_interval=20, |
| 141 | + ping_timeout=10, |
| 142 | + ) as ws: |
| 143 | + # Auth |
| 144 | + await ws.send(json.dumps({"action": "auth", "key": self._api_key, "secret": self._api_secret})) |
| 145 | + auth_resp = json.loads(await ws.recv()) |
| 146 | + self._check_auth(auth_resp) |
| 147 | + |
| 148 | + # Subscribe to trades and quotes for all symbols |
| 149 | + await ws.send(json.dumps({ |
| 150 | + "action": "subscribe", |
| 151 | + "trades": self.symbols, |
| 152 | + "quotes": self.symbols, |
| 153 | + })) |
| 154 | + |
| 155 | + logger.info(f"[AlpacaWS] Subscribed to {self.symbols}") |
| 156 | + |
| 157 | + async for raw in ws: |
| 158 | + if not self._running: |
| 159 | + break |
| 160 | + |
| 161 | + messages = json.loads(raw) |
| 162 | + if not isinstance(messages, list): |
| 163 | + messages = [messages] |
| 164 | + |
| 165 | + for msg in messages: |
| 166 | + tick = self._parse_message(msg) |
| 167 | + if tick is not None: |
| 168 | + try: |
| 169 | + tick_queue.put_nowait(tick) |
| 170 | + except asyncio.QueueFull: |
| 171 | + logger.warning( |
| 172 | + f"[AlpacaWS] Queue full — dropping tick for {tick.symbol}" |
| 173 | + ) |
| 174 | + |
| 175 | + def _check_auth(self, response: list) -> None: |
| 176 | + """Raise if authentication failed.""" |
| 177 | + for msg in (response if isinstance(response, list) else [response]): |
| 178 | + if msg.get("T") == "error": |
| 179 | + raise ConnectionError(f"Alpaca auth error: {msg.get('msg')}") |
| 180 | + if msg.get("T") == "success" and msg.get("msg") == "authenticated": |
| 181 | + return |
| 182 | + # No explicit error and no explicit success — assume ok (some versions omit) |
| 183 | + |
| 184 | + def _parse_message(self, msg: dict) -> Optional[Tick]: |
| 185 | + """Convert an Alpaca message dict to a Tick, or None if not a trade.""" |
| 186 | + msg_type = msg.get("T") |
| 187 | + |
| 188 | + if msg_type == "q": |
| 189 | + # Quote — cache bid/ask for enrichment; don't emit a tick |
| 190 | + sym = msg.get("S", "").upper() |
| 191 | + self._latest_quotes[sym] = msg |
| 192 | + return None |
| 193 | + |
| 194 | + if msg_type == "t": |
| 195 | + # Trade — emit a tick with price and volume |
| 196 | + sym = msg.get("S", "").upper() |
| 197 | + price = float(msg.get("p", 0)) |
| 198 | + volume = int(msg.get("s", 0)) |
| 199 | + ts_str = msg.get("t", "") |
| 200 | + |
| 201 | + try: |
| 202 | + ts = datetime.fromisoformat(ts_str.replace("Z", "+00:00")) |
| 203 | + except (ValueError, AttributeError): |
| 204 | + ts = datetime.now(timezone.utc) |
| 205 | + |
| 206 | + # Enrich with latest quote if available |
| 207 | + quote = self._latest_quotes.get(sym, {}) |
| 208 | + bid = float(quote["bp"]) if "bp" in quote else None |
| 209 | + ask = float(quote["ap"]) if "ap" in quote else None |
| 210 | + |
| 211 | + if price <= 0: |
| 212 | + return None |
| 213 | + |
| 214 | + return Tick( |
| 215 | + symbol=sym, |
| 216 | + price=price, |
| 217 | + volume=volume, |
| 218 | + bid=bid, |
| 219 | + ask=ask, |
| 220 | + timestamp=ts, |
| 221 | + ) |
| 222 | + |
| 223 | + return None |
0 commit comments