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.
@@ -0,0 +1,61 @@
1
+ """TTL cache for slow public datasets; live quotes and errors are never stored."""
2
+
3
+ import contextlib
4
+ import hashlib
5
+ import json
6
+ import os
7
+ import tempfile
8
+ import time
9
+ from pathlib import Path
10
+
11
+ CACHE_DIR = Path.home() / ".stock-analysis" / "cache"
12
+ DAY = 86400
13
+ WEEK = 7 * DAY
14
+
15
+
16
+ def _namespace_prefix(namespace):
17
+ return hashlib.sha256(namespace.encode()).hexdigest()[:12]
18
+
19
+
20
+ def _read(path, ttl):
21
+ try:
22
+ entry = json.loads(path.read_text(encoding="utf-8"))
23
+ if 0 <= time.time() - entry["fetched_at"] < ttl:
24
+ return entry
25
+ except (OSError, ValueError, KeyError, TypeError):
26
+ pass
27
+ return None
28
+
29
+
30
+ def cached_values(namespace, ttl):
31
+ for path in CACHE_DIR.glob(f"{_namespace_prefix(namespace)}-*.json"):
32
+ entry = _read(path, ttl)
33
+ if entry and str(entry.get("key", "")).startswith(namespace + ":"):
34
+ yield entry.get("data")
35
+
36
+
37
+ def cached(key: str, ttl: int, fetch):
38
+ path = CACHE_DIR / f"{_namespace_prefix(key.partition(':')[0])}-{hashlib.sha256(key.encode()).hexdigest()}.json"
39
+ entry = _read(path, ttl)
40
+ if entry and "data" in entry:
41
+ return entry["data"]
42
+ result = fetch()
43
+ rows = result if isinstance(result, list) else [result]
44
+ if not result or any(isinstance(row, dict) and (row.get("error") or row.get("warnings")) for row in rows):
45
+ return result
46
+ tmp = None
47
+ try:
48
+ CACHE_DIR.mkdir(parents=True, exist_ok=True)
49
+ with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=CACHE_DIR, delete=False) as stream:
50
+ tmp = stream.name
51
+ json.dump(
52
+ {"key": key, "fetched_at": time.time(), "data": result}, stream, ensure_ascii=False, allow_nan=False
53
+ )
54
+ os.replace(tmp, path)
55
+ except (OSError, ValueError, TypeError):
56
+ pass
57
+ finally:
58
+ if tmp:
59
+ with contextlib.suppress(OSError):
60
+ Path(tmp).unlink(missing_ok=True)
61
+ return result
@@ -6,6 +6,7 @@ Does NOT store history, does NOT push, does NOT schedule. Host agent decides whe
6
6
 
7
7
  import argparse
8
8
  import json
9
+ import math
9
10
  import sys
10
11
  from concurrent.futures import ThreadPoolExecutor
11
12
  from datetime import datetime, timezone
@@ -31,8 +32,14 @@ RULE_TYPES = {
31
32
  def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) -> dict | None:
32
33
  rtype = rule.get("type")
33
34
  value = rule.get("value")
34
- if rtype not in RULE_TYPES:
35
+ if not isinstance(rtype, str) or rtype not in RULE_TYPES:
35
36
  return {"rule": rtype, "triggered": False, "error": f"unknown rule type: {rtype}"}
37
+ if rtype in ("price_below", "price_above", "change_pct_above", "change_pct_below", "volume_ratio_above"):
38
+ try:
39
+ if isinstance(value, bool) or not math.isfinite(float(value)):
40
+ raise ValueError
41
+ except (ValueError, TypeError):
42
+ return {"rule": rtype, "triggered": False, "error": "Rule value must be a finite number"}
36
43
 
37
44
  price = quote.get("price")
38
45
  change_pct = quote.get("change_pct")
@@ -90,6 +97,8 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
90
97
  "message": f"量比 {ratio} 超过 {value}",
91
98
  }
92
99
  elif rtype == "anomaly":
