File size: 13,275 Bytes
d5b7ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Alpaca adapter β€” real Alpaca API for stocks."""

from __future__ import annotations

import logging
from datetime import datetime, timedelta, timezone

import pandas as pd

from trading_cli.execution.adapters.base import (
    AccountInfo,
    MarketClock,
    OrderResult,
    Position,
    TradingAdapter,
)
from trading_cli.execution.adapters.registry import register_adapter

logger = logging.getLogger(__name__)


@register_adapter
class AlpacaAdapter(TradingAdapter):
    """Alpaca Markets adapter for US equities (paper & live trading)."""

    def __init__(self, config: dict) -> None:
        self._config = config
        self._api_key = config.get("alpaca_api_key", "")
        self._api_secret = config.get("alpaca_api_secret", "")
        self._paper = config.get("alpaca_paper", True)
        self._demo = not (self._api_key and self._api_secret)

        if self._demo:
            logger.info("AlpacaAdapter: no API keys found, running in demo mode")
            return

        try:
            from alpaca.trading.client import TradingClient
            from alpaca.data.historical import StockHistoricalDataClient
            from alpaca.data.historical.news import NewsClient

            self._trading_client = TradingClient(
                api_key=self._api_key,
                secret_key=self._api_secret,
                paper=self._paper,
            )
            self._historical_client = StockHistoricalDataClient(
                api_key=self._api_key,
                secret_key=self._api_secret,
            )
            self._news_client = NewsClient(
                api_key=self._api_key,
                secret_key=self._api_secret,
            )
            logger.info("AlpacaAdapter connected (paper=%s)", self._paper)
        except ImportError as exc:
            raise RuntimeError("alpaca-py not installed. Run: uv add alpaca-py") from exc
        except Exception as exc:
            logger.error("Failed to connect to Alpaca: %s", exc)
            self._demo = True

    @property
    def adapter_id(self) -> str:
        return "alpaca"

    @property
    def supports_paper_trading(self) -> bool:
        return True

    @property
    def is_demo_mode(self) -> bool:
        return self._demo

    # ── Account & Positions ───────────────────────────────────────────────────

    def get_account(self) -> AccountInfo:
        if self._demo:
            return AccountInfo(
                equity=100000.0,
                cash=100000.0,
                buying_power=400000.0,
                portfolio_value=100000.0,
            )
        acct = self._trading_client.get_account()
        return AccountInfo(
            equity=float(acct.equity),
            cash=float(acct.cash),
            buying_power=float(acct.buying_power),
            portfolio_value=float(acct.portfolio_value),
        )

    def get_positions(self) -> list[Position]:
        if self._demo:
            return []
        raw = self._trading_client.get_all_positions()
        out = []
        for p in raw:
            out.append(
                Position(
                    symbol=p.symbol,
                    qty=float(p.qty),
                    avg_entry_price=float(p.avg_entry_price),
                    current_price=float(p.current_price),
                    unrealized_pl=float(p.unrealized_pl),
                    unrealized_plpc=float(p.unrealized_plpc),
                    market_value=float(p.market_value),
                    side=str(p.side),
                )
            )
        return out

    # ── Orders ───────────────────────────────────────────────────────────────

    def submit_market_order(self, symbol: str, qty: int, side: str) -> OrderResult:
        if self._demo:
            return OrderResult(
                order_id=f"DEMO-{datetime.now().timestamp()}",
                symbol=symbol,
                action=side,
                qty=qty,
                status="filled",
                filled_price=100.0,  # Mock price
            )

        from alpaca.trading.requests import MarketOrderRequest
        from alpaca.trading.enums import OrderSide, TimeInForce

        order_side = OrderSide.BUY if side.upper() == "BUY" else OrderSide.SELL
        req = MarketOrderRequest(
            symbol=symbol,
            qty=qty,
            side=order_side,
            time_in_force=TimeInForce.DAY,
        )
        try:
            order = self._trading_client.submit_order(order_data=req)
            filled_price = float(order.filled_avg_price) if order.filled_avg_price else None
            return OrderResult(
                order_id=str(order.id),
                symbol=symbol,
                action=side,
                qty=qty,
                status=str(order.status),
                filled_price=filled_price,
            )
        except Exception as exc:
            logger.error("Order submission failed for %s %s %d: %s", side, symbol, qty, exc)
            raise

    def close_position(self, symbol: str) -> OrderResult | None:
        if self._demo:
            return None
        try:
            response = self._trading_client.close_position(symbol)
            return OrderResult(
                order_id=str(response.id),
                symbol=symbol,
                action="SELL",
                qty=int(float(response.qty or 0)),
                status=str(response.status),
            )
        except Exception as exc:
            logger.error("Close position failed for %s: %s", symbol, exc)
            return None

    # ── Market Data ───────────────────────────────────────────────────────────

    def fetch_ohlcv(self, symbol: str, days: int = 90) -> pd.DataFrame:
        if self._demo:
            # Fallback to yfinance in demo mode
            from trading_cli.data.market import fetch_ohlcv_yfinance
            return fetch_ohlcv_yfinance(symbol, days)

        try:
            from alpaca.data.requests import StockBarsRequest
            from alpaca.data.timeframe import TimeFrame

            end = datetime.now(tz=timezone.utc)
            start = end - timedelta(days=days + 10)  # extra buffer for weekends

            request = StockBarsRequest(
                symbol_or_symbols=symbol,
                timeframe=TimeFrame.Day,
                start=start,
                end=end,
                feed="iex",
            )
            bars = self._historical_client.get_stock_bars(request)
            df = bars.df
            if isinstance(df.index, pd.MultiIndex):
                df = df.xs(symbol, level=0) if symbol in df.index.get_level_values(0) else df
            df.index = pd.to_datetime(df.index, utc=True)
            df = df.rename(columns={"open": "Open", "high": "High", "low": "Low",
                                     "close": "Close", "volume": "Volume"})
            return df.tail(days)
        except Exception as exc:
            logger.warning("Alpaca OHLCV fetch failed for %s: %s β€” falling back to yfinance", symbol, exc)
            from trading_cli.data.market import fetch_ohlcv_yfinance
            return fetch_ohlcv_yfinance(symbol, days)

    def get_latest_quote(self, symbol: str) -> float | None:
        if self._demo:
            return None
        try:
            from alpaca.data.requests import StockLatestTradeRequest

            req = StockLatestTradeRequest(symbol_or_symbols=symbol, feed="iex")
            trades = self._historical_client.get_stock_latest_trade(req)
            return float(trades[symbol].price)
        except Exception as exc:
            logger.warning("Alpaca latest quote failed for %s: %s", symbol, exc)
            return None

    def get_latest_quotes_batch(self, symbols: list[str]) -> dict[str, float]:
        if self._demo:
            return {}
        try:
            from alpaca.data.requests import StockLatestTradeRequest

            req = StockLatestTradeRequest(symbol_or_symbols=symbols, feed="iex")
            trades = self._historical_client.get_stock_latest_trade(req)
            return {sym: float(trade.price) for sym, trade in trades.items()}
        except Exception as exc:
            logger.warning("Batch Alpaca quote failed: %s", exc)
            return {}

    # ── Market Info ───────────────────────────────────────────────────────────

    def get_market_clock(self) -> MarketClock:
        if self._demo:
            now = datetime.now(tz=timezone.utc)
            hour_et = (now.hour - 5) % 24
            is_open = now.weekday() < 5 and 9 <= hour_et < 16
            return MarketClock(
                is_open=is_open,
                next_open="09:30 ET",
                next_close="16:00 ET",
            )
        try:
            clock = self._trading_client.get_clock()
            return MarketClock(
                is_open=clock.is_open,
                next_open=str(clock.next_open),
                next_close=str(clock.next_close),
            )
        except Exception as exc:
            logger.warning("get_market_clock failed: %s", exc)
            return MarketClock(is_open=False, next_open="Unknown", next_close="Unknown")

    # ── News ──────────────────────────────────────────────────────────────────

    def fetch_news(self, symbol: str, max_articles: int = 50,
                   days_ago: int = 0) -> list[tuple[str, float]]:
        if self._demo or not hasattr(self, '_news_client') or self._news_client is None:
            return []

        try:
            from alpaca.data.requests import NewsRequest

            now = datetime.now(tz=timezone.utc)
            target_date = now - timedelta(days=days_ago)
            day_start = target_date.replace(hour=0, minute=0, second=0, microsecond=0)
            day_end = target_date.replace(hour=23, minute=59, second=59)

            request = NewsRequest(
                symbols=symbol,
                start=day_start,
                end=day_end,
                limit=min(max_articles, 100),
            )
            response = self._news_client.get_news(request)
            items = getattr(response, "news", response) if response else []

            headlines: list[tuple[str, float]] = []
            for item in items:
                title = getattr(item, "headline", "") or getattr(item, "title", "")
                if not title:
                    continue
                created = getattr(item, "created_at", None) or getattr(item, "updated_at", None)
                if created:
                    import pandas as pd
                    ts = pd.Timestamp(created).timestamp() if isinstance(created, str) else float(created)
                else:
                    ts = now.timestamp()
                headlines.append((title, float(ts)))

            return headlines
        except Exception as exc:
            logger.warning("Alpaca news fetch failed for %s: %s", symbol, exc)
            return []

    # ── Asset Search ──────────────────────────────────────────────────────────

    def get_all_assets(self) -> list[dict[str, str]]:
        """Fetch all available assets with their symbols and company names.
        
        Returns:
            List of dicts with 'symbol' and 'name' keys.
        """
        if self._demo:
            # Return a basic hardcoded list for demo mode
            return [
                {"symbol": "AAPL", "name": "Apple Inc."},
                {"symbol": "TSLA", "name": "Tesla Inc."},
                {"symbol": "NVDA", "name": "NVIDIA Corporation"},
                {"symbol": "MSFT", "name": "Microsoft Corporation"},
                {"symbol": "AMZN", "name": "Amazon.com Inc."},
                {"symbol": "GOOGL", "name": "Alphabet Inc. Class A"},
                {"symbol": "META", "name": "Meta Platforms Inc."},
                {"symbol": "SPY", "name": "SPDR S&P 500 ETF Trust"},
            ]
        
        try:
            from alpaca.trading.requests import GetAssetsRequest
            from alpaca.trading.enums import AssetStatus, AssetClass
            
            # Get all active US equity assets
            request = GetAssetsRequest(
                status=AssetStatus.ACTIVE,
                asset_class=AssetClass.US_EQUITY,
            )
            assets = self._trading_client.get_all_assets(request)
            
            return [
                {"symbol": asset.symbol, "name": asset.name}
                for asset in assets
                if asset.tradable
            ]
        except Exception as exc:
            logger.warning("Failed to fetch assets: %s", exc)
            return []