stock-analysis-plugin 0.2.6 → 0.2.7

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.
@@ -11,8 +11,9 @@ import re
11
11
  import sys
12
12
  import tempfile
13
13
  import time
14
- from datetime import date, datetime, timedelta
14
+ from datetime import date, datetime, timedelta, timezone
15
15
  from pathlib import Path
16
+ from zoneinfo import ZoneInfo
16
17
 
17
18
  sys.path.insert(0, str(Path(__file__).resolve().parent))
18
19
  from _cache import DAY, WEEK, cached # noqa: E402
@@ -96,7 +97,7 @@ _STICKY_CHAINS = {
96
97
  }
97
98
 
98
99
 
99
- def _failover(sources: list, label: str):
100
+ def _failover(sources: list, label: str, evidence: bool = False):
100
101
  """Return first truthy result; else raise RuntimeError aggregating each source's error.
101
102
 
102
103
  Sticky ordering (issue #25): for chains in _STICKY_CHAINS (keyed by the label
@@ -117,6 +118,21 @@ def _failover(sources: list, label: str):
117
118
  if result:
118
119
  if chain in _STICKY_CHAINS:
119
120
  _disk_cache_set(f"sticky-{chain}", name)
121
+ if evidence:
122
+ fetched_at = datetime.now(timezone.utc).isoformat()
123
+ rows = result if isinstance(result, list) else [result]
124
+ for row in rows:
125
+ if not isinstance(row, dict) or "error" in row:
126
+ continue
127
+ row.update(source=name, fetched_at=fetched_at)
128
+ row.setdefault("data_as_of", row.get("date"))
129
+ if chain.startswith("kline"):
130
+ row.setdefault(
131
+ "adjusted",
132
+ {"yfinance": "total_return", "alphavantage": "total_return", "longbridge": "qfq"}.get(
133
+ name, "unknown"
134
+ ),
135
+ )
120
136
  return result
121
137
  except Exception as e:
122
138
  errors.append(f"{name}: {type(e).__name__}: {scrub_secrets(str(e))}")
@@ -277,6 +293,20 @@ def _gbk_var_payload(content: bytes) -> str:
277
293
  return content.decode("gbk", errors="replace").split('="', 1)[-1].rsplit('"', 1)[0]
278
294
 
279
295
 
296
+ def _unix_quote_time(value) -> str | None:
297
+ if isinstance(value, (int, float)) and not isinstance(value, bool) and value > 0:
298
+ with contextlib.suppress(ValueError, OverflowError, OSError):
299
+ return datetime.fromtimestamp(value, timezone.utc).isoformat()
300
+ return None
301
+
302
+
303
+ def _quote_time(value: str, fmt: str) -> str | None:
304
+ try:
305
+ return datetime.strptime(value, fmt).replace(tzinfo=timezone(timedelta(hours=8))).isoformat()
306
+ except ValueError:
307
+ return None
308
+
309
+
280
310
  def _tencent_payload_row(f: list, symbol: str) -> dict:
281
311
  """Build the quote row from one Tencent `~`-separated payload (already split).
282
312
  Shared by _quote_tencent (single) and _tencent_batch_quotes (snapshot leg)."""
@@ -303,6 +333,7 @@ def _tencent_payload_row(f: list, symbol: str) -> dict:
303
333
  "turnover_rate": None if hk else _parse_float(f[38]),
304
334
  "volume_ratio": None if hk else _parse_float(f[49]),
305
335
  "amplitude": _parse_float(f[43]),
336
+ "data_as_of": _quote_time(f[30], "%Y/%m/%d %H:%M:%S" if hk else "%Y%m%d%H%M%S"),
306
337
  }
307
338
 
308
339
 
@@ -394,6 +425,7 @@ def _quote_sina(symbol: str) -> dict:
394
425
  "low": _parse_float(f[5]),
395
426
  "open": _parse_float(f[1]),
396
427
  "prev_close": prev_close,
428
+ "data_as_of": _quote_time(f"{f[30]} {f[31]}", "%Y-%m-%d %H:%M:%S") if len(f) > 31 else None,
397
429
  }
398
430
  if normalize_stock_code(symbol)["is_etf"]:
399
431
  row["is_etf"] = True
@@ -463,7 +495,7 @@ def _clean_row(d: dict) -> dict:
463
495
  # --------------- kline ---------------
464
496
 
465
497
 
466
- def kline_a(symbol: str, period: str, count: int) -> list:
498
+ def kline_a(symbol: str, period: str, count: int, evidence: bool = False) -> list:
467
499
  """A-share qfq kline: akshare → tushare → efinance → tencent → baostock.
