@drico2008/fincli 0.1.9 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +909 -718
- package/fincli/__init__.py +3 -3
- package/fincli/app/agents/__init__.py +5 -0
- package/fincli/app/agents/registry.py +76 -0
- package/fincli/app/analysis/ai_prompts.py +23 -16
- package/fincli/app/analysis/analyzer.py +107 -100
- package/fincli/app/analysis/assistant_context.py +187 -186
- package/fincli/app/analysis/backtest.py +179 -0
- package/fincli/app/analysis/gameplay_plan.py +79 -0
- package/fincli/app/analysis/multi_timeframe.py +180 -0
- package/fincli/app/analysis/trading_methods.py +144 -0
- package/fincli/app/cli/commands.py +105 -83
- package/fincli/app/cli/router.py +2123 -1294
- package/fincli/app/connectors/__init__.py +5 -0
- package/fincli/app/connectors/catalog.py +148 -0
- package/fincli/app/connectors/news_connectors.py +412 -0
- package/fincli/app/modules/alerts.py +80 -0
- package/fincli/app/modules/economic_calendar.py +374 -1
- package/fincli/app/modules/reports.py +151 -0
- package/fincli/app/modules/scanner.py +111 -93
- package/fincli/app/modules/transactions.py +84 -84
- package/fincli/app/modules/user_profile.py +84 -0
- package/fincli/app/plugins/loader.py +72 -0
- package/fincli/app/providers/ai/manager.py +60 -60
- package/fincli/app/providers/market/alphavantage_provider.py +194 -0
- package/fincli/app/providers/market/base.py +98 -77
- package/fincli/app/providers/market/custom_provider.py +186 -169
- package/fincli/app/providers/market/manager.py +84 -1
- package/fincli/app/providers/market/symbols.py +143 -0
- package/fincli/app/providers/market/twelvedata_provider.py +167 -167
- package/fincli/app/research/__init__.py +7 -0
- package/fincli/app/research/engine.py +75 -0
- package/fincli/app/research/formatter.py +22 -0
- package/fincli/app/research/models.py +18 -0
- package/fincli/app/research/prompt_builder.py +47 -0
- package/fincli/app/services/macro_data.py +50 -0
- package/fincli/app/services/market_data.py +203 -203
- package/fincli/app/services/news_aggregator.py +90 -0
- package/fincli/app/services/web_research.py +267 -267
- package/fincli/app/storage/config.py +122 -88
- package/fincli/app/storage/database.py +200 -101
- package/fincli/app/storage/secrets.py +8 -2
- package/fincli/app/tui/components.py +68 -50
- package/fincli/app/tui/layout.py +269 -258
- package/fincli/app/tui/market_provider_selector.py +3 -1
- package/fincli/app/tui/theme.py +134 -74
- package/fincli/app/utils/formatting.py +123 -60
- package/package.json +23 -23
- package/pyproject.toml +35 -35
|
@@ -1,203 +1,203 @@
|
|
|
1
|
-
"""Market data service with provider fallback chain."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import asyncio
|
|
6
|
-
from concurrent.futures import ThreadPoolExecutor
|
|
7
|
-
from dataclasses import asdict
|
|
8
|
-
from datetime import datetime
|
|
9
|
-
from typing import Any, Awaitable
|
|
10
|
-
|
|
11
|
-
from fincli.app.providers.market.base import BaseMarketProvider, Candle, FundamentalSnapshot, NewsItem, ProviderStatus, Quote
|
|
12
|
-
from fincli.app.storage.market_cache import MarketCache
|
|
13
|
-
from fincli.app.utils.errors import ProviderError
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class MarketDataService:
|
|
17
|
-
"""Fetch market data through a prioritized provider chain."""
|
|
18
|
-
|
|
19
|
-
def __init__(
|
|
20
|
-
self,
|
|
21
|
-
providers: list[BaseMarketProvider],
|
|
22
|
-
cache: MarketCache | None = None,
|
|
23
|
-
cache_ttl_seconds: int = 300,
|
|
24
|
-
) -> None:
|
|
25
|
-
if not providers:
|
|
26
|
-
raise ProviderError("MarketDataService membutuhkan minimal satu provider.")
|
|
27
|
-
self.providers = providers
|
|
28
|
-
self.cache = cache
|
|
29
|
-
self.cache_ttl_seconds = cache_ttl_seconds
|
|
30
|
-
self.last_errors: list[str] = []
|
|
31
|
-
|
|
32
|
-
@property
|
|
33
|
-
def primary_provider(self) -> BaseMarketProvider:
|
|
34
|
-
return self.providers[0]
|
|
35
|
-
|
|
36
|
-
async def quote(self, symbol: str) -> Quote:
|
|
37
|
-
cache_key = self._cache_key(symbol)
|
|
38
|
-
cached = self._cache_get("quote", cache_key)
|
|
39
|
-
if isinstance(cached, dict):
|
|
40
|
-
return _quote_from_payload(cached)
|
|
41
|
-
quote = await self._with_fallback("quote", symbol)
|
|
42
|
-
self._cache_set("quote", cache_key, _quote_to_payload(quote))
|
|
43
|
-
return quote
|
|
44
|
-
|
|
45
|
-
async def history(self, symbol: str, period: str = "6mo", interval: str = "1d") -> list[Candle]:
|
|
46
|
-
cache_key = self._cache_key(symbol, period, interval)
|
|
47
|
-
cached = self._cache_get("history", cache_key)
|
|
48
|
-
if isinstance(cached, list):
|
|
49
|
-
return [_candle_from_payload(item) for item in cached if isinstance(item, dict)]
|
|
50
|
-
candles = await self._with_fallback("history", symbol, period, interval)
|
|
51
|
-
if candles:
|
|
52
|
-
self._cache_set("history", cache_key, [_candle_to_payload(candle) for candle in candles])
|
|
53
|
-
return candles
|
|
54
|
-
|
|
55
|
-
async def news(self, symbol: str, limit: int = 5) -> list[NewsItem]:
|
|
56
|
-
cache_key = self._cache_key(symbol, str(limit))
|
|
57
|
-
cached = self._cache_get("news", cache_key)
|
|
58
|
-
if isinstance(cached, list):
|
|
59
|
-
return [_news_from_payload(item) for item in cached if isinstance(item, dict)]
|
|
60
|
-
items = await self._with_fallback("news", symbol, limit)
|
|
61
|
-
self._cache_set("news", cache_key, [_news_to_payload(item) for item in items])
|
|
62
|
-
return items
|
|
63
|
-
|
|
64
|
-
async def fundamentals(self, symbol: str) -> FundamentalSnapshot:
|
|
65
|
-
cache_key = self._cache_key(symbol)
|
|
66
|
-
cached = self._cache_get("fundamentals", cache_key)
|
|
67
|
-
if isinstance(cached, dict):
|
|
68
|
-
return _fundamentals_from_payload(cached)
|
|
69
|
-
snapshot = await self._with_fallback("fundamentals", symbol)
|
|
70
|
-
self._cache_set("fundamentals", cache_key, _fundamentals_to_payload(snapshot))
|
|
71
|
-
return snapshot
|
|
72
|
-
|
|
73
|
-
async def status(self) -> ProviderStatus:
|
|
74
|
-
provider = self.primary_provider
|
|
75
|
-
try:
|
|
76
|
-
return await provider.status()
|
|
77
|
-
except Exception as exc: # noqa: BLE001
|
|
78
|
-
return ProviderStatus(
|
|
79
|
-
name=getattr(provider, "name", "unknown"),
|
|
80
|
-
realtime=False,
|
|
81
|
-
status="unavailable",
|
|
82
|
-
message=str(exc),
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
async def _with_fallback(self, method_name: str, *args: object) -> Any:
|
|
86
|
-
errors: list[str] = []
|
|
87
|
-
for provider in self.providers:
|
|
88
|
-
try:
|
|
89
|
-
method = getattr(provider, method_name)
|
|
90
|
-
return await method(*args)
|
|
91
|
-
except Exception as exc: # noqa: BLE001
|
|
92
|
-
errors.append(f"{getattr(provider, 'name', 'unknown')}: {exc}")
|
|
93
|
-
self.last_errors = errors
|
|
94
|
-
raise ProviderError(
|
|
95
|
-
f"Semua provider gagal untuk {method_name}.",
|
|
96
|
-
"\n".join(errors),
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
def _cache_key(self, symbol: str, *parts: object) -> str:
|
|
100
|
-
provider_chain = ",".join(provider.name for provider in self.providers)
|
|
101
|
-
normalized = [symbol.upper(), *(str(part).lower() for part in parts), f"providers={provider_chain}"]
|
|
102
|
-
return "|".join(normalized)
|
|
103
|
-
|
|
104
|
-
def _cache_get(self, namespace: str, cache_key: str) -> dict[str, Any] | list[Any] | None:
|
|
105
|
-
if self.cache is None:
|
|
106
|
-
return None
|
|
107
|
-
return self.cache.get(namespace, cache_key)
|
|
108
|
-
|
|
109
|
-
def _cache_set(self, namespace: str, cache_key: str, payload: dict[str, Any] | list[Any]) -> None:
|
|
110
|
-
if self.cache is None:
|
|
111
|
-
return
|
|
112
|
-
self.cache.set(namespace, cache_key, payload, self.cache_ttl_seconds)
|
|
113
|
-
|
|
114
|
-
def run(self, awaitable: Awaitable[Any]) -> Any:
|
|
115
|
-
try:
|
|
116
|
-
asyncio.get_running_loop()
|
|
117
|
-
except RuntimeError:
|
|
118
|
-
return asyncio.run(awaitable)
|
|
119
|
-
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
120
|
-
future = executor.submit(asyncio.run, awaitable)
|
|
121
|
-
return future.result()
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def _quote_to_payload(quote: Quote) -> dict[str, Any]:
|
|
125
|
-
payload = asdict(quote)
|
|
126
|
-
payload["timestamp"] = quote.timestamp.isoformat()
|
|
127
|
-
return payload
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
def _quote_from_payload(payload: dict[str, Any]) -> Quote:
|
|
131
|
-
return Quote(
|
|
132
|
-
symbol=str(payload["symbol"]),
|
|
133
|
-
price=None if payload.get("price") is None else float(payload["price"]),
|
|
134
|
-
currency=str(payload.get("currency", "USD")),
|
|
135
|
-
provider=str(payload.get("provider", "cache")),
|
|
136
|
-
timestamp=_parse_datetime(payload.get("timestamp")),
|
|
137
|
-
status=str(payload.get("status", "cached")),
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
def _candle_to_payload(candle: Candle) -> dict[str, Any]:
|
|
142
|
-
payload = asdict(candle)
|
|
143
|
-
payload["timestamp"] = candle.timestamp.isoformat()
|
|
144
|
-
return payload
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def _candle_from_payload(payload: dict[str, Any]) -> Candle:
|
|
148
|
-
return Candle(
|
|
149
|
-
timestamp=_parse_datetime(payload.get("timestamp")),
|
|
150
|
-
open=float(payload["open"]),
|
|
151
|
-
high=float(payload["high"]),
|
|
152
|
-
low=float(payload["low"]),
|
|
153
|
-
close=float(payload["close"]),
|
|
154
|
-
volume=float(payload.get("volume", 0)),
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
def _news_to_payload(item: NewsItem) -> dict[str, Any]:
|
|
159
|
-
payload = asdict(item)
|
|
160
|
-
payload["published_at"] = item.published_at.isoformat() if item.published_at else None
|
|
161
|
-
return payload
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
def _news_from_payload(payload: dict[str, Any]) -> NewsItem:
|
|
165
|
-
published_at = payload.get("published_at")
|
|
166
|
-
return NewsItem(
|
|
167
|
-
title=str(payload.get("title", "")),
|
|
168
|
-
source=str(payload.get("source", "")),
|
|
169
|
-
url=None if payload.get("url") is None else str(payload.get("url")),
|
|
170
|
-
published_at=None if published_at is None else _parse_datetime(published_at),
|
|
171
|
-
summary=str(payload.get("summary", "")),
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
def _fundamentals_to_payload(snapshot: FundamentalSnapshot) -> dict[str, Any]:
|
|
176
|
-
return asdict(snapshot)
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
def _fundamentals_from_payload(payload: dict[str, Any]) -> FundamentalSnapshot:
|
|
180
|
-
return FundamentalSnapshot(
|
|
181
|
-
symbol=str(payload["symbol"]),
|
|
182
|
-
provider=str(payload.get("provider", "cache")),
|
|
183
|
-
currency=str(payload.get("currency", "USD")),
|
|
184
|
-
market_cap=_optional_float(payload.get("market_cap")),
|
|
185
|
-
pe_ratio=_optional_float(payload.get("pe_ratio")),
|
|
186
|
-
eps=_optional_float(payload.get("eps")),
|
|
187
|
-
revenue=_optional_float(payload.get("revenue")),
|
|
188
|
-
beta=_optional_float(payload.get("beta")),
|
|
189
|
-
sector=None if payload.get("sector") is None else str(payload.get("sector")),
|
|
190
|
-
industry=None if payload.get("industry") is None else str(payload.get("industry")),
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
def _parse_datetime(value: object) -> datetime:
|
|
195
|
-
if isinstance(value, datetime):
|
|
196
|
-
return value
|
|
197
|
-
return datetime.fromisoformat(str(value))
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
def _optional_float(value: object) -> float | None:
|
|
201
|
-
if value is None:
|
|
202
|
-
return None
|
|
203
|
-
return float(value)
|
|
1
|
+
"""Market data service with provider fallback chain."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
7
|
+
from dataclasses import asdict
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from typing import Any, Awaitable
|
|
10
|
+
|
|
11
|
+
from fincli.app.providers.market.base import BaseMarketProvider, Candle, FundamentalSnapshot, NewsItem, ProviderStatus, Quote
|
|
12
|
+
from fincli.app.storage.market_cache import MarketCache
|
|
13
|
+
from fincli.app.utils.errors import ProviderError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MarketDataService:
|
|
17
|
+
"""Fetch market data through a prioritized provider chain."""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
providers: list[BaseMarketProvider],
|
|
22
|
+
cache: MarketCache | None = None,
|
|
23
|
+
cache_ttl_seconds: int = 300,
|
|
24
|
+
) -> None:
|
|
25
|
+
if not providers:
|
|
26
|
+
raise ProviderError("MarketDataService membutuhkan minimal satu provider.")
|
|
27
|
+
self.providers = providers
|
|
28
|
+
self.cache = cache
|
|
29
|
+
self.cache_ttl_seconds = cache_ttl_seconds
|
|
30
|
+
self.last_errors: list[str] = []
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def primary_provider(self) -> BaseMarketProvider:
|
|
34
|
+
return self.providers[0]
|
|
35
|
+
|
|
36
|
+
async def quote(self, symbol: str) -> Quote:
|
|
37
|
+
cache_key = self._cache_key(symbol)
|
|
38
|
+
cached = self._cache_get("quote", cache_key)
|
|
39
|
+
if isinstance(cached, dict):
|
|
40
|
+
return _quote_from_payload(cached)
|
|
41
|
+
quote = await self._with_fallback("quote", symbol)
|
|
42
|
+
self._cache_set("quote", cache_key, _quote_to_payload(quote))
|
|
43
|
+
return quote
|
|
44
|
+
|
|
45
|
+
async def history(self, symbol: str, period: str = "6mo", interval: str = "1d") -> list[Candle]:
|
|
46
|
+
cache_key = self._cache_key(symbol, period, interval)
|
|
47
|
+
cached = self._cache_get("history", cache_key)
|
|
48
|
+
if isinstance(cached, list):
|
|
49
|
+
return [_candle_from_payload(item) for item in cached if isinstance(item, dict)]
|
|
50
|
+
candles = await self._with_fallback("history", symbol, period, interval)
|
|
51
|
+
if candles:
|
|
52
|
+
self._cache_set("history", cache_key, [_candle_to_payload(candle) for candle in candles])
|
|
53
|
+
return candles
|
|
54
|
+
|
|
55
|
+
async def news(self, symbol: str, limit: int = 5) -> list[NewsItem]:
|
|
56
|
+
cache_key = self._cache_key(symbol, str(limit))
|
|
57
|
+
cached = self._cache_get("news", cache_key)
|
|
58
|
+
if isinstance(cached, list):
|
|
59
|
+
return [_news_from_payload(item) for item in cached if isinstance(item, dict)]
|
|
60
|
+
items = await self._with_fallback("news", symbol, limit)
|
|
61
|
+
self._cache_set("news", cache_key, [_news_to_payload(item) for item in items])
|
|
62
|
+
return items
|
|
63
|
+
|
|
64
|
+
async def fundamentals(self, symbol: str) -> FundamentalSnapshot:
|
|
65
|
+
cache_key = self._cache_key(symbol)
|
|
66
|
+
cached = self._cache_get("fundamentals", cache_key)
|
|
67
|
+
if isinstance(cached, dict):
|
|
68
|
+
return _fundamentals_from_payload(cached)
|
|
69
|
+
snapshot = await self._with_fallback("fundamentals", symbol)
|
|
70
|
+
self._cache_set("fundamentals", cache_key, _fundamentals_to_payload(snapshot))
|
|
71
|
+
return snapshot
|
|
72
|
+
|
|
73
|
+
async def status(self) -> ProviderStatus:
|
|
74
|
+
provider = self.primary_provider
|
|
75
|
+
try:
|
|
76
|
+
return await provider.status()
|
|
77
|
+
except Exception as exc: # noqa: BLE001
|
|
78
|
+
return ProviderStatus(
|
|
79
|
+
name=getattr(provider, "name", "unknown"),
|
|
80
|
+
realtime=False,
|
|
81
|
+
status="unavailable",
|
|
82
|
+
message=str(exc),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
async def _with_fallback(self, method_name: str, *args: object) -> Any:
|
|
86
|
+
errors: list[str] = []
|
|
87
|
+
for provider in self.providers:
|
|
88
|
+
try:
|
|
89
|
+
method = getattr(provider, method_name)
|
|
90
|
+
return await method(*args)
|
|
91
|
+
except Exception as exc: # noqa: BLE001
|
|
92
|
+
errors.append(f"{getattr(provider, 'name', 'unknown')}: {exc}")
|
|
93
|
+
self.last_errors = errors
|
|
94
|
+
raise ProviderError(
|
|
95
|
+
f"Semua provider gagal untuk {method_name}.",
|
|
96
|
+
"\n".join(errors),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def _cache_key(self, symbol: str, *parts: object) -> str:
|
|
100
|
+
provider_chain = ",".join(provider.name for provider in self.providers)
|
|
101
|
+
normalized = [symbol.upper(), *(str(part).lower() for part in parts), f"providers={provider_chain}"]
|
|
102
|
+
return "|".join(normalized)
|
|
103
|
+
|
|
104
|
+
def _cache_get(self, namespace: str, cache_key: str) -> dict[str, Any] | list[Any] | None:
|
|
105
|
+
if self.cache is None:
|
|
106
|
+
return None
|
|
107
|
+
return self.cache.get(namespace, cache_key)
|
|
108
|
+
|
|
109
|
+
def _cache_set(self, namespace: str, cache_key: str, payload: dict[str, Any] | list[Any]) -> None:
|
|
110
|
+
if self.cache is None:
|
|
111
|
+
return
|
|
112
|
+
self.cache.set(namespace, cache_key, payload, self.cache_ttl_seconds)
|
|
113
|
+
|
|
114
|
+
def run(self, awaitable: Awaitable[Any]) -> Any:
|
|
115
|
+
try:
|
|
116
|
+
asyncio.get_running_loop()
|
|
117
|
+
except RuntimeError:
|
|
118
|
+
return asyncio.run(awaitable)
|
|
119
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
120
|
+
future = executor.submit(asyncio.run, awaitable)
|
|
121
|
+
return future.result()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _quote_to_payload(quote: Quote) -> dict[str, Any]:
|
|
125
|
+
payload = asdict(quote)
|
|
126
|
+
payload["timestamp"] = quote.timestamp.isoformat()
|
|
127
|
+
return payload
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _quote_from_payload(payload: dict[str, Any]) -> Quote:
|
|
131
|
+
return Quote(
|
|
132
|
+
symbol=str(payload["symbol"]),
|
|
133
|
+
price=None if payload.get("price") is None else float(payload["price"]),
|
|
134
|
+
currency=str(payload.get("currency", "USD")),
|
|
135
|
+
provider=str(payload.get("provider", "cache")),
|
|
136
|
+
timestamp=_parse_datetime(payload.get("timestamp")),
|
|
137
|
+
status=str(payload.get("status", "cached")),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _candle_to_payload(candle: Candle) -> dict[str, Any]:
|
|
142
|
+
payload = asdict(candle)
|
|
143
|
+
payload["timestamp"] = candle.timestamp.isoformat()
|
|
144
|
+
return payload
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _candle_from_payload(payload: dict[str, Any]) -> Candle:
|
|
148
|
+
return Candle(
|
|
149
|
+
timestamp=_parse_datetime(payload.get("timestamp")),
|
|
150
|
+
open=float(payload["open"]),
|
|
151
|
+
high=float(payload["high"]),
|
|
152
|
+
low=float(payload["low"]),
|
|
153
|
+
close=float(payload["close"]),
|
|
154
|
+
volume=float(payload.get("volume", 0)),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _news_to_payload(item: NewsItem) -> dict[str, Any]:
|
|
159
|
+
payload = asdict(item)
|
|
160
|
+
payload["published_at"] = item.published_at.isoformat() if item.published_at else None
|
|
161
|
+
return payload
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _news_from_payload(payload: dict[str, Any]) -> NewsItem:
|
|
165
|
+
published_at = payload.get("published_at")
|
|
166
|
+
return NewsItem(
|
|
167
|
+
title=str(payload.get("title", "")),
|
|
168
|
+
source=str(payload.get("source", "")),
|
|
169
|
+
url=None if payload.get("url") is None else str(payload.get("url")),
|
|
170
|
+
published_at=None if published_at is None else _parse_datetime(published_at),
|
|
171
|
+
summary=str(payload.get("summary", "")),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _fundamentals_to_payload(snapshot: FundamentalSnapshot) -> dict[str, Any]:
|
|
176
|
+
return asdict(snapshot)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _fundamentals_from_payload(payload: dict[str, Any]) -> FundamentalSnapshot:
|
|
180
|
+
return FundamentalSnapshot(
|
|
181
|
+
symbol=str(payload["symbol"]),
|
|
182
|
+
provider=str(payload.get("provider", "cache")),
|
|
183
|
+
currency=str(payload.get("currency", "USD")),
|
|
184
|
+
market_cap=_optional_float(payload.get("market_cap")),
|
|
185
|
+
pe_ratio=_optional_float(payload.get("pe_ratio")),
|
|
186
|
+
eps=_optional_float(payload.get("eps")),
|
|
187
|
+
revenue=_optional_float(payload.get("revenue")),
|
|
188
|
+
beta=_optional_float(payload.get("beta")),
|
|
189
|
+
sector=None if payload.get("sector") is None else str(payload.get("sector")),
|
|
190
|
+
industry=None if payload.get("industry") is None else str(payload.get("industry")),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _parse_datetime(value: object) -> datetime:
|
|
195
|
+
if isinstance(value, datetime):
|
|
196
|
+
return value
|
|
197
|
+
return datetime.fromisoformat(str(value))
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _optional_float(value: object) -> float | None:
|
|
201
|
+
if value is None:
|
|
202
|
+
return None
|
|
203
|
+
return float(value)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""News aggregation helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timedelta, timezone
|
|
7
|
+
|
|
8
|
+
from fincli.app.connectors.news_connectors import NewsConnectorManager
|
|
9
|
+
from fincli.app.providers.market.base import NewsItem
|
|
10
|
+
from fincli.app.services.market_data import MarketDataService
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True, slots=True)
|
|
14
|
+
class NewsDesk:
|
|
15
|
+
symbol: str
|
|
16
|
+
provider_chain: tuple[str, ...]
|
|
17
|
+
items: list[NewsItem]
|
|
18
|
+
note: str
|
|
19
|
+
errors: tuple[str, ...] = ()
|
|
20
|
+
lookback_days: int | None = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class NewsAggregator:
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
market_service: MarketDataService,
|
|
27
|
+
news_connectors: NewsConnectorManager | None = None,
|
|
28
|
+
priority: list[str] | None = None,
|
|
29
|
+
) -> None:
|
|
30
|
+
self.market_service = market_service
|
|
31
|
+
self.news_connectors = news_connectors or NewsConnectorManager()
|
|
32
|
+
self.priority = priority or ["yfinance", "google_news_rss", "yahoo_finance_rss"]
|
|
33
|
+
|
|
34
|
+
async def latest(self, symbol: str, limit: int = 12, lookback_days: int | None = None) -> NewsDesk:
|
|
35
|
+
normalized = symbol.upper()
|
|
36
|
+
items: list[NewsItem] = []
|
|
37
|
+
errors: list[str] = []
|
|
38
|
+
seen: set[str] = set()
|
|
39
|
+
provider_chain = tuple(_dedupe(self.priority))
|
|
40
|
+
|
|
41
|
+
for provider in provider_chain:
|
|
42
|
+
try:
|
|
43
|
+
fetched = await self._fetch_provider(provider, normalized, max(limit - len(items), 1))
|
|
44
|
+
except Exception as exc: # noqa: BLE001 - fallback chain should continue
|
|
45
|
+
errors.append(f"{provider}: {exc}")
|
|
46
|
+
continue
|
|
47
|
+
for item in fetched:
|
|
48
|
+
if lookback_days is not None and not _within_lookback(item, lookback_days):
|
|
49
|
+
continue
|
|
50
|
+
key = (item.url or item.title).strip().lower()
|
|
51
|
+
if key and key not in seen:
|
|
52
|
+
seen.add(key)
|
|
53
|
+
items.append(item)
|
|
54
|
+
if len(items) >= limit:
|
|
55
|
+
break
|
|
56
|
+
if len(items) >= limit:
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
note = "Provider-backed news. Realtime/delayed status depends on provider entitlement."
|
|
60
|
+
if not items:
|
|
61
|
+
note = "No news returned by active providers. Try /research <symbol> --deep or configure /news_model priority."
|
|
62
|
+
elif errors:
|
|
63
|
+
note = f"{note} Fallback used after {len(errors)} provider error(s)."
|
|
64
|
+
return NewsDesk(normalized, provider_chain, items, note, tuple(errors), lookback_days)
|
|
65
|
+
|
|
66
|
+
async def _fetch_provider(self, provider: str, symbol: str, limit: int) -> list[NewsItem]:
|
|
67
|
+
if provider == "yfinance" or any(item.name == provider for item in self.market_service.providers):
|
|
68
|
+
return await self.market_service.news(symbol, limit=limit)
|
|
69
|
+
return await self.news_connectors.fetch(provider, symbol, limit=limit)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _dedupe(values: list[str]) -> list[str]:
|
|
73
|
+
seen: set[str] = set()
|
|
74
|
+
result: list[str] = []
|
|
75
|
+
for value in values:
|
|
76
|
+
normalized = value.strip().lower()
|
|
77
|
+
if normalized and normalized not in seen:
|
|
78
|
+
seen.add(normalized)
|
|
79
|
+
result.append(normalized)
|
|
80
|
+
return result
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _within_lookback(item: NewsItem, lookback_days: int) -> bool:
|
|
84
|
+
if item.published_at is None:
|
|
85
|
+
return True
|
|
86
|
+
published = item.published_at
|
|
87
|
+
if published.tzinfo is None:
|
|
88
|
+
published = published.replace(tzinfo=timezone.utc)
|
|
89
|
+
cutoff = datetime.now(timezone.utc) - timedelta(days=lookback_days)
|
|
90
|
+
return published >= cutoff
|