stock-analysis-plugin 0.2.6 → 0.2.8

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 (36) hide show
  1. package/README.md +88 -15
  2. package/package.json +2 -1
  3. package/pi/index.ts +867 -72
  4. package/schemas/market_review_schema.json +12 -5
  5. package/schemas/report_schema.json +65 -1
  6. package/scripts/cli-timeout.mjs +27 -0
  7. package/scripts/json-input.mjs +26 -0
  8. package/skills/bull-trend/SKILL.md +11 -1
  9. package/skills/bull-trend/references/monthly-trend-strategy.md +47 -0
  10. package/skills/bull-trend/references/trend-horizons.md +43 -0
  11. package/skills/bull-trend/references/trend-temperature.md +57 -0
  12. package/skills/bull-trend/scripts/market_trend.py +78 -0
  13. package/skills/event-driven/SKILL.md +4 -0
  14. package/skills/growth-quality/SKILL.md +4 -0
  15. package/skills/market-review/SKILL.md +2 -0
  16. package/skills/stock-analysis/SKILL.md +17 -3
  17. package/skills/stock-analysis/references/stateless-analysis.md +66 -0
  18. package/skills/stock-screener/SKILL.md +5 -1
  19. package/skills/strategy-backtest/SKILL.md +19 -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 +2 -0
  23. package/templates/stock_analysis/full.md.j2 +2 -0
  24. package/tools/_subproc.py +66 -11
  25. package/tools/backtest.py +143 -19
  26. package/tools/capabilities.py +12 -0
  27. package/tools/cli_budgets.json +27 -0
  28. package/tools/gather.py +4 -6
  29. package/tools/market_review.py +30 -9
  30. package/tools/screener.py +57 -15
  31. package/tools/signal_tracker.py +77 -6
  32. package/tools/stateless_analysis.py +630 -0
  33. package/tools/stock_data.py +90 -15
  34. package/tools/technical.py +186 -0
  35. package/tools/trend_strategy.py +365 -0
  36. package/tools/watchlist_context.py +18 -7
@@ -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,11 +18,16 @@ 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
23
  from signal_tracker import _append_record, _load_records, _store_lock
23
24
 
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
29
+
30
+
25
31
  def calc_temperature(stats: dict, indices: list) -> dict:
26
32
  """Calculate market temperature score from breadth, index change, and limit data."""
27
33
  scores = []
@@ -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
 
package/tools/screener.py CHANGED
@@ -18,7 +18,7 @@ import yaml
18
18
 
19
19
  sys.path.insert(0, str(Path(__file__).resolve().parent))
20
20
 
21
- from _subproc import run_tool, utf8_stdio
21
+ from _subproc import command_timeout, run_tool, utf8_stdio
22
22
  from market_review import calc_temperature
23
23
  from stock_data import compute_market_stats
24
24
 
@@ -35,6 +35,8 @@ DEFAULT_FILTERS = {
35
35
  "volume_ratio_min": 0.5,
36
36
  }
37
37
 
