stock-analysis-plugin 0.2.4 → 0.2.6

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.
@@ -3,6 +3,7 @@
3
3
 
4
4
  import argparse
5
5
  import json
6
+ import math
6
7
  import sys
7
8
  from concurrent.futures import ThreadPoolExecutor, as_completed
8
9
  from pathlib import Path
@@ -12,7 +13,24 @@ from _subproc import utf8_stdio
12
13
  from gather import gather_analysis
13
14
 
14
15
 
16
+ def has_analysis_data(data) -> bool:
17
+ if not isinstance(data, dict):
18
+ return False
19
+ for key, field in (("quote", "price"), ("kline", "close"), ("technical", "signal_score")):
20
+ payload = data.get(key)
21
+ rows = payload if isinstance(payload, list) else [payload]
22
+ for row in rows:
23
+ if not isinstance(row, dict) or "error" in row:
24
+ continue
25
+ value = row.get(field)
26
+ if isinstance(value, (int, float)) and math.isfinite(value) and (key == "technical" or value > 0):
27
+ return True
28
+ return False
29
+
30
+
15
31
  def analyze_watchlist(symbols: list[str], workers: int = 3) -> dict:
32
+ if isinstance(workers, bool) or not isinstance(workers, int) or workers <= 0:
33
+ return {"error": "workers must be a positive integer"}
16
34
  results = {}
17
35
  success = 0
18
36
  failed = 0
@@ -24,7 +42,10 @@ def analyze_watchlist(symbols: list[str], workers: int = 3) -> dict:
24
42
  try:
25
43
  data = future.result()
26
44
  results[sym] = data
27
- success += 1
45
+ if has_analysis_data(data):
46
+ success += 1
47
+ else:
48
+ failed += 1
28
49
  except Exception:
29
50
  results[sym] = None
30
51
  failed += 1
@@ -53,6 +74,8 @@ def main():
53
74
  result = analyze_watchlist(codes, workers=args.workers)
54
75
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2)
55
76
  print()
77
+ if "error" in result:
78
+ sys.exit(1)
56
79
  else:
57
80
  parser.print_help()
58
81
  sys.exit(1)
@@ -16,7 +16,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
16
16
  from _subproc import run_tool as _run_json # noqa: E402
17
17
  from _subproc import utf8_stdio
18
18
  from stock_data import detect_market # noqa: E402
19
- from watchlist import analyze_watchlist # noqa: E402
19
+ from watchlist import analyze_watchlist, has_analysis_data # noqa: E402
20
20
 
21
21
 
22
22
  def _suggest_next_tools(market: str, trend: str, anomalies: list, risk_level: str) -> list:
@@ -39,7 +39,7 @@ def _suggest_next_tools(market: str, trend: str, anomalies: list, risk_level: st
39
39
 
40
40
 
41
41
  def _build_item(symbol: str, data: dict | None) -> dict:
42
- if not data:
42
+ if not has_analysis_data(data):
43
43
  return {"symbol": symbol, "error": "no data"}
44
44
 
45
45
  tech = data.get("technical") or {}
@@ -52,7 +52,7 @@ def _build_item(symbol: str, data: dict | None) -> dict:
52
52
  score = tech.get("signal_score")
53
53
  trend = (tech.get("trend") or {}).get("overall") or "unknown"
54
54
  risk_level = risk.get("risk_level", "unknown")
55
- veto = bool(risk.get("veto_buy", False))
55
+ veto = risk.get("veto_buy")
56
56
  buy_signal = tech.get("buy_signal")
57
57
  market = detect_market(symbol)
58
58
 
@@ -79,6 +79,8 @@ def _build_item(symbol: str, data: dict | None) -> dict:
79
79
 
80
80
  def build_context(symbols: list[str], include_market_review: bool = False, workers: int = 3) -> dict:
81
81
  raw = analyze_watchlist(symbols, workers=workers)
82
+ if "error" in raw:
83
+ return raw
82
84
  results = raw.get("results", {})
83
85
 
84
86
  items = []
@@ -148,6 +150,8 @@ def main():
148
150
  result = build_context(codes, include_market_review=args.include_market_review, workers=args.workers)
149
151
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
150
152
  print()
153
+ if "error" in result:
154
+ sys.exit(1)
151
155
 
152
156
 
153
157
  if __name__ == "__main__":