stock-analysis-plugin 0.2.5 → 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.
Files changed (45) hide show
  1. package/README.md +126 -15
  2. package/package.json +2 -1
  3. package/pi/index.ts +918 -72
  4. package/schemas/market_review_schema.json +12 -5
  5. package/schemas/report_schema.json +68 -4
  6. package/scripts/cli-timeout.mjs +27 -0
  7. package/scripts/json-input.mjs +26 -0
  8. package/scripts/setup-python.mjs +6 -6
  9. package/skills/bull-trend/SKILL.md +34 -10
  10. package/skills/bull-trend/references/fat-tails.md +91 -0
  11. package/skills/bull-trend/scripts/tail_analysis.py +132 -0
  12. package/skills/event-driven/SKILL.md +6 -0
  13. package/skills/growth-quality/SKILL.md +6 -0
  14. package/skills/market-review/SKILL.md +2 -0
  15. package/skills/stock-analysis/SKILL.md +25 -5
  16. package/skills/stock-analysis/references/stateless-analysis.md +66 -0
  17. package/skills/stock-screener/SKILL.md +7 -1
  18. package/skills/strategy-backtest/SKILL.md +13 -0
  19. package/skills/wisburg-research/SKILL.md +2 -0
  20. package/templates/market_review/full.md.j2 +7 -0
  21. package/templates/stock_analysis/_evidence.md.j2 +11 -0
  22. package/templates/stock_analysis/brief.md.j2 +6 -1
  23. package/templates/stock_analysis/full.md.j2 +8 -3
  24. package/tools/_subproc.py +66 -11
  25. package/tools/alert_rules.py +32 -4
  26. package/tools/backtest.py +95 -18
  27. package/tools/capabilities.py +11 -0
  28. package/tools/cli_budgets.json +26 -0
  29. package/tools/gather.py +24 -16
  30. package/tools/lixinger.py +100 -0
  31. package/tools/market_regime.py +7 -3
  32. package/tools/market_review.py +37 -15
  33. package/tools/position_context.py +20 -2
  34. package/tools/report_renderer.py +2 -0
  35. package/tools/risk_screening.py +45 -17
  36. package/tools/screener.py +74 -24
  37. package/tools/sec_edgar.py +346 -0
  38. package/tools/sec_holders.py +263 -0
  39. package/tools/signal_tracker.py +218 -24
  40. package/tools/stateless_analysis.py +630 -0
  41. package/tools/stock_data.py +170 -32
  42. package/tools/technical.py +16 -2
  43. package/tools/value_data.py +159 -0
  44. package/tools/watchlist.py +24 -1
  45. package/tools/watchlist_context.py +25 -10
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,11 +68,11 @@ 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
- # run_tool gives venv-python + a bounded timeout + None on any failure
73
- # (non-zero exit, empty/invalid stdout); callers turn that into clean JSON.
74
- data = run_tool("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)])
73
+ data = run_tool(
74
+ "stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], preserve_errors=True
75
+ )
75
76
  if data is None:
76
77
  raise RuntimeError(f"Failed to fetch kline for {symbol}")
77
78
  if isinstance(data, dict) and "error" in data:
@@ -190,7 +191,7 @@ def get_indicator(
190
191
 
191
192
 
192
193
  def check_condition(series: pd.Series, operator: str, value: float, idx: int) -> bool:
193
- if idx < 1 or idx >= len(series):
194
+ if idx < 0 or idx >= len(series):
194
195
  return False
195
196
  cur = float(series.iloc[idx])
196
197
  ops = {
@@ -202,6 +203,8 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
202
203
  }
203
204
  if operator in ops:
204
205
  return ops[operator]
206
+ if idx == 0:
207
+ return False
205
208
  prev = float(series.iloc[idx - 1])
206
209
  if operator == "cross_above":
207
210
  return prev <= value and cur > value
@@ -251,9 +254,21 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
251
254
 
252
255
 
253
256
  def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> dict:
254
- market = "A" if re.match(r"^\d{6}$", symbol) else "OTHER"
257
+ if not math.isfinite(capital) or capital <= 0:
258
+ raise ValueError("capital must be finite and positive")
259
+ if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
260
+ raise ValueError("backtest prices must be finite and positive")
261
+ dates = pd.to_datetime(df["date"], errors="coerce")
262
+ if dates.isna().any() or dates.duplicated().any() or not dates.is_monotonic_increasing:
263
+ raise ValueError("backtest dates must be valid, unique and increasing")
264
+ market = detect_market(symbol)
255
265
  lot_size = 100 if market == "A" else 1
256
266
  sim_cfg = strategy.get("simulation", {})
267
+ execution = sim_cfg.get("execution", "next_open")
268
+ if execution not in ("next_open", "same_close"):
269
+ raise ValueError("simulation.execution must be next_open or same_close")
270
+ if execution == "next_open" and ("open" not in df or not np.isfinite(df["open"]).all() or (df["open"] <= 0).any()):
271
+ raise ValueError("next_open requires finite positive open prices")
257
272
  slippage = float(sim_cfg.get("slippage", 0.001))
258
273
  commission = float(sim_cfg.get("commission", 0.0015))
259
274
  tax_raw = sim_cfg.get("stamp_tax") # sell-side only (e.g. A股印花税); absent/empty = 0 (disabled)
@@ -268,6 +283,8 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
268
283
  exit_ = strategy.get("exit", {})
269
284
  position_cfg = strategy.get("position", {})
270
285
  size_frac = float(position_cfg.get("size", 1.0))
286
+ if not 0 <= size_frac <= 1:
287
+ raise ValueError("position.size must be a fraction in [0, 1]")
271
288
  stop_loss = float(exit_.get("stop_loss", -0.10))
272
289
  take_profit = float(exit_.get("take_profit", 0.50))
273
290
  ts_val = exit_.get("trailing_stop")
@@ -286,19 +303,43 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
286
303
  trade_id = 0
287
304
  commission_paid = 0.0
288
305
  stamp_tax_paid = 0.0
306
+ pending = None
307
+ blocked_orders = 0
308
+
309
+ def tradable(row, side):
310
+ if "volume" in row and (not pd.notna(row["volume"]) or row["volume"] <= 0):
311
+ return False
312
+ limit = row.get("limit_up" if side == "buy" else "limit_down")
313
+ # shortcut: limits need supplied historical prices; add verified limit series before enforcing provider bars.
314
+ return not (
315
+ limit is not None
316
+ and pd.notna(limit)
317
+ and row.get("high") == row.get("low")
318
+ and abs(float(row["high"]) - float(limit)) < 0.005
319
+ )
289
320
 
290
321
  for i in range(1, len(df)):
291
322
  row = df.iloc[i]
292
323
  date_str = str(row["date"].date()) if hasattr(row["date"], "date") else str(row["date"])
293
- price = float(row["close"])
324
+ signal_idx = i if execution == "same_close" else i - 1
325
+ price = float(df["close"].iloc[signal_idx])
326
+ fill_price = float(row["close"] if execution == "same_close" else row["open"])
327
+ signal_date = str(pd.Timestamp(df["date"].iloc[signal_idx]).date())
294
328
 
295
329
  if holding is None:
296
- hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, i, cache)
297
- if hit:
298
- buy_price = price * (1 + slippage)
330
+ hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, signal_idx, cache)
331
+ if pending:
332
+ hit, reason, signal_date = True, pending[0], pending[1]
333
+ if hit and not tradable(row, "buy"):
334
+ pending = (reason, signal_date)
335
+ blocked_orders += 1
336
+ if hit and tradable(row, "buy"):
337
+ pending = None
338
+ buy_price = fill_price * (1 + slippage)
299
339
  buy_amount = cash * size_frac