468
500
 
469
501
  Explicit indices keep raw points; unadjusted stock sources never enter the chain."""
@@ -483,7 +515,7 @@ def kline_a(symbol: str, period: str, count: int) -> list:
483
515
  ("baostock", lambda: _kline_baostock(symbol, period, count)),
484
516
  ]
485
517
  with socket_timeout(25):
486
- return _failover(sources, label=f"kline_a:{symbol}")
518
+ return _failover(sources, label=f"kline_a:{symbol}", evidence=evidence)
487
519
 
488
520
 
489
521
  def _kline_akshare(symbol: str, period: str, count: int) -> list:
@@ -587,6 +619,7 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
587
619
  df = df.merge(factors, on="trade_date", how="left", validate="one_to_one")
588
620
  if df["adj_factor"].isna().any():
589
621
  raise ValueError("tushare adjustment factors missing for price dates")
622
+ df["adjustment_factor"] = df["adj_factor"] / latest_factor
590
623
  df[prices] = df[prices].mul(df["adj_factor"] / latest_factor, axis=0)
591
624
  else:
592
625
  df = df.drop(columns=prices, errors="ignore")
@@ -598,7 +631,11 @@ def _kline_tushare(symbol: str, period: str, count: int) -> list:
598
631
  df = df.sort_values("date")
599
632
  if "volume" in df.columns:
600
633
  df["volume"] = pd.to_numeric(df["volume"], errors="coerce")
601
- keep = [c for c in ["date", "open", "high", "low", "close", "volume", "turnover", "change_pct"] if c in df.columns]
634
+ keep = [
635
+ c
636
+ for c in ["date", "open", "high", "low", "close", "volume", "turnover", "change_pct", "adjustment_factor"]
637
+ if c in df.columns
638
+ ]
602
639
  df = df[keep].tail(count)
603
640
  return [_clean_row(r) for r in df.to_dict("records")]
604
641
 
@@ -832,11 +869,15 @@ def _quote_longbridge(symbol: str) -> dict:
832
869
  raise ValueError(f"longbridge returned no quote for {symbol}")
833
870
 
834
871
  q = quotes[0]
872
+ timestamp = getattr(q, "timestamp", None)
835
873
  prev_close = float(q.prev_close) if q.prev_close else None
836
874
  price = float(q.last_done) if q.last_done else None
837
875
  return _clean_row(
838
876
  {
839
877
  "symbol": symbol,
878
+ "data_as_of": timestamp.isoformat()
879
+ if isinstance(timestamp, datetime) and timestamp.utcoffset() is not None
880
+ else None,
840
881
  "name": getattr(q, "symbol", symbol),
841
882
  "price": price,
842
883
  "change": (price - prev_close) if price and prev_close else None,
@@ -905,6 +946,7 @@ def _kline_alphavantage(symbol: str, period: str, count: int) -> list:
905
946
  "high": float(vals["2. high"]) * factor,
906
947
  "low": float(vals["3. low"]) * factor,
907
948
  "close": adjusted,
949
+ "adjustment_factor": factor,
908
950
  "volume": int(vals.get("6. volume", vals.get("5. volume", 0))),
909
951
  }
910
952
  )
@@ -932,9 +974,13 @@ def _quote_alphavantage(symbol: str) -> dict:
932
974
  if not gq:
933
975
  raise ValueError(f"alphavantage quote failed for {symbol}")
934
976
 
977
+ trading_day = None
978
+ with contextlib.suppress(ValueError, TypeError):
979
+ trading_day = date.fromisoformat(gq.get("07. latest trading day")).isoformat()
935
980
  return _clean_row(
936
981
  {
937
982
  "symbol": symbol,
983
+ "data_as_of": trading_day,
938
984
  "price": float(gq.get("05. price", 0)),
939
985
  "change": float(gq.get("09. change", 0)),
940
986
  "change_pct": float(gq.get("10. change percent", "0").rstrip("%")),
@@ -947,7 +993,7 @@ def _quote_alphavantage(symbol: str) -> dict:
947
993
  )
948
994
 
949
995
 
950
- def _yf_failover(symbol: str, yfinance, finnhub, longbridge, alphavantage, label: str):
996
+ def _yf_failover(symbol: str, yfinance, finnhub, longbridge, alphavantage, label: str, evidence: bool = False):
951
997
  """Shared HK/US/JP/KR/TW failover chain: yfinance → finnhub/longbridge (HK/US) → alphavantage (US only).