100
+ if not isinstance(value, str) or not value:
101
+ return {"rule": rtype, "triggered": False, "error": "Anomaly type must be a nonempty string"}
93
102
  # value is an anomaly type string, e.g. "macd_golden_cross"
94
103
  anomalies = (anom or {}).get("anomalies", [])
95
104
  for a in anomalies:
@@ -106,6 +115,8 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
106
115
  return {"rule": rtype, "triggered": True, "severity": "high", "message": "风险筛查触发一票否决"}
107
116
  elif rtype == "risk_level_at_least":
108
117
  order = {"low": 0, "medium": 1, "high": 2}
118
+ if not isinstance(value, str) or value not in order:
119
+ return {"rule": rtype, "triggered": False, "error": "Risk threshold must be low/medium/high"}
109
120
  cur = order.get((risk or {}).get("risk_level"))
110
121
  want = order.get(value)
111
122
  if cur is not None and want is not None and cur >= want:
@@ -122,7 +133,7 @@ def _check_rule(rule: dict, quote: dict, tech: dict, anom: dict, risk: dict) ->
122
133
 
123
134
 
124
135
  def check_rules(symbol: str, rules: list[dict]) -> dict:
125
- if not rules:
136
+ if not isinstance(rules, list) or not rules or any(not isinstance(rule, dict) for rule in rules):
126
137
  return {"symbol": symbol, "error": "no rules provided"}
127
138
 
128
139
  needs_tech = any(r.get("type") == "volume_ratio_above" for r in rules)
@@ -141,6 +152,9 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
141
152
  with ThreadPoolExecutor(max_workers=len(jobs)) as pool:
142
153
  futures = {key: pool.submit(_run_json, script, args) for key, (script, args) in jobs.items()}
143
154
  fetched = {key: future.result() for key, future in futures.items()}
155
+ fetched = {
156
+ key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
157
+ }
144
158
  quote = fetched["quote"] or {}
145
159
  tech = fetched.get("tech")
146
160
  anom = fetched.get("anom")
@@ -153,13 +167,25 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
153
167
  warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
154
168
  if needs_anom and not anom:
155
169
  warnings.append("anomaly fetch failed — anomaly rules cannot fire")
156
- if needs_risk and not risk:
170
+ if needs_risk and (not risk or risk.get("data_complete") is False or risk.get("risk_level") == "unknown"):
157
171
  warnings.append("risk screening fetch failed — risk rules cannot fire")
158
172
 
159
173
  hits = []
160
174
  evaluated = []
161
175
  for rule in rules:
162
- result = _check_rule(rule, quote, tech, anom, risk)
176
+ rtype = rule.get("type")
177
+ missing = (
178
+ (rtype in ("price_below", "price_above") and quote.get("price") is None)
179
+ or (rtype in ("change_pct_below", "change_pct_above") and quote.get("change_pct") is None)
180
+ or (rtype == "volume_ratio_above" and (tech or {}).get("volume", {}).get("volume_ratio") is None)
181
+ or (rtype == "anomaly" and not isinstance((anom or {}).get("anomalies"), list))
182
+ or (rtype == "risk_veto" and (risk or {}).get("veto_buy") is None)
183
+ or (rtype == "risk_level_at_least" and (risk or {}).get("risk_level") not in ("low", "medium", "high"))
184
+ )
185
+ if missing:
186
+ result = {"rule": rtype, "triggered": False, "error": "Required source data unavailable"}
187
+ else:
188
+ result = _check_rule(rule, quote, tech, anom, risk)
163
189
  if result is None:
164
190
  evaluated.append({"rule": rule.get("type"), "triggered": False})
165
191
  else:
@@ -214,6 +240,8 @@ def main():
214
240
  result = check_rules(args.symbol, rules)
215
241
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
216
242
  print()
243
+ if "error" in result:
244
+ sys.exit(1)
217
245
 
218
246
 
219
247
  if __name__ == "__main__":
package/tools/backtest.py CHANGED
@@ -16,6 +16,7 @@ import yaml
16
16
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
17
 