300
- shares = int(buy_amount / buy_price / lot_size) * lot_size
340
+ shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
301
341
  if shares <= 0:
342
+ equity_curve.append({"date": date_str, "equity": round(cash, 2)})
302
343
  continue
303
344
  cost = shares * buy_price * (1 + commission)
304
345
  commission_paid += shares * buy_price * commission
@@ -310,13 +351,14 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
310
351
  "buy_price": round(buy_price, 4),
311
352
  "shares": shares,
312
353
  "cost": cost,
313
- "peak_close": price,
354
+ "peak_close": float(row["close"]),
314
355
  }
315
356
  trades.append(
316
357
  {
317
358
  "id": trade_id,
318
359
  "type": "buy",
319
360
  "date": date_str,
361
+ "signal_date": signal_date,
320
362
  "price": round(buy_price, 2),
321
363
  "shares": shares,
322
364
  "amount": round(cost, 2),
@@ -341,10 +383,16 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
341
383
  elif pnl_pct >= take_profit:
342
384
  sell, reason = True, f"止盈触发 ({round(pnl_pct * 100, 1)}% >= {round(take_profit * 100, 1)}%)"
343
385
  elif exit_conditions:
344
- sell, reason = evaluate_conditions(df, exit_conditions, exit_logic, i, cache)
345
-
346
- if sell:
347
- sell_price = price * (1 - slippage)
386
+ sell, reason = evaluate_conditions(df, exit_conditions, exit_logic, signal_idx, cache)
387
+
388
+ if pending:
389
+ sell, reason, signal_date = True, pending[0], pending[1]
390
+ if sell and not tradable(row, "sell"):
391
+ pending = (reason, signal_date)
392
+ blocked_orders += 1
393
+ if sell and tradable(row, "sell"):
394
+ pending = None
395
+ sell_price = fill_price * (1 - slippage)
348
396
  gross = holding["shares"] * sell_price
349
397
  proceeds = gross * (1 - commission - stamp_tax)
350
398
  commission_paid += gross * commission
@@ -364,6 +412,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
364
412
  "id": trade_id_sell,
365
413
  "type": "sell",
366
414
  "date": date_str,
415
+ "signal_date": signal_date,
367
416
  "price": round(sell_price, 2),
368
417
  "shares": holding["shares"],
369
418
  "amount": round(proceeds, 2),
@@ -377,7 +426,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
377
426
 
378
427
  equity = cash
379
428
  if holding:
380
- equity += holding["shares"] * price
429
+ equity += holding["shares"] * float(row["close"])
381
430
  equity_curve.append({"date": date_str, "equity": round(equity, 2)})
382
431
 
383
432
  if holding:
@@ -389,6 +438,22 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
389
438
  "trades": trades,
390
439
  "equity_curve": equity_curve,
391
440
  "final_equity": equity_curve[-1]["equity"] if equity_curve else capital,
441
+ "execution": {
442
+ "model": execution,
443
+ "blocked_orders": blocked_orders,
444
+ "unfilled_order": pending is not None,
445
+ "limitations": [
446
+ "Stops are close signals, not intraday guaranteed stop prices",
447
+ "Historical price limits are enforced only when limit_up/limit_down are supplied",
448
+ "Queue priority, partial fills, market impact and settlement are not simulated",
449
+ "Adjusted OHLC are synthetic prices; corporate actions and cash dividends are not simulated separately",
450
+ ]
451
+ + (
452
+ ["same_close uses the signal day's close and can overstate executable returns"]
453
+ if execution == "same_close"
454
+ else []
455
+ ),
456
+ },
392
457
  "costs": {
393
458
  "slippage": slippage,
394
459
  "commission": commission,
@@ -417,7 +482,7 @@ def compute_metrics(trades: list, equity_curve: list, initial: float, final: flo
417
482
  years = 1
418
483
  annual_return = (1 + total_return) ** (1 / max(years, 0.01)) - 1 if total_return > -1 else total_return
419
484
 
420
- equities = pd.Series([e["equity"] for e in equity_curve], dtype=float)
485
+ equities = pd.Series([initial, *[e["equity"] for e in equity_curve]], dtype=float)
421
486
  peak = equities.cummax()
422
487
  dd = (equities - peak) / peak
423
488
  max_drawdown = float(dd.min()) if len(dd) > 0 else 0
@@ -653,6 +718,13 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
653
718
  if len(df) < 30:
654
719
  return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
655
720
 
721
+ requested_period = f"{start} to {end}"
722
+ actual_start = df["date"].iloc[0].strftime("%Y-%m-%d")
723
+ actual_end = df["date"].iloc[-1].strftime("%Y-%m-%d")
724
+ warnings = []
725
+ if actual_start != start or actual_end != end:
726
+ warnings.append(f"Actual data covers {actual_start} to {actual_end}; requested {requested_period}")
727
+ start, end = actual_start, actual_end
656
728
  sim = simulate(df, strategy, capital, symbol)
657
729
  final = sim["final_equity"]
658
730
  metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, final, start, end)
@@ -661,9 +733,12 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
661
733
  "strategy": strategy.get("name", "unnamed"),
662
734
  "symbol": symbol,
663
735
  "period": f"{start} to {end}",
736
+ "requested_period": requested_period,
737
+ "warnings": warnings,
664
738
  "initial_capital": capital,
665
739
  "final_capital": final,
666
740
  "costs": sim["costs"],
741
+ "execution": sim["execution"],
667
742
  "metrics": metrics,
668
743
  "trades": sim["trades"],
669
744
  "equity_curve": sample_curve(sim["equity_curve"]),
@@ -837,6 +912,8 @@ def main():
837
912
  sys.exit(1)
838
913
 
839
914
  print(json.dumps(json_safe(result), ensure_ascii=False, default=str))
915
+ if "error" in result:
916
+ sys.exit(1)
840
917
 
841
918
 
842
919
  if __name__ == "__main__":
@@ -18,6 +18,17 @@ 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
+ # Explicit-input analytics; data acquisition retains each provider's market limits.
22
+ "compare_stocks": {"markets": ALL_MARKETS, "reason": None},
23
+ "analyze_disclosure_changes": {"markets": ALL_MARKETS, "reason": None},
24
+ "analyze_portfolio_risk": {"markets": ALL_MARKETS, "reason": None},
25
+ "calculate_position_size": {"markets": ALL_MARKETS, "reason": None},
26
+ "analyze_event_study": {"markets": ALL_MARKETS, "reason": None},
27
+ "validate_backtest": {"markets": ALL_MARKETS, "reason": None},
28
+ "compare_analysis_snapshots": {"markets": ALL_MARKETS, "reason": None},
29
+ "get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
30
+ "get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
31
+ "get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
21
32
  "get_valuation_history": {"markets": ["A", "HK"], "reason": "A/HK historical valuation providers only"},
22
33
  "get_index_valuation": {"markets": ["A"], "reason": "Chinese index valuation only"},
23
34
  "get_earnings_calendar": {
@@ -0,0 +1,26 @@
1
+ {
2
+ "default": 120,
3
+ "stock_data.py:market_snapshot": 240,
4
+ "screener.py:screen": 390,
5
+ "sec_edgar.py:holdings": 300,
6
+ "technical.py:analyze": 150,
7
+ "market_regime.py:detect": 270,
8
+ "backtest.py:run": 270,
9
+ "risk_screening.py:screen": 450,
10
+ "signal_tracker.py:evaluate": 300,
11
+ "stock_data.py:market_stats": 240,
12
+ "market_review.py:review": 300,
13
+ "anomaly_detect.py:detect": 390,
14
+ "position_context.py:analyze": 300,
15
+ "alert_rules.py:check": 480,
16
+ "technical.py:calculate_ma": 150,
17
+ "value_data.py:dividends": 300,
18
+ "value_data.py:etf_details": 450,
19
+ "watchlist.py:analyze": 30,
20
+ "watchlist_context.py:build": 30,
21
+ "l2_batch": 360,
22
+ "l2_workers": 6,
23
+ "watchlist_item": 480,
24
+ "stateless_analysis.py:event_study": 300,
25
+ "stateless_analysis.py:robustness": 660
26
+ }
package/tools/gather.py CHANGED
@@ -8,14 +8,13 @@ from concurrent.futures import ThreadPoolExecutor
8
8
  from pathlib import Path
9
9
 
10
10
  sys.path.insert(0, str(Path(__file__).resolve().parent))
11
- from _subproc import json_safe, run_tool, utf8_stdio # noqa: E402
11
+ from _subproc import command_timeout, 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
- # gather fans out the heavier CLIs, so the default timeout stays 60s; raw stdout
16
- # wanted here (parsing is _parse_json's job); def keeps timeout kw-passable.
17
- def _run(script, args, timeout=60):
18
- return run_tool(script, args, timeout=timeout, parse_json=False)
16
+ def _run(script, args, timeout=None):
17
+ return run_tool(script, args, timeout=timeout, parse_json=False, preserve_errors=True)
19
18
 
20
19
 
21
20
  def _news_query(symbol: str) -> str:
@@ -32,57 +31,66 @@ def _parse_json(raw: str | None):
32
31
  return raw
33
32
 
34
33
 
35
- def _run_all(tasks: dict) -> dict:
34
+ def _run_all(tasks: dict, symbol: str) -> dict:
36
35
  with ThreadPoolExecutor(max_workers=len(tasks)) as pool:
37
36
  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()}
37
+ result = {key: _parse_json(f.result()) for f, key in futures.items()}
38
+ result["technical"] = analyze_records(symbol, result["kline"]) if result["kline"] is not None else None
39
+ return result
39
40
 
40
41
 
41
42
  def gather_analysis(symbol: str) -> dict:
43
+ market = detect_market(symbol)
42
44
  tasks = {
43
45
  "quote": ("stock_data.py", ["quote", symbol]),
44
46
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "120"]),
45
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "120"]),
46
47
  "financials": ("stock_data.py", ["financials", symbol]),