38
+ L1_WEIGHTS = {"value": 0.4, "momentum": 0.3, "liquidity": 0.3}
39
+
38
40
  L2_WEIGHTS = {
39
41
  "value": 0.15,
40
42
  "quality": 0.20,
@@ -48,15 +50,17 @@ L2_WEIGHTS = {
48
50
  _INDEX_REGION = {"A": "cn", "HK": "hk", "US": "us"}
49
51
 
50
52
 
51
- def _fetch_json(args: list[str], timeout: int = 30) -> dict | list | None:
52
- """Run a stock_data.py CLI command and parse its JSON stdout; None on any failure."""
53
- return run_tool("stock_data.py", args, timeout=timeout)
53
+ def _fetch_json(args: list[str], timeout: int | None = None) -> dict | list | None:
54
+ """Fetch JSON with the shared command budget and preserve structured failures."""
55
+ return run_tool("stock_data.py", args, timeout=timeout, preserve_errors=True)
54
56
 
55
57
 
56
58
  def fetch_snapshot(market: str) -> list:
57
59
  # a cold full-market snapshot can exceed 30s (CI runners) — allow longer; with
58
60
  # all eastmoney/sina legs timing out, the tencent fallback leg needs ~2min more
59
- return _fetch_json(["market_snapshot", "--market", market], timeout=240)
61
+ return _fetch_json(
62
+ ["market_snapshot", "--market", market], timeout=command_timeout("stock_data.py", ["market_snapshot"])
63
+ )
60
64
 
61
65
 
62
66
  def load_config(path: str) -> dict:
@@ -106,9 +110,10 @@ def apply_filters(df: pd.DataFrame, filters: dict) -> pd.DataFrame:
106
110
 
107
111
 
108
112
  def _minmax(s: pd.Series) -> pd.Series:
113
+ s = s.where(np.isfinite(s))
109
114
  mn, mx = s.min(), s.max()
110
115
  if mx == mn:
111
- return pd.Series(50.0, index=s.index)
116
+ return pd.Series(50.0, index=s.index).where(s.notna())
112
117
  return ((s - mn) / (mx - mn) * 100).clip(0, 100)
113
118
 
114
119
 
@@ -131,7 +136,7 @@ def compute_scores(df: pd.DataFrame) -> pd.DataFrame:
131
136
  if "dividend_yield" in df.columns:
132
137
  df["dividend_score"] = _minmax(df["dividend_yield"])
133
138
  value_parts.append(df["dividend_score"])
134
- df["value_score"] = pd.concat(value_parts, axis=1).mean(axis=1) if value_parts else 0.0
139
+ df["value_score"] = pd.concat(value_parts, axis=1).mean(axis=1) if value_parts else np.nan
135
140
 
136
141
  # Momentum: higher change_pct and volume_ratio → higher score
137
142
  mom_parts = []
@@ -139,7 +144,7 @@ def compute_scores(df: pd.DataFrame) -> pd.DataFrame:
139
144
  mom_parts.append(_minmax(df["change_pct"]))
140
145
  if "volume_ratio" in df.columns:
141
146
  mom_parts.append(_minmax(df["volume_ratio"]))
142
- df["momentum_score"] = sum(mom_parts) / max(len(mom_parts), 1)
147
+ df["momentum_score"] = pd.concat(mom_parts, axis=1).mean(axis=1) if mom_parts else np.nan
143
148
 
144
149
  # Liquidity: higher turnover_rate and volume → higher score
145
150
  liq_parts = []
@@ -147,9 +152,11 @@ def compute_scores(df: pd.DataFrame) -> pd.DataFrame:
147
152
  liq_parts.append(_minmax(df["turnover_rate"]))
148
153
  if "volume" in df.columns:
149
154
  liq_parts.append(_minmax(df["volume"].astype(float)))
150
- df["liquidity_score"] = sum(liq_parts) / max(len(liq_parts), 1)
155
+ df["liquidity_score"] = pd.concat(liq_parts, axis=1).mean(axis=1) if liq_parts else np.nan
151
156
 
152
- df["composite_score"] = 0.4 * df["value_score"] + 0.3 * df["momentum_score"] + 0.3 * df["liquidity_score"]
157
+ groups = df[[f"{group}_score" for group in L1_WEIGHTS]].rename(columns=lambda name: name.removesuffix("_score"))
158
+ weights = pd.Series(L1_WEIGHTS)
159
+ df["composite_score"] = groups.mul(weights).sum(axis=1, min_count=1) / groups.notna().mul(weights).sum(axis=1)
153
160
  return df
154
161
 
155
162
 
@@ -298,8 +305,9 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
298
305
  scores = c["scores"]
299
306
  # scores are sanitized to None when non-finite; L2 math needs numbers
300
307
  components = {
301
- "value": scores["value"] or 0,
302
- "liquidity": scores["liquidity"] or 0,
308
+ group: scores[group]
309
+ for group in ("value", "liquidity")
310
+ if group in c["factor_coverage"]["available"] and scores[group] is not None
303
311
  }
304
312
  # momentum is L2-only: candidates without kline factors drop the group and renormalize weights.
305
313
  components.update(
@@ -309,11 +317,20 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
309
317
  {g: round(groups[g], 1) for g in ("quality", "growth", "moneyflow", "low_volatility") if g in groups}
310
318
  )
311
319
  total_weight = sum(L2_WEIGHTS[g] for g in components)
312
- composite_l2 = sum(L2_WEIGHTS[g] * components[g] for g in components) / total_weight
313
- scores["final"] = round(composite_l2 * multiplier, 1)
320
+ composite_l2 = sum(L2_WEIGHTS[g] * components[g] for g in components) / total_weight if total_weight else None
321
+ scores["final"] = round(composite_l2 * multiplier, 1) if composite_l2 is not None else None
322
+ c["factor_coverage"] = {
323
+ "available": list(components),
324
+ "missing": [g for g in L2_WEIGHTS if g not in components],
325
+ "ratio": round(total_weight, 4),
326
+ "effective_weights": {g: L2_WEIGHTS[g] / total_weight for g in components},
327
+ "effective_scores": components,
328
+ }
314
329
 
315
330
 
316
331
  def screen(market: str, top: int, config: dict | None, l2: bool = False) -> dict:
332
+ if isinstance(top, bool) or not isinstance(top, int) or top <= 0:
333
+ return {"error": "top must be a positive integer", "market": market}
317
334
  if config is not None and (not isinstance(config, dict) or not isinstance(config.get("filters", {}), dict)):
318
335
  return {"error": "config and filters must be objects", "market": market}
319
336
  filters = merge_filters(config)
@@ -443,11 +460,36 @@ def screen(market: str, top: int, config: dict | None, l2: bool = False) -> dict
443
460
  }
444
461
  if "dividend_score" in row:
445
462
  entry["scores"]["dividend"] = _finite(round(float(row["dividend_score"]), 1))
463
+ factor_inputs = {
464
+ "value": ("pe", "pb", "dividend_yield"),
465
+ "momentum": ("change_pct", "volume_ratio"),
466
+ "liquidity": ("turnover_rate", "volume"),
467
+ }
468
+ weights = L1_WEIGHTS
469
+ available = [
470
+ g
471
+ for g, fields in factor_inputs.items()
472
+ if entry["scores"][g] is not None and any(field in entry for field in fields)
473
+ ]
474
+ for group in weights:
475
+ if group not in available:
476
+ entry["scores"][group] = None
477
+ if not available:
478
+ entry["scores"]["composite"] = entry["scores"]["final"] = None
479
+ total_weight = sum(weights[g] for g in available)
480
+ entry["factor_coverage"] = {
481
+ "available": available,
482
+ "missing": [g for g in weights if g not in available],
483
+ "ratio": round(total_weight, 4),
484
+ "effective_weights": {g: weights[g] / total_weight for g in available},
485
+ }
446
486
  candidates.append(entry)
447
487
 
448
488
  if l2_enabled:
449
489
  _apply_l2(candidates, market, multiplier)
450
- candidates.sort(key=lambda c: c["scores"]["final"], reverse=True)
490
+ candidates.sort(
491
+ key=lambda c: c["scores"]["final"] if c["scores"]["final"] is not None else -float("inf"), reverse=True
492
+ )
451
493
  for rank, entry in enumerate(candidates, 1):
452
494
  entry["rank"] = rank
453
495
 
@@ -16,12 +16,13 @@ import math
16
16
  import os
17
17
  import sys
18
18
  import tempfile
19
+ import time
19
20
  from datetime import datetime
20
21
  from pathlib import Path
21
22
  from uuid import uuid4
22
23
 
23
24
  sys.path.insert(0, str(Path(__file__).resolve().parent))
24
- from _subproc import run_tool, utf8_stdio # noqa: E402
25
+ from _subproc import command_timeout, run_tool, utf8_stdio # noqa: E402
25
26
  from stock_data import detect_market # noqa: E402
26
27
 
27
28
 
@@ -234,7 +235,26 @@ def _valid_bar(bar) -> bool:
234
235
  )