18
18
  from _subproc import json_safe, run_tool, utf8_stdio
19
+ from stock_data import detect_market
19
20
  from technical import bollinger_series, ema_series, ma_series, macd_series, rsi_series
20
21
 
21
22
  # --------------- Strategy Loading ---------------
@@ -67,7 +68,7 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
67
68
  if start and end:
68
69
  d0 = datetime.strptime(start, "%Y-%m-%d")
69
70
  d1 = datetime.strptime(end, "%Y-%m-%d")
70
- count = max((d1 - d0).days + 60, 300)
71
+ count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
71
72
 
72
73
  # run_tool gives venv-python + a bounded timeout + None on any failure
73
74
  # (non-zero exit, empty/invalid stdout); callers turn that into clean JSON.
@@ -251,7 +252,11 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
251
252
 
252
253
 
253
254
  def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> dict:
254
- market = "A" if re.match(r"^\d{6}$", symbol) else "OTHER"
255
+ if not math.isfinite(capital) or capital <= 0:
256
+ raise ValueError("capital must be finite and positive")
257
+ if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
258
+ raise ValueError("backtest prices must be finite and positive")
259
+ market = detect_market(symbol)
255
260
  lot_size = 100 if market == "A" else 1
256
261
  sim_cfg = strategy.get("simulation", {})
257
262
  slippage = float(sim_cfg.get("slippage", 0.001))
@@ -268,6 +273,8 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
268
273
  exit_ = strategy.get("exit", {})
269
274
  position_cfg = strategy.get("position", {})
270
275
  size_frac = float(position_cfg.get("size", 1.0))
276
+ if not 0 <= size_frac <= 1:
277
+ raise ValueError("position.size must be a fraction in [0, 1]")
271
278
  stop_loss = float(exit_.get("stop_loss", -0.10))
272
279
  take_profit = float(exit_.get("take_profit", 0.50))
273
280
  ts_val = exit_.get("trailing_stop")
@@ -297,7 +304,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
297
304
  if hit:
298
305
  buy_price = price * (1 + slippage)
299
306
  buy_amount = cash * size_frac
300
- shares = int(buy_amount / buy_price / lot_size) * lot_size
307
+ shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
301
308
  if shares <= 0:
302
309
  continue
303
310
  cost = shares * buy_price * (1 + commission)
@@ -417,7 +424,7 @@ def compute_metrics(trades: list, equity_curve: list, initial: float, final: flo
417
424
  years = 1
418
425
  annual_return = (1 + total_return) ** (1 / max(years, 0.01)) - 1 if total_return > -1 else total_return
419
426
 
420
- equities = pd.Series([e["equity"] for e in equity_curve], dtype=float)
427
+ equities = pd.Series([initial, *[e["equity"] for e in equity_curve]], dtype=float)
421
428
  peak = equities.cummax()
422
429
  dd = (equities - peak) / peak
423
430
  max_drawdown = float(dd.min()) if len(dd) > 0 else 0
@@ -653,6 +660,13 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
653
660
  if len(df) < 30:
654
661
  return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
655
662
 
663
+ requested_period = f"{start} to {end}"
664
+ actual_start = df["date"].iloc[0].strftime("%Y-%m-%d")
665
+ actual_end = df["date"].iloc[-1].strftime("%Y-%m-%d")
666
+ warnings = []
667
+ if actual_start != start or actual_end != end:
668
+ warnings.append(f"Actual data covers {actual_start} to {actual_end}; requested {requested_period}")
669
+ start, end = actual_start, actual_end
656
670
  sim = simulate(df, strategy, capital, symbol)
657
671
  final = sim["final_equity"]
658
672
  metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, final, start, end)
@@ -661,6 +675,8 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
661
675
  "strategy": strategy.get("name", "unnamed"),
662
676
  "symbol": symbol,
663
677
  "period": f"{start} to {end}",
678
+ "requested_period": requested_period,
679
+ "warnings": warnings,
664
680
  "initial_capital": capital,
665
681
  "final_capital": final,
666
682
  "costs": sim["costs"],