47
48
  "capital_flow": ("stock_data.py", ["capital_flow", symbol]),
48
49
  "chip_distribution": ("stock_data.py", ["chip_distribution", symbol]),
49
50
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
50
51
  "social_sentiment": ("search_intel.py", ["sentiment", symbol]),
51
52
  "risk": ("risk_screening.py", ["screen", symbol]),
52
- "regime": ("market_regime.py", ["detect"]),
53
53
  }
54
- return _run_all(tasks)
54
+ if market in ("A", "HK", "US"):
55
+ tasks["regime"] = ("market_regime.py", ["detect", market])
56
+ result = _run_all(tasks, symbol)
57
+ if market not in ("A", "HK", "US"):
58
+ result["regime"] = {"error": f"Market regime unavailable for {market}"}
59
+ return result
55
60
 
56
61
 
57
62
  def gather_technical(symbol: str, kline_count: int = 120, with_quote: bool = False) -> dict:
58
63
  tasks = {
59
64
  "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
65
  }
62
66
  if with_quote:
63
67
  tasks["quote"] = ("stock_data.py", ["quote", symbol])
64
- return _run_all(tasks)
68
+ return _run_all(tasks, symbol)
65
69
 
66
70
 
67
71
  def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -> dict:
68
72
  args = ["screen", "--market", market, "--top", str(top)]