235
236
 
236
237
 
238
+ def _price_factor(rec, bars):
239
+ if all(bar.get("adjusted") == "none" for bar in bars):
240
+ return 1.0
241
+ anchor = next((bar for bar in bars if bar["date"] == rec["record_date"]), {})
242
+ basis = anchor.get("adjusted")
243
+ factor = anchor.get("adjustment_factor")
244
+ if (
245
+ basis in ("qfq", "total_return")
246
+ and all(bar.get("adjusted") == basis for bar in bars)
247
+ and not isinstance(factor, bool)
248
+ and isinstance(factor, (int, float))
249
+ and math.isfinite(factor)
250
+ and factor > 0
251
+ ):
252
+ return factor
253
+ return None
254
+
255
+
237
256
  def cmd_evaluate(args):
257
+ deadline = time.monotonic() + command_timeout("signal_tracker.py", ["evaluate"]) - 30
238
258
  store = _store_path(args)
239
259
  try:
240
260
  with _store_lock(store):
@@ -244,16 +264,22 @@ def cmd_evaluate(args):
244
264
  results = []
245
265
  evaluated = closed = 0
246
266
  changed = False
267
+ deferred = 0
247
268
  for rec in records:
248
269
  if rec.get("status") != "open":
249
270
  continue
250
271
  if args.symbol and rec.get("symbol") != args.symbol:
251
272
  continue
273
+ remaining = deadline - time.monotonic()
274
+ if remaining <= 0:
275
+ deferred += 1
276
+ continue
252
277
  evaluated += 1
253
278
  entry = {"id": rec.get("id"), "symbol": rec.get("symbol")}
254
279
  bars = run_tool(
255
280
  "stock_data.py",
256
281
  ["kline", rec["symbol"], "--period", "daily", "--count", str(_kline_count(rec))],
282
+ timeout=min(command_timeout("stock_data.py", ["kline"]), remaining),
257
283
  )
258
284
  if (
259
285
  not isinstance(bars, list)
@@ -262,12 +288,24 @@ def cmd_evaluate(args):
262
288
  or min(str(b.get("date", ""))[:10] for b in bars) > rec["record_date"]
263
289
  ):
264
290
  entry.update({"outcome": "skipped_no_data", "return_pct": None, "exit_date": None})
291
+ elif (factor := _price_factor(rec, bars)) is None:
292
+ entry.update(
293
+ outcome="skipped_price_basis",
294
+ return_pct=None,
295
+ exit_date=None,
296
+ warning="Adjusted or unknown prices lack a verified conversion to the original signal prices",
297
+ )
265
298
  else:
266
- ev = _evaluate_bars(rec, bars)
299
+ priced = dict(rec)
300
+ for key in ("entry_price", "target_price", "stop_price"):
301
+ if priced.get(key) is not None:
302
+ priced[key] *= factor
303
+ ev = _evaluate_bars(priced, bars)
267
304
  if ev is None:
268
305
  entry.update({"outcome": "open", "return_pct": None, "exit_date": None})
269
306
  else:
270
307
  outcome, exit_price, exit_date = ev
308
+ exit_price /= factor
271
309
  ret = round((exit_price - rec["entry_price"]) / rec["entry_price"] * 100, 2)
272
310
  if rec.get("direction") == "sell":
273
311
  ret = -ret