@@ -837,6 +853,8 @@ def main():
837
853
  sys.exit(1)
838
854
 
839
855
  print(json.dumps(json_safe(result), ensure_ascii=False, default=str))
856
+ if "error" in result:
857
+ sys.exit(1)
840
858
 
841
859
 
842
860
  if __name__ == "__main__":
@@ -18,6 +18,18 @@ ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
18
18
 
19
19
  # tool_name -> {markets: [supported markets], reason: str for unsupported}
20
20
  TOOL_MATRIX = {
21
+ "get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
22
+ "get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
23
+ "get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
24
+ "get_valuation_history": {"markets": ["A", "HK"], "reason": "A/HK historical valuation providers only"},
25
+ "get_index_valuation": {"markets": ["A"], "reason": "Chinese index valuation only"},
26
+ "get_earnings_calendar": {
27
+ "markets": ["A", "HK"],
28
+ "reason": "A-share disclosed calendar / HK single-symbol provider schedule",
29
+ },
30
+ "get_shareholder_structure": {"markets": ["A", "HK"], "reason": "A-share shareholder reports / HK southbound only"},
31
+ "get_etf_details": {"markets": ["A"], "reason": "A-share ETF portfolio/NAV/size only"},
32
+ "get_dividends": {"markets": ["A", "HK"], "reason": "A/HK dividend history and report-year ranking"},
21
33
  "get_kline": {"markets": ALL_MARKETS, "reason": None},
22
34
  "get_quote": {"markets": ALL_MARKETS, "reason": None},
23
35
  "get_news": {"markets": ALL_MARKETS, "reason": None},
@@ -27,6 +27,12 @@ PROVIDERS = {
27
27
  # see stock_data._kline_tushare) — the tushare package is not in requirements,
28
28
  # so availability is the token, not a package probe
29
29
  "tushare": ("requests", ["TUSHARE_TOKEN"], ["A"], "pure-HTTP API (api.tushare.pro); no tushare package needed"),
30
+ "lixinger": (
31
+ "requests",
32
+ ["LIXINGER_TOKEN"],
33
+ ["A"],
34
+ "optional index PE/PB history; subscription permissions not probed",
35
+ ),
30
36
  "efinance": ("efinance", [], ["A"], None),
31
37
  "tencent": ("requests", [], ["A"], "qt.gtimg.cn quote/kline, no auth (issue #25)"),
32
38
  "sina": (
package/tools/gather.py CHANGED
@@ -10,6 +10,7 @@ from pathlib import Path
10
10
  sys.path.insert(0, str(Path(__file__).resolve().parent))
11
11
  from _subproc import json_safe, run_tool, utf8_stdio # noqa: E402
12
12
  from stock_data import detect_market # noqa: E402
13
+ from technical import analyze_records # noqa: E402
13
14
 
14
15
 
15
16
  # gather fans out the heavier CLIs, so the default timeout stays 60s; raw stdout
@@ -32,36 +33,41 @@ def _parse_json(raw: str | None):
32
33
  return raw
33
34
 
34
35
 
35
- def _run_all(tasks: dict) -> dict:
36
+ def _run_all(tasks: dict, symbol: str) -> dict:
36
37
  with ThreadPoolExecutor(max_workers=len(tasks)) as pool:
37
38
  futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
38
- return {key: _parse_json(f.result()) for f, key in futures.items()}
39
+ result = {key: _parse_json(f.result()) for f, key in futures.items()}
40
+ result["technical"] = analyze_records(symbol, result["kline"]) if result["kline"] is not None else None
41
+ return result
39
42
 
40
43
 
41
44
  def gather_analysis(symbol: str) -> dict:
45
+ market = detect_market(symbol)
42
46
  tasks = {
43
47
  "quote": ("stock_data.py", ["quote", symbol]),
44
48
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "120"]),
45
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "120"]),
46
49
  "financials": ("stock_data.py", ["financials", symbol]),
47
50
  "capital_flow": ("stock_data.py", ["capital_flow", symbol]),