952
998
 
953
999
  Each provider is a zero-arg callable; callers bind their own symbol/period/count.
@@ -958,10 +1004,10 @@ def _yf_failover(symbol: str, yfinance, finnhub, longbridge, alphavantage, label
958
1004
  sources += [("finnhub", finnhub), ("longbridge", longbridge)]
959
1005
  if market == "US":
960
1006
  sources.append(("alphavantage", alphavantage))
961
- return _failover(sources, label=label)
1007
+ return _failover(sources, label=label, evidence=evidence)
962
1008
 
963
1009
 
964
- def kline_yf(symbol: str, period: str, count: int) -> list:
1010
+ def kline_yf(symbol: str, period: str, count: int, evidence: bool = False) -> list:
965
1011
  """HK/US/JP/KR/TW kline. Failover: yfinance → finnhub/longbridge (HK/US) → alphavantage (US only)."""
966
1012
  return _yf_failover(
967
1013
  symbol,
@@ -970,6 +1016,7 @@ def kline_yf(symbol: str, period: str, count: int) -> list:
970
1016
  lambda: _kline_longbridge(symbol, period, count),
971
1017
  lambda: _kline_alphavantage(symbol, period, count),
972
1018
  label=f"kline:{symbol}",
1019
+ evidence=evidence,
973
1020
  )
974
1021
 
975
1022
 
@@ -977,12 +1024,12 @@ def cmd_kline(args):
977
1024
  market = detect_market(args.symbol)
978
1025
  try:
979
1026
  if market == "A":
980
- data = kline_a(args.symbol, args.period, args.count)
1027
+ data = kline_a(args.symbol, args.period, args.count, evidence=True)
981
1028
  if data:
982
1029
  adjusted = "none" if _A_INDEX_RE.fullmatch(args.symbol) else "qfq"
983
1030
  data = [dict(row, adjusted=adjusted) for row in data]
984
1031
  else:
985
- data = kline_yf(args.symbol, args.period, args.count)
1032
+ data = kline_yf(args.symbol, args.period, args.count, evidence=True)
986
1033
  return data
987
1034
  except Exception as e:
988
1035
  return {"error": str(e)}
@@ -991,7 +1038,7 @@ def cmd_kline(args):
991
1038
  # --------------- quote ---------------
992
1039
 
993
1040
 
994
- def quote_a(symbol: str) -> dict:
1041
+ def quote_a(symbol: str, evidence: bool = False) -> dict:
995
1042
  """A-share quote with failover: akshare → tushare → efinance → tencent → sina → pytdx. ETFs try fund spot first.
996
1043
 
997
1044
  Explicitly-prefixed symbols (sh000001, typically indices) go straight to
@@ -1001,7 +1048,7 @@ def quote_a(symbol: str) -> dict:
1001
1048
  if _A_PREFIXED_RE.fullmatch(symbol):
1002
1049
  sources = [("tencent", lambda: _quote_tencent(symbol)), ("sina", lambda: _quote_sina(symbol))]
1003
1050
  with socket_timeout(25):
1004
- return _failover(sources, label=f"quote_a:{symbol}")
1051
+ return _failover(sources, label=f"quote_a:{symbol}", evidence=evidence)
1005
1052
  is_etf = normalize_stock_code(symbol)["is_etf"]
1006
1053
  sources = []
1007
1054
  if is_etf:
@@ -1016,7 +1063,7 @@ def quote_a(symbol: str) -> dict:
1016
1063
  ]
1017
1064
  chain = "quote_a_etf" if is_etf else "quote_a"
1018
1065
  with socket_timeout(25):
1019
- return _failover(sources, label=f"{chain}:{symbol}")
1066
+ return _failover(sources, label=f"{chain}:{symbol}", evidence=evidence)
1020
1067
 
1021
1068
 
1022
1069
  def _quote_akshare_etf(symbol: str) -> dict:
@@ -1102,6 +1149,7 @@ def _quote_yfinance(symbol: str) -> dict:
1102
1149
  return _clean_row(
1103
1150
  {
1104
1151
  "symbol": symbol,
1152
+ "data_as_of": _unix_quote_time(info.get("regularMarketTime")),
1105
1153
  "name": info.get("shortName"),
1106
1154
  "price": info.get("regularMarketPrice") or info.get("currentPrice"),
1107
1155
  "change": info.get("regularMarketChange"),
@@ -1133,6 +1181,7 @@ def _quote_finnhub(symbol: str) -> dict:
1133
1181
  return _clean_row(
1134
1182
  {
1135
1183
  "symbol": symbol,
1184
+ "data_as_of": _unix_quote_time(q.get("t")),
1136
1185
  "price": q["c"],
1137
1186
  "change": q["d"],
1138
1187
  "change_pct": q["dp"],
@@ -1144,7 +1193,7 @@ def _quote_finnhub(symbol: str) -> dict:
1144
1193
  )
1145
1194
 
1146
1195
 
1147
- def quote_yf(symbol: str) -> dict:
1196
+ def quote_yf(symbol: str, evidence: bool = False) -> dict:
1148
1197
  """HK adds Tencent after yfinance; US/JP/KR/TW keep their existing failover chains."""
1149
1198
  if detect_market(symbol) == "HK":
1150
1199
  return _failover(
@@ -1155,6 +1204,7 @@ def quote_yf(symbol: str) -> dict:
1155
1204
  ("longbridge", lambda: _quote_longbridge(symbol)),
1156
1205
  ],
1157
1206
  label=f"quote:{symbol}",
1207
+ evidence=evidence,
1158
1208
  )
1159
1209
  return _yf_failover(
1160
1210
  symbol,
@@ -1163,6 +1213,7 @@ def quote_yf(symbol: str) -> dict:
1163
1213
  lambda: _quote_longbridge(symbol),
1164
1214
  lambda: _quote_alphavantage(symbol),
1165
1215
  label=f"quote:{symbol}",
1216
+ evidence=evidence,
1166
1217
  )
1167
1218
 
1168
1219
 
@@ -1192,6 +1243,7 @@ def _quote_stale_marker(market: str):
1192
1243
  as_of = prev[0]
1193
1244
  return {
1194
1245
  "as_of": as_of,
1246
+ "as_of_basis": "trading_calendar",
1195
1247
  "stale": True,
1196
1248
  "note": f"market is not in a live session; quote is the last completed trading day {as_of}'s close",
1197
1249
  }
@@ -1200,13 +1252,36 @@ def _quote_stale_marker(market: str):
1200
1252
  def cmd_quote(args):
1201
1253
  market = detect_market(args.symbol)
1202
1254
  try:
1203
- result = quote_a(args.symbol) if market == "A" else quote_yf(args.symbol)
1255
+ result = quote_a(args.symbol, evidence=True) if market == "A" else quote_yf(args.symbol, evidence=True)
1204
1256
  except Exception as e:
1205
1257
  return {"error": str(e)}
1206
1258
  if isinstance(result, dict) and "error" not in result:
1207
1259
  marker = _quote_stale_marker(market)
1208
1260
  if marker:
1209
1261
  result.update(marker)
1262
+ result.setdefault("data_as_of", None)
1263
+ if result["data_as_of"]:
1264
+ result["as_of"] = result["data_as_of"]
1265
+ result["as_of_basis"] = "provider"
1266
+ if marker:
1267
+ result["calendar_as_of"] = marker["as_of"]
1268
+ result["note"] = (
1269
+ "Quote time is supplied by the provider; the market calendar is a separate freshness hint"
1270
+ )
1271
+ try:
1272
+ from trading_calendar import market_phase
1273
+
1274
+ info = market_phase("CN" if market == "A" else market)
1275
+ value = result["data_as_of"]
1276
+ quoted_day = date.fromisoformat(value) if len(value) == 10 else None
1277
+ quoted = datetime.fromisoformat(value)
1278
+ if quoted.tzinfo:
1279
+ quoted_day = quoted.astimezone(ZoneInfo(info["timezone"])).date()
1280
+ if quoted_day and quoted_day.isoformat() < info["date"]:
1281
+ result["stale"] = True
1282
+ result["note"] = "Provider quote predates the current market date"
1283
+ except (ValueError, KeyError, TypeError, OSError):
1284
+ pass # Calendar/timestamp unavailable: retain the provider evidence without guessing.
1210
1285
  return result
1211
1286
 
1212
1287
 
@@ -14,7 +14,7 @@ from pathlib import Path
14
14
 
15
15
  sys.path.insert(0, str(Path(__file__).resolve().parent))
16
16
  from _subproc import run_tool as _run_json # noqa: E402
17
- from _subproc import utf8_stdio
17
+ from _subproc import scrub_secrets, utf8_stdio
18
18
  from stock_data import detect_market # noqa: E402
19
19
  from watchlist import analyze_watchlist, has_analysis_data # noqa: E402
20
20
 
@@ -46,8 +46,15 @@ def _build_item(symbol: str, data: dict | None) -> dict:
46
46
  risk = data.get("risk") or {}
47
47
 
48
48
  # anomalies — separate call (watchlist gather doesn't include them)
49
- anom_result = _run_json("anomaly_detect.py", ["detect", symbol]) or {}
50
- anomalies = anom_result.get("anomalies", []) if isinstance(anom_result, dict) else []
49
+ anom_result = _run_json("anomaly_detect.py", ["detect", symbol], preserve_errors=True)
50
+ anomaly_ok = (
51
+ isinstance(anom_result, dict) and "error" not in anom_result and isinstance(anom_result.get("anomalies"), list)
52
+ )
53
+ anomalies = anom_result["anomalies"] if anomaly_ok else []
54
+ anomaly_error = None
55
+ if not anomaly_ok:
56
+ error = anom_result.get("error") if isinstance(anom_result, dict) else None
57
+ anomaly_error = scrub_secrets(error) if isinstance(error, str) else "Anomaly data unavailable"
51
58
 
52
59
  score = tech.get("signal_score")
53
60
  trend = (tech.get("trend") or {}).get("overall") or "unknown"
@@ -72,7 +79,10 @@ def _build_item(symbol: str, data: dict | None) -> dict:
72
79
  "anomalies": [
73
80
  {"type": a.get("type"), "severity": a.get("severity"), "direction": a.get("direction")} for a in anomalies
74
81
  ],
75
- "anomaly_count": len(anomalies),
82
+ "anomaly_count": len(anomalies) if anomaly_ok else None,
83
+ "anomaly_status": "ok" if anomaly_ok else "unavailable",
84
+ "anomaly_error": anomaly_error,
85
+ "warnings": [f"Anomaly detection unavailable: {anomaly_error}"] if anomaly_error else [],
76
86
  "next_tools": _suggest_next_tools(market, trend, anomalies, risk_level),
77
87
  }
78
88
 
@@ -100,11 +110,12 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
100
110
  "fetched_at": datetime.now(timezone.utc).isoformat(),
101
111
  "freshness": "realtime_or_latest_trade_day",
102
112
  "fallback_used": False,
103
- "warnings": [],
113
+ "warnings": [f"{i['symbol']}: {warning}" for i in items for warning in i.get("warnings", [])],
104
114
  },
105
115
  "summary": {
106
116
  "total": len(symbols),
107
- "success": sum(1 for i in items if "error" not in i),
117
+ "success": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "ok"),
118
+ "partial": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "unavailable"),
108
119
  "failed": sum(1 for i in items if "error" in i),
109
120
  "high_risk_count": sum(1 for i in items if i.get("risk_level") == "high"),
110
121
  "veto_count": sum(1 for i in items if i.get("veto_buy")),
@@ -116,7 +127,7 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
116
127
  markets = sorted({i.get("market") for i in items if i.get("market")})
117
128
  review = {}
118
129
  for m in markets:
119
- r = _run_json("market_review.py", ["review", "--market", m], timeout=60)
130
+ r = _run_json("market_review.py", ["review", "--market", m])
120
131
  if r:
121
132
  review[m] = r
122
133
  output["market_review"] = review