69
73
  if config:
70
74
  args += ["--config", config]
71
- raw = _run("screener.py", args, timeout=120)
75
+ raw = _run("screener.py", args, timeout=command_timeout("screener.py", args))
72
76
  return {"screen_results": _parse_json(raw)}
73
77
 
74
78
 
75
79
  def gather_fundamental(symbol: str) -> dict:
80
+ market = detect_market(symbol)
76
81
  tasks = {
77
82
  "quote": ("stock_data.py", ["quote", symbol]),
78
83
  "kline": ("stock_data.py", ["kline", symbol, "--period", "daily", "--count", "60"]),
79
- "technical": ("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]),
80
84
  "financials": ("stock_data.py", ["financials", symbol]),
81
85
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
82
86
  "stock_info": ("stock_data.py", ["stock_info", symbol]),
83
- "sector_rankings": ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"]),
84
87
  }
85
- return _run_all(tasks)
88
+ if market == "A":
89
+ tasks["sector_rankings"] = ("stock_data.py", ["sector_rankings", "--top", "5", "--direction", "both"])
90
+ result = _run_all(tasks, symbol)
91
+ if market != "A":
92
+ result["sector_rankings"] = {"error": f"Sector rankings unavailable for {market}"}
93
+ return result
86
94
 
87
95
 
88
96
  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__":
@@ -10,6 +10,7 @@ tmp_path).
10
10
 