48
51
  "chip_distribution": ("stock_data.py", ["chip_distribution", symbol]),
49
52
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
50
53
  "social_sentiment": ("search_intel.py", ["sentiment", symbol]),
51
54
  "risk": ("risk_screening.py", ["screen", symbol]),
52
- "regime": ("market_regime.py", ["detect"]),
53
55
  }
54
- return _run_all(tasks)
56
+ if market in ("A", "HK", "US"):
57
+ tasks["regime"] = ("market_regime.py", ["detect", market])
58
+ result = _run_all(tasks, symbol)
59
+ if market not in ("A", "HK", "US"):
60
+ result["regime"] = {"error": f"Market regime unavailable for {market}"}
61
+ return result
55
62
 
56
63
 
57
64
  def gather_technical(symbol: str, kline_count: int = 120, with_quote: bool = False) -> dict:
58
65
  tasks = {
59
66
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(kline_count)]),
60
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", str(kline_count)]),
61
67
  }
62
68
  if with_quote:
63
69
  tasks["quote"] = ("stock_data.py", ["quote", symbol])
64
- return _run_all(tasks)
70
+ return _run_all(tasks, symbol)
65
71
 
66
72
 
67
73
  def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -> dict:
@@ -73,16 +79,20 @@ def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -
73
79
 
74
80
 
75
81
  def gather_fundamental(symbol: str) -> dict:
82
+ market = detect_market(symbol)
76
83
  tasks = {
77
84
  "quote": ("stock_data.py", ["quote", symbol]),
78
85
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "60"]),
79
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]),
80
86
  "financials": ("stock_data.py", ["financials", symbol]),
81
87
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
82
88
  "stock_info": ("stock_data.py", ["stock_info", symbol]),
83
- "sector_rankings": ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"]),
84
89
  }
85
- return _run_all(tasks)
90
+ if market == "A":
91
+ tasks["sector_rankings"] = ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"])
92
+ result = _run_all(tasks, symbol)
93
+ if market != "A":
94
+ result["sector_rankings"] = {"error": f"Sector rankings unavailable for {market}"}
95
+ return result
86
96
 
87
97
 
88
98
  def main():
@@ -0,0 +1,100 @@
1
+ """Optional Lixinger HK non-financial datasets (same env token as index data)."""
2
+
3
+ import os
4
+ from datetime import date, timedelta
5
+
6
+ import requests
7
+ from _cache import DAY, cached
8
+
9
+
10
+ def hk_data(symbol, dataset, metrics):
11
+ code = symbol.split(".")[0].zfill(5)
12
+ end = date.today()
13
+ try:
14
+ start = end.replace(year=end.year - 10)
15
+ except ValueError:
16
+ start = end.replace(year=end.year - 10, day=28) + timedelta(days=1)
17
+
18
+ def fetch():
19
+ response = requests.post(
20
+ f"https://open.lixinger.com/api/hk/company/{dataset}/non_financial",
21
+ json={
22
+ "token": os.environ["LIXINGER_TOKEN"],
23
+ "stockCodes": [code],
24
+ "startDate": start.isoformat(),
25
+ "endDate": end.isoformat(),
26
+ "metricsList": metrics,
27
+ },
28
+ timeout=20,
29
+ )
30
+ response.raise_for_status()
31
+ payload = response.json()
32
+ if payload.get("code") != 1 or not isinstance(payload.get("data"), list) or not payload["data"]:
33
+ raise ValueError("Lixinger HK non-financial data unavailable; check token permissions/company type")
34
+ return payload["data"]
35
+
36
+ return cached(f"lixinger-hk:{dataset}:{code}:{end}:{','.join(metrics)}", DAY, fetch)
37
+
38
+
39
+ def financials_hk(symbol, periods, annual):
40
+ fields = {
41
+ "roe": "m.roe",
42
+ "gross_margin": "ps.gp_m",
43
+ "net_profit_margin": "ps.np_s_r",
44
+ "total_revenue": "ps.toi",
45
+ "net_income": "ps.np",
46
+ "operating_cash_flow": "cfs.ncffoa",
47
+ }
48
+ data = hk_data(symbol, "fs", [f"q.{field}.t" for field in fields.values()])
49
+ history = []
50
+ for row in sorted(data, key=lambda r: r.get("date", ""), reverse=True):
51
+ if annual and row.get("reportType") != "annual_report":
52
+ continue
53
+ if not row.get("date") or row.get("stockCode", symbol.split(".")[0].zfill(5)) != symbol.split(".")[0].zfill(5):
54
+ continue
55
+ entry = {
56
+ "report_date": row["date"][:10],
57
+ "disclosure_date": (row.get("reportDate") or "")[:10] or None,
58
+ "report_type": row.get("reportType"),
59
+ "currency": row.get("currency"),
60
+ "source": "lixinger",
61
+ }
62
+ for key, field in fields.items():
63
+ table, metric = field.split(".")
64
+ value = row.get("q", {}).get(table, {}).get(metric, {}).get("t")
65
+ entry[key] = (
66
+ value * 100 if value is not None and key in ("roe", "gross_margin", "net_profit_margin") else value
67
+ )
68
+ if any(entry.get(key) is not None for key in fields):
69
+ history.append(entry)
70
+ if not history:
71
+ raise ValueError("Lixinger returned no matching financial periods")
72
+ history = history[:periods]
73
+ latest = history[0]
74
+ result = {
75
+ **dict.fromkeys(("name", "market_cap", "pe", "forward_pe", "pb", "debt_to_equity", "dividend_yield")),
76
+ "snapshot_available": False,
77
+ "symbol": symbol,
78
+ "source": "lixinger",
79
+ "report_date": latest["report_date"],
80
+ "currency": latest["currency"],
81
+ "total_revenue": latest["total_revenue"],
82
+ "net_income": latest["net_income"],
83
+ "roe": latest["roe"] / 100 if latest["roe"] is not None else None,
84
+ "profit_margin": latest["net_profit_margin"] / 100 if latest["net_profit_margin"] is not None else None,
85
+ "periods": history,
86
+ "requested_periods": periods,
87
+ "available_periods": len(history),
88
+ "history_units": {
89
+ "roe": "percent",
90
+ "gross_margin": "percent",
91
+ "net_profit_margin": "percent",
92
+ "total_revenue": "reported currency",
93
+ "net_income": "reported currency",
94
+ "operating_cash_flow": "reported currency",
95
+ },
96
+ "accounting_basis": "provider-reported HK financial statements; not normalized to A-share accounting",
97
+ }
98
+ if len(history) < periods:
99
+ result["warnings"] = ["Requested financial history incomplete; only actual available periods returned"]
100
+ return result
@@ -54,7 +54,9 @@ def _get_index_kline(market: str) -> list:
54
54
  def _compute_indicators(klines: list) -> dict:
55
55
  import numpy as np
56
56
 
57
- closes = np.array([float(k["close"]) for k in klines if k.get("close") is not None])
57
+ df = pd.DataFrame(klines).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
58
+ df = df[np.isfinite(df).all(axis=1) & (df > 0).all(axis=1)]
59
+ closes = df["close"].to_numpy()
58
60
  if len(closes) < 20:
59
61
  return {}
60
62
 
@@ -65,8 +67,8 @@ def _compute_indicators(klines: list) -> dict:
65
67
  ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else float(np.mean(closes))
66
68
  close = float(closes[-1])
67
69
 
68
- highs = np.array([float(k["high"]) for k in klines if k.get("high") is not None])
69
- lows = np.array([float(k["low"]) for k in klines if k.get("low") is not None])
70
+ highs = df["high"].to_numpy()
71
+ lows = df["low"].to_numpy()
70
72
 
71
73
  tr_values = []
72
74
  for i in range(-min(14, len(closes) - 1), 0):
@@ -226,6 +228,8 @@ def main():
226
228
  if args.command == "detect":
227
229
  result = detect_regime(args.market)
228
230
  print(json.dumps(result, ensure_ascii=False, default=str))
231
+ if "error" in result:
232
+ sys.exit(1)
229
233
 
230
234
 
231
235
  if __name__ == "__main__":
@@ -19,7 +19,7 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
19
19
 
20
20
  from _subproc import run_tool as _run_tool
21
21
  from _subproc import utf8_stdio
22
- from signal_tracker import _load_records
22
+ from signal_tracker import _append_record, _load_records, _store_lock
23
23
 
24
24
 
25
25
  def calc_temperature(stats: dict, indices: list) -> dict:
@@ -145,10 +145,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
145
145
  """Append one review report object to the JSONL archive (side effect of render --save)."""
146
146
  store = store or _default_store()
147
147
  try:
148
- store.parent.mkdir(parents=True, exist_ok=True)
149
- with store.open("a", encoding="utf-8") as f:
150
- f.write(json.dumps(report, ensure_ascii=False, default=str) + "\n")
151
- except OSError as e:
148
+ with _store_lock(store):
149
+ _append_record(store, report)
150
+ except (OSError, ValueError) as e:
152
151
  return {"error": str(e)}
153
152
  return {"saved": True, "date": report.get("review_date") or report.get("date")}
154
153
 
@@ -156,7 +155,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
156
155
  def cmd_history(args) -> dict:
157
156
  """Recent archived reviews, newest first. Empty store -> clean empty list."""
158
157
  try:
159
- records = _load_records(Path(args.store) if args.store else _default_store())
158
+ store = Path(args.store) if args.store else _default_store()
159
+ with _store_lock(store):
160
+ records = _load_records(store)
160
161
  except (OSError, UnicodeDecodeError) as e:
161
162
  # store is a directory / not UTF-8 — same clean-error contract as save_review
162
163
  return {"error": str(e)}
@@ -6,6 +6,7 @@ Not a portfolio system. Stateless: user passes cost/quantity/stops each call.
6
6
 
7
7
  import argparse
8
8
  import json
9
+ import math
9
10
  import sys
10
11
  from datetime import datetime, timezone
11
12
  from pathlib import Path
@@ -48,13 +49,28 @@ def analyze_position(
48
49
  stop_loss: float | None = None,
49
50
  take_profit: float | None = None,
50
51
  ) -> dict:
52
+ for field, value in (
53
+ ("cost", cost),
54
+ ("quantity", quantity),
55
+ ("stop_loss", stop_loss),
56
+ ("take_profit", take_profit),
57
+ ):
58
+ if value is not None and (not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0):
59
+ return {"symbol": symbol, "error": f"{field} must be finite and positive"}
51
60
  quote = _run_json("stock_data.py", ["quote", symbol]) or {}
52
61
  tech = _run_json("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]) or {}
53
62
 
54
- price = quote.get("price")
63
+ price = quote.get("price") if isinstance(quote, dict) and "error" not in quote else None
55
64
  if price is None:
56
65
  return {"symbol": symbol, "error": "cannot fetch current price"}
57
- price = float(price)
66
+ try:
67
+ price = float(price)
68
+ except (TypeError, ValueError):
69
+ return {"symbol": symbol, "error": "current price must be finite and positive"}
70
+ if not math.isfinite(price) or price <= 0:
71
+ return {"symbol": symbol, "error": "current price must be finite and positive"}
72
+ if not isinstance(tech, dict):
73
+ tech = {}
58
74
 
59
75
  pnl_per_share = price - cost
60
76
  pnl_pct = (pnl_per_share / cost * 100) if cost else 0.0
@@ -133,6 +149,8 @@ def main():
133
149
  )
134
150
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
135
151
  print()
152
+ if "error" in result:
153
+ sys.exit(1)
136
154
 
137
155
 
138
156
  if __name__ == "__main__":
@@ -112,6 +112,8 @@ def main():
112
112
  result["archived"] = save_review(report)
113
113
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
114
114
  print()
115
+ if "error" in result:
116
+ sys.exit(1)
115
117
 
116
118
 
117
119
  if __name__ == "__main__":