@@ -278,6 +316,7 @@ def cmd_evaluate(args):
278
316
  "exit_price": exit_price,
279
317
  "exit_date": exit_date,
280
318
  "return_pct": ret,
319
+ "evaluation_price_basis": {"adjusted": bars[0].get("adjusted"), "factor": factor},
281
320
  }
282
321
  )
283
322
  changed = True
@@ -289,7 +328,18 @@ def cmd_evaluate(args):
289
328
  with _store_lock(store):
290
329
  current = _load_records(store, strict=True)
291
330
  merged = [updates.get(r["id"], r) if r.get("status") == "open" else r for r in current]
292
- if changed:
331
+ if deferred:
332
+ attempted = {entry["id"] for entry in results}
333
+ positions = [
334
+ i
335
+ for i, rec in enumerate(merged)
336
+ if rec.get("status") == "open" and (not args.symbol or rec.get("symbol") == args.symbol)
337
+ ]
338
+ # Keep closed/unselected records in place; give unattempted signals the next turn.
339
+ queued = sorted((merged[i] for i in positions), key=lambda rec: rec["id"] in attempted)
340
+ for i, rec in zip(positions, queued):
341
+ merged[i] = rec
342
+ if changed or deferred:
293
343
  _save_records(store, merged)
294
344
  persisted = {r["id"]: r for r in merged}
295
345
  for entry in results:
@@ -299,7 +349,15 @@ def cmd_evaluate(args):
299
349
  closed += 1
300
350
  except (OSError, ValueError) as e:
301
351
  return {"error": str(e)}
302
- return {"evaluated": evaluated, "closed": closed, "still_open": evaluated - closed, "results": results}
352
+ return {
353
+ "evaluated": evaluated,
354
+ "closed": closed,
355
+ "still_open": evaluated + deferred - closed,
356
+ "results": results,
357
+ "deferred": deferred,
358
+ "complete": deferred == 0,
359
+ "warnings": ["Time budget exhausted; deferred signals remain open for the next evaluation"] if deferred else [],
360
+ }
303
361
 
304
362
 
305
363
  def cmd_summary(args):
@@ -324,14 +382,27 @@ def cmd_summary(args):
324
382
  for o in ("target_hit", "stop_hit", "timeout_profit", "timeout_loss")
325
383
  }
326
384
  wins = counts["target_hit"] + counts["timeout_profit"]
327
- returns = [r["return_pct"] for r in closed_recs if isinstance(r.get("return_pct"), (int, float))]
385
+ returns = [
386
+ r["return_pct"]
387
+ for r in closed_recs
388
+ if isinstance(r.get("return_pct"), (int, float))
389
+ and not isinstance(r["return_pct"], bool)
390
+ and math.isfinite(r["return_pct"])
391
+ ]
328
392
  by_source = {}
329
393
  for r in closed_recs:
330
- agg = by_source.setdefault(r.get("source") or "unknown", {"closed": 0, "wins": 0})
394
+ agg = by_source.setdefault(r.get("source") or "unknown", {"closed": 0, "wins": 0, "returns": []})
331
395
  agg["closed"] += 1
396
+ value = r.get("return_pct")
397
+ if isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value):
398
+ agg["returns"].append(value)
332
399
  if r.get("outcome") in ("target_hit", "timeout_profit"):
333
400
  agg["wins"] += 1
334
401
  for agg in by_source.values():
402
+ samples = agg.pop("returns")
403
+ agg["return_samples"] = len(samples)
404
+ agg["avg_return_pct"] = round(sum(samples) / len(samples), 2) if samples else None
405
+ agg["sample_warning"] = "Small observational sample; not strategy validation" if len(samples) < 15 else None
335
406
  agg["win_rate"] = round(agg["wins"] / agg["closed"] * 100, 2) if agg["closed"] else None
336
407
  return {
337
408
  "filters": {"source": args.source, "symbol": args.symbol, "status": args.status},