11
11
  import argparse
12
12
  import json
13
+ import math
13
14
  import os
14
15
  import sys
15
16
  from concurrent.futures import ThreadPoolExecutor
@@ -17,9 +18,14 @@ from pathlib import Path
17
18
 
18
19
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
19
20
 
21
+ from _subproc import command_timeout, utf8_stdio
20
22
  from _subproc import run_tool as _run_tool
21
- from _subproc import utf8_stdio
22
- from signal_tracker import _load_records
23
+ from signal_tracker import _append_record, _load_records, _store_lock
24
+
25
+
26
+ def _index_change(row):
27
+ value = row.get("change_pct") if isinstance(row, dict) else None
28
+ return value if isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value) else None
23
29
 
24
30
 
25
31
  def calc_temperature(stats: dict, indices: list) -> dict:
@@ -37,7 +43,7 @@ def calc_temperature(stats: dict, indices: list) -> dict:
37
43
  weights.append(0.45)
38
44
 
39
45
  if indices and isinstance(indices, list):
40
- changes = [idx.get("change_pct") for idx in indices if idx.get("change_pct") is not None]
46
+ changes = [value for row in indices if (value := _index_change(row)) is not None]
41
47
  if changes:
42
48
  avg_change = sum(changes) / len(changes)
43
49
  index_score = max(0, min(100, 50 + avg_change * 12))
@@ -54,7 +60,7 @@ def calc_temperature(stats: dict, indices: list) -> dict:
54
60
  weights.append(0.20)
55
61
 
56
62
  if not scores:
57
- return {"score": 50, "level": "neutral", "signal": "yellow"}
63
+ return {"score": None, "level": "unknown", "signal": "unknown"}
58
64
 
59
65
  total_weight = sum(weights)
60
66
  temperature = sum(s * w for s, w in zip(scores, weights)) / total_weight
@@ -69,8 +75,8 @@ def calc_temperature(stats: dict, indices: list) -> dict:
69
75
  return {"score": round(temperature, 1), "level": level, "signal": signal}
70
76
 
71
77
 
72
- def _signal_to_stance(signal: str) -> str:
73
- return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal, "balanced")
78
+ def _signal_to_stance(signal: str) -> str | None:
79
+ return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal)
74
80
 
75
81
 
76
82
  def review_market(market: str = "A") -> dict:
@@ -100,10 +106,7 @@ def review_market(market: str = "A") -> dict:
100
106
  results = {}
101
107
  with ThreadPoolExecutor(max_workers=5) as executor:
102
108
  futures = {
103
- # stats fans out to a full-market snapshot — screener gives the same path 240s
104
- # (screener.fetch_snapshot); the default 30s would silently degrade the
105
- # temperature to the neutral 50 fallback on a weak network.
106
- key: executor.submit(_run_tool, script, args, 240 if key == "stats" else 30)
109
+ key: executor.submit(_run_tool, script, args, command_timeout(script, args))
107
110
  for key, (script, args) in tasks.items()
108
111
  }
109
112
  for key, future in futures.items():
@@ -113,6 +116,22 @@ def review_market(market: str = "A") -> dict:
113
116
  results[key] = None
114
117
 
115
118
  temperature = calc_temperature(results.get("stats"), results.get("indices"))
119
+ warnings = [
120
+ f"{key} data unavailable"
121
+ for key, value in results.items()
122
+ if not value
123
+ or (isinstance(value, dict) and "error" in value)
124
+ or (key == "indices" and isinstance(value, list) and any(_index_change(row) is None for row in value))
125
+ or (key == "sectors" and isinstance(value, dict) and not value.get("top") and not value.get("bottom"))
126
+ or (
127
+ isinstance(value, list)
128
+ and any(
129
+ isinstance(row, dict) and ("error" in row or row.get("note") == "data unavailable") for row in value
130
+ )
131
+ )
132
+ ]
133
+ if temperature["score"] is None:
134
+ warnings.append("Insufficient market data to determine temperature or strategy stance")
116
135
 
117
136
  output = {
118
137
  "market": market,
@@ -124,6 +143,8 @@ def review_market(market: str = "A") -> dict:
124
143
  "regime": results.get("regime"),
125
144
  "temperature": temperature,
126
145
  "strategy_stance": _signal_to_stance(temperature["signal"]),
146
+ "warnings": warnings,
147
+ "data_quality": {"flags": ["partial_data"] if warnings else [], "limitations": warnings},
127
148
  }
128
149
  return output
129
150
 
@@ -145,10 +166,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
145
166
  """Append one review report object to the JSONL archive (side effect of render --save)."""
146
167
  store = store or _default_store()
147
168
  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:
169
+ with _store_lock(store):
170
+ _append_record(store, report)
171
+ except (OSError, ValueError) as e:
152
172
  return {"error": str(e)}
153
173
  return {"saved": True, "date": report.get("review_date") or report.get("date")}
154
174
 
@@ -156,7 +176,9 @@ def save_review(report: dict, store: Path | None = None) -> dict:
156
176
  def cmd_history(args) -> dict:
157
177
  """Recent archived reviews, newest first. Empty store -> clean empty list."""
158
178
  try:
159
- records = _load_records(Path(args.store) if args.store else _default_store())
179
+ store = Path(args.store) if args.store else _default_store()
180
+ with _store_lock(store):
181
+ records = _load_records(store)
160
182
  except (OSError, UnicodeDecodeError) as e:
161
183
  # store is a directory / not UTF-8 — same clean-error contract as save_review
162
184
  return {"error": str(e)}