@weaxs/openclaw-stock-analysis 0.2.8 → 0.2.10

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 (46) hide show
  1. package/README.md +3 -1
  2. package/dist/index.js +154 -20
  3. package/index.ts +126 -6
  4. package/openclaw.plugin.json +4 -1
  5. package/package.json +4 -3
  6. package/scripts/profile-offline.py +127 -0
  7. package/scripts/profile-workflows.py +87 -0
  8. package/scripts/setup-python.mjs +11 -4
  9. package/scripts/venv-python.mjs +7 -0
  10. package/skills/event-driven/SKILL.md +2 -0
  11. package/skills/growth-quality/SKILL.md +2 -0
  12. package/skills/market-review/SKILL.md +1 -1
  13. package/skills/sector-rotation/SKILL.md +57 -0
  14. package/skills/sector-rotation/references/computation.md +83 -0
  15. package/skills/sector-rotation/references/strategy_rules.md +62 -0
  16. package/skills/sector-rotation/scripts/rotation_metrics.py +11 -0
  17. package/skills/stock-analysis/SKILL.md +15 -4
  18. package/skills/stock-analysis/references/host-workflows.md +45 -0
  19. package/skills/stock-analysis/references/stateless-analysis.md +16 -2
  20. package/skills/stock-screener/SKILL.md +6 -0
  21. package/skills/strategy-backtest/SKILL.md +10 -3
  22. package/tools/_cache.py +93 -21
  23. package/tools/_subproc.py +43 -8
  24. package/tools/anomaly_detect.py +11 -6
  25. package/tools/backtest.py +225 -70
  26. package/tools/capabilities.py +3 -0
  27. package/tools/cli_budgets.json +2 -1
  28. package/tools/gather.py +63 -8
  29. package/tools/market_regime.py +26 -15
  30. package/tools/market_review.py +2 -2
  31. package/tools/position_context.py +5 -2
  32. package/tools/report_renderer.py +4 -0
  33. package/tools/requirements.txt +2 -2
  34. package/tools/risk_screening.py +40 -15
  35. package/tools/screener.py +44 -18
  36. package/tools/search_intel.py +94 -32
  37. package/tools/sec_holders.py +27 -25
  38. package/tools/sector_rotation.py +466 -0
  39. package/tools/signal_tracker.py +100 -49
  40. package/tools/stateless_analysis.py +600 -33
  41. package/tools/stock_data.py +111 -26
  42. package/tools/technical.py +42 -40
  43. package/tools/trading_calendar.py +26 -21
  44. package/tools/value_data.py +6 -2
  45. package/tools/watchlist.py +24 -15
  46. package/tools/watchlist_context.py +63 -49
@@ -54,7 +54,8 @@ def _get_index_kline(market: str) -> list:
54
54
  def _compute_indicators(klines: list) -> dict:
55
55
  import numpy as np
56
56
 
57
- df = pd.DataFrame(klines).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
57
+ raw = pd.DataFrame(klines)
58
+ df = raw.reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
58
59
  df = df[np.isfinite(df).all(axis=1) & (df > 0).all(axis=1)]
59
60
  closes = df["close"].to_numpy()
60
61
  if len(closes) < 20:
@@ -64,7 +65,7 @@ def _compute_indicators(klines: list) -> dict:
64
65
  ma5 = float(ma_series(close_s, 5).iloc[-1])
65
66
  ma10 = float(ma_series(close_s, 10).iloc[-1])
66
67
  ma20 = float(ma_series(close_s, 20).iloc[-1])
67
- ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else float(np.mean(closes))
68
+ ma60 = float(ma_series(close_s, 60).iloc[-1]) if len(closes) >= 60 else None
68
69
  close = float(closes[-1])
69
70
 
70
71
  highs = df["high"].to_numpy()
@@ -79,14 +80,7 @@ def _compute_indicators(klines: list) -> dict:
79
80
 
80
81
  upper, mid, lower = bollinger_series(close_s)
81
82
  boll_width = float((upper.iloc[-1] - lower.iloc[-1]) / mid.iloc[-1]) if mid.iloc[-1] > 0 else 0
82
-
83
- # rsi_series leaves NaN when the window has zero losses: 14 straight up bars
84
- # is RSI 100, but 14 flat bars (no gains either) is undefined — call it neutral.
85
- rsi_val = rsi_series(close_s, 14).iloc[-1]
86
- if pd.isna(rsi_val):
87
- rsi14 = 100.0 if float(close_s.diff().clip(lower=0).iloc[-14:].sum()) > 0 else 50.0
88
- else:
89
- rsi14 = float(rsi_val)
83
+ rsi14 = float(rsi_series(close_s, 14).iloc[-1])
90
84
 
91
85
  # Real EMA12-EMA26 (the old code mislabeled an np.mean simple average as EMA).
92
86
  dif, _, _ = macd_series(close_s)
@@ -96,12 +90,15 @@ def _compute_indicators(klines: list) -> dict:
96
90
 
97
91
  prev_close = float(closes[-2]) if len(closes) >= 2 else close
98
92
  change_pct = (close - prev_close) / prev_close * 100 if prev_close > 0 else 0
93
+ as_of = pd.to_datetime(raw.iloc[df.index[-1]].get("date"), errors="coerce")
99
94
 
100
95
  return {
96
+ "as_of": as_of.strftime("%Y-%m-%d") if not pd.isna(as_of) else None,
97
+ "dropped_rows": len(klines) - len(df),
101
98
  "ma5": round(ma5, 2),
102
99
  "ma10": round(ma10, 2),
103
100
  "ma20": round(ma20, 2),
104
- "ma60": round(ma60, 2),
101
+ "ma60": round(ma60, 2) if ma60 is not None else None,
105
102
  "close": round(close, 2),
106
103
  "change_pct": round(change_pct, 2),
107
104
  "atr_ratio": round(atr_ratio, 4),
@@ -124,14 +121,14 @@ def classify_regime(ind: dict) -> tuple:
124
121
 
125
122
  bullish_ma = ma5 > ma10 > ma20
126
123
  bearish_ma = ma5 < ma10 < ma20
127
- above_ma60 = close > ma60
124
+ above_ma60 = close > ma60 if ma60 is not None else None
128
125
  high_volatility = atr_ratio > 0.025 or boll_width > 0.15
129
126
  strong_trend = ma_spread > 0.03
130
127
 
131
128
  if bullish_ma and above_ma60 and strong_trend:
132
129
  regime = "trending_up"
133
130
  confidence = 0.85 + (0.1 if ma_spread > 0.05 else 0)
134
- elif bearish_ma and not above_ma60 and strong_trend:
131
+ elif bearish_ma and above_ma60 is not None and not above_ma60 and strong_trend:
135
132
  regime = "trending_down"
136
133
  confidence = 0.85 + (0.1 if ma_spread > 0.05 else 0)
137
134
  elif high_volatility and not strong_trend:
@@ -176,6 +173,13 @@ def detect_regime(market: str = "A") -> dict:
176
173
  if not klines:
177
174
  return {"error": f"Failed to fetch index data for {name}"}
178
175
 
176
+ import numpy as np
177
+
178
+ if pd.isna(pd.to_datetime(klines[-1].get("date"), errors="coerce")):
179
+ return {"error": "Latest index bar has invalid date"}
180
+ latest = pd.DataFrame([klines[-1]]).reindex(columns=["close", "high", "low"]).apply(pd.to_numeric, errors="coerce")
181
+ if not np.isfinite(latest).all().all() or not (latest > 0).all().all():
182
+ return {"error": "Latest index bar has invalid OHLC data"}
179
183
  ind = _compute_indicators(klines)
180
184
  if not ind:
181
185
  return {"error": "Insufficient data to compute indicators"}
@@ -194,13 +198,19 @@ def detect_regime(market: str = "A") -> dict:
194
198
 
195
199
  return {
196
200
  "market": market,
197
- "index": {"code": code, "name": name, "close": ind["close"], "change_pct": ind["change_pct"]},
201
+ "index": {
202
+ "code": code,
203
+ "name": name,
204
+ "close": ind["close"],
205
+ "change_pct": ind["change_pct"],
206
+ "as_of": ind["as_of"],
207
+ },
198
208
  "regime": regime,
199
209
  "regime_cn": REGIME_CN.get(regime, regime),
200
210
  "confidence": round(confidence, 2),
201
211
  "indicators": {
202
212
  "ma_arrangement": ma_arrangement,
203
- "above_ma60": ind["close"] > ind["ma60"],
213
+ "above_ma60": ind["close"] > ind["ma60"] if ind["ma60"] is not None else None,
204
214
  "ma_spread_pct": ind["ma_spread_pct"],
205
215
  "atr_ratio": ind["atr_ratio"],
206
216
  "boll_width": ind["boll_width"],
@@ -210,6 +220,7 @@ def detect_regime(market: str = "A") -> dict:
210
220
  "sector_heat": sector_heat,
211
221
  "recommended_skills": recommended,
212
222
  "avoid_skills": avoid,
223
+ "warnings": [f"Dropped {ind['dropped_rows']} invalid OHLC rows"] if ind["dropped_rows"] else [],
213
224
  }
214
225
 
215
226
 
@@ -79,7 +79,7 @@ def _signal_to_stance(signal: str) -> str | None:
79
79
  return {"green": "offensive", "yellow": "balanced", "red": "defensive"}.get(signal)
80
80
 
81
81
 
82
- def review_market(market: str = "A") -> dict:
82
+ def review_market(market: str = "A", *, run=None) -> dict:
83
83
  """Gather market data and compute review for a single market."""
84
84
  from datetime import datetime
85
85
 
@@ -106,7 +106,7 @@ def review_market(market: str = "A") -> dict:
106
106
  results = {}
107
107
  with ThreadPoolExecutor(max_workers=5) as executor:
108
108
  futures = {
109
- key: executor.submit(_run_tool, script, args, command_timeout(script, args))
109
+ key: executor.submit(run or _run_tool, script, args, command_timeout(script, args))
110
110
  for key, (script, args) in tasks.items()
111
111
  }
112
112
  for key, future in futures.items():
@@ -8,6 +8,7 @@ import argparse
8
8
  import json
9
9
  import math
10
10
  import sys
11
+ from concurrent.futures import ThreadPoolExecutor
11
12
  from datetime import datetime, timezone
12
13
  from pathlib import Path
13
14
 
@@ -57,8 +58,10 @@ def analyze_position(
57
58
  ):
58
59
  if value is not None and (not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0):
59
60
  return {"symbol": symbol, "error": f"{field} must be finite and positive"}
60
- quote = _run_json("stock_data.py", ["quote", symbol]) or {}
61
- tech = _run_json("technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"]) or {}
61
+ with ThreadPoolExecutor(max_workers=2) as pool:
62
+ quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
63
+ tech_job = pool.submit(_run_json, "technical.py", ["analyze", symbol, "--period", "daily", "--count", "60"])
64
+ quote, tech = quote_job.result() or {}, tech_job.result() or {}
62
65
 
63
66
  price = quote.get("price") if isinstance(quote, dict) and "error" not in quote else None
64
67
  if price is None:
@@ -95,11 +95,13 @@ def main():
95
95
  p.add_argument("--template", default="full", choices=["brief", "full"])
96
96
  p.add_argument("--input", "-i", default="-", help="JSON file path or '-' for stdin")
97
97
  p.add_argument("--input-b64", help="Base64-encoded JSON (avoids shell escaping)")
98
+ p.add_argument("--final", action="store_true", help="Mark a completed report for final reply delivery by the host")
98
99
 
99
100
  p2 = sub.add_parser("market", help="Render a market review report")
100
101
  p2.add_argument("--input", "-i", default="-", help="JSON file path or '-' for stdin")
101
102
  p2.add_argument("--input-b64", help="Base64-encoded JSON (avoids shell escaping)")
102
103
  p2.add_argument("--save", action="store_true", help="Append the report object to the review JSONL archive")
104
+ p2.add_argument("--final", action="store_true", help="Mark a completed report for final reply delivery by the host")
103
105
 
104
106
  args = parser.parse_args()
105
107
  if not args.command:
@@ -110,6 +112,8 @@ def main():
110
112
  result = render(args.command, getattr(args, "template", "full"), report)
111
113
  if args.command == "market" and args.save and "error" not in result:
112
114
  result["archived"] = save_review(report)
115
+ if args.final and "error" not in result and "error" not in result.get("archived", {}):
116
+ result["final"] = True
113
117
  json.dump(result, sys.stdout, ensure_ascii=False, indent=2, default=str)
114
118
  print()
115
119
  if "error" in result:
@@ -5,7 +5,7 @@ pandas>=2.0.0
5
5
  numpy>=1.24.0
6
6
  pyyaml>=6.0
7
7
  pypinyin>=0.50.0
8
- requests>=2.31.0
8
+ requests>=2.33.0
9
9
  exchange-calendars>=4.5.0
10
10
  newspaper4k>=0.9.0
11
11
  efinance>=0.5.0
@@ -13,4 +13,4 @@ baostock>=0.8.8
13
13
  pytdx>=1.72
14
14
  longport>=1.0.0
15
15
  finnhub-python>=2.4.0
16
- jinja2>=3.1.0
16
+ jinja2>=3.1.6
@@ -6,7 +6,7 @@ import json
6
6
  import math
7
7
  import os
8
8
  import sys
9
- from concurrent.futures import ThreadPoolExecutor
9
+ from concurrent.futures import Future, ThreadPoolExecutor
10
10
  from datetime import datetime, timedelta
11
11
 
12
12
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
@@ -28,8 +28,12 @@ def _safe_float(val) -> float | None:
28
28
  # --------------- Check 1: Valuation ---------------
29
29
 
30
30
 
31
- def check_valuation(symbol: str) -> dict:
32
- quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
31
+ _UNSET = object()
32
+
33
+
34
+ def check_valuation(symbol: str, *, quote=_UNSET) -> dict:
35
+ if quote is _UNSET:
36
+ quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
33
37
  if not isinstance(quote, dict) or "error" in quote:
34
38
  quote = {}
35
39
  pe = _safe_float(quote.get("pe"))
@@ -93,8 +97,9 @@ def check_valuation(symbol: str) -> dict:
93
97
  # --------------- Check 2: Technical Warning ---------------
94
98
 
95
99
 
96
- def check_technical(symbol: str) -> dict:
97
- ta = _run_tool("technical.py", ["analyze", symbol])
100
+ def check_technical(symbol: str, *, ta=_UNSET) -> dict:
101
+ if ta is _UNSET:
102
+ ta = _run_tool("technical.py", ["analyze", symbol])
98
103
  result = {"flags": [], "available": False}
99
104
 
100
105
  if not isinstance(ta, dict) or not ta or "error" in ta:
@@ -149,11 +154,17 @@ def check_technical(symbol: str) -> dict:
149
154
  # --------------- Check 3: Lock-up Expiry ---------------
150
155
 
151
156
 
152
- def check_lockup(symbol: str) -> dict:
153
- result = _run_tool("risk_screening.py", ["lockup", symbol], timeout=25)
157
+ def check_lockup(symbol: str, *, run=None) -> dict:
158
+ result = (run or _run_tool)("risk_screening.py", ["lockup", symbol], timeout=25)
154
159
  return (
155
160
  result
156
- if isinstance(result, dict) and "error" not in result
161
+ if (
162
+ isinstance(result, dict)
163
+ and "error" not in result
164
+ and isinstance(result.get("upcoming_30d"), list)
165
+ and isinstance(result.get("flags"), list)
166
+ and isinstance(result.get("available"), bool)
167
+ )
157
168
  else {"upcoming_30d": [], "flags": [], "available": False}
158
169
  )
159
170
 
@@ -204,7 +215,7 @@ def _fetch_lockup(symbol: str) -> dict:
204
215
  # --------------- Check 4-7: News-based risks ---------------
205
216
 
206
217
 
207
- def check_news_risks(symbol: str, name: str = "") -> dict:
218
+ def check_news_risks(symbol: str, name: str = "", *, run=None) -> dict:
208
219
  search_name = name if name else symbol
209
220
  categories = {
210
221
  "insider": f"{search_name} 减持 股东减持",
@@ -218,7 +229,7 @@ def check_news_risks(symbol: str, name: str = "") -> dict:
218
229
  # declaration order keeps result keys and flags stably ordered
219
230
  with ThreadPoolExecutor(max_workers=len(categories)) as pool:
220
231
  futures = {
221
- cat: pool.submit(_run_tool, "search_intel.py", ["search", query, "--count", "3"])
232
+ cat: pool.submit(run or _run_tool, "search_intel.py", ["search", query, "--count", "3"])
222
233
  for cat, query in categories.items()
223
234
  }
224
235
  for cat, future in futures.items():
@@ -279,15 +290,31 @@ def compute_risk(flags: list) -> dict:
279
290
  # --------------- Main entry ---------------
280
291
 
281
292
 
282
- def screen_risk(symbol: str, name: str = "") -> dict:
293
+ def screen_risk(symbol: str, name: str = "", *, inputs=None, run=None) -> dict:
294
+ inputs = inputs or {}
295
+
296
+ def supplied(key):
297
+ value = inputs[key]
298
+ return value.result() if isinstance(value, Future) else value
299
+
300
+ with ThreadPoolExecutor(max_workers=4) as pool:
301
+ valuation = pool.submit(
302
+ lambda: check_valuation(symbol, quote=supplied("quote")) if "quote" in inputs else check_valuation(symbol)
303
+ )
304
+ technical = pool.submit(
305
+ lambda: (
306
+ check_technical(symbol, ta=supplied("technical")) if "technical" in inputs else check_technical(symbol)
307
+ )
308
+ )
309
+ lockups = pool.submit(check_lockup, symbol, **({"run": run} if run else {}))
310
+ searches = pool.submit(check_news_risks, symbol, name, **({"run": run} if run else {}))
311
+ val, tech, lockup, news = (future.result() for future in (valuation, technical, lockups, searches))
283
312
  all_flags = []
284
313
  checks = {}
285
314
 
286
- val = check_valuation(symbol)
287
315
  checks["valuation"] = {k: val[k] for k in ("pe", "pb", "status", "available")}
288
316
  all_flags.extend(val["flags"])
289
317
 
290
- tech = check_technical(symbol)
291
318
  checks["technical"] = {
292
319
  "buy_signal": tech.get("buy_signal"),
293
320
  "signal_score": tech.get("signal_score"),
@@ -296,11 +323,9 @@ def screen_risk(symbol: str, name: str = "") -> dict:
296
323
  }
297
324
  all_flags.extend(tech["flags"])
298
325
 
299
- lockup = check_lockup(symbol)
300
326
  checks["lockup"] = {"upcoming_30d": lockup["upcoming_30d"], "available": lockup["available"]}
301
327
  all_flags.extend(lockup["flags"])
302
328
 
303
- news = check_news_risks(symbol, name)
304
329
  checks["news_risks"] = {k: v for k, v in news.items() if k != "flags"}
305
330
  all_flags.extend(news["flags"])
306
331
 
package/tools/screener.py CHANGED
@@ -10,6 +10,7 @@ import argparse
10
10
  import json
11
11
  import sys
12
12
  from concurrent.futures import ThreadPoolExecutor
13
+ from contextlib import nullcontext
13
14
  from pathlib import Path
14
15
 
15
16
  import numpy as np
@@ -127,12 +128,13 @@ def _finite(v):
127
128
 
128
129
 
129
130
  def compute_scores(df: pd.DataFrame) -> pd.DataFrame:
130
- # Value: lower PE/PB → higher score (inverted)
131
+ # Value: lower positive PE/PB → higher score; finite non-positive values get the worst score.
131
132
  value_parts = []
132
- if "pe" in df.columns:
133
- value_parts.append(100 - _minmax(df["pe"]))
134
- if "pb" in df.columns:
135
- value_parts.append(100 - _minmax(df["pb"]))
133
+ for name in ("pe", "pb"):
134
+ if name in df.columns:
135
+ series = df[name]
136
+ score = 100 - _minmax(series.where(series > 0))
137
+ value_parts.append(score.mask((series <= 0) & np.isfinite(series), 0))
136
138
  if "dividend_yield" in df.columns:
137
139
  df["dividend_score"] = _minmax(df["dividend_yield"])
138
140
  value_parts.append(df["dividend_score"])
@@ -194,11 +196,27 @@ def compute_sentiment(market: str, snapshot: list | None = None) -> dict:
194
196
  return fallback
195
197
 
196
198
 
197
- def _enrich_one(symbol: str, market: str) -> dict:
199
+ def _enrich_one(symbol: str, market: str, *, pool=None) -> dict:
198
200
  """Fetch raw L2 indicators for one candidate; missing factor sources just yield missing keys."""
201
+ jobs = {
202
+ "kline": ["kline", symbol, "--period", "daily", "--count", "250"],
203
+ "fundamental": ["fundamental_context" if market == "A" else "financials", symbol],
204
+ }
205
+ if market == "A":
206
+ jobs["flow"] = ["capital_flow", symbol, "--mode", "summary"]
207
+ collected = {}
208
+ with ThreadPoolExecutor(max_workers=3) if pool is None else nullcontext(pool) as pool:
209
+ futures = {key: pool.submit(_fetch_json, args) for key, args in jobs.items()}
210
+ for key, future in futures.items():
211
+ try:
212
+ collected[key] = future.result()
213
+ except Exception:
214
+ collected[key] = None
199
215
  result: dict = {}
200
216
  try:
201
- kline = _fetch_json(["kline", symbol, "--period", "daily", "--count", "250"])
217
+ kline = collected["kline"]
218
+ if isinstance(kline, list):
219
+ result["vol20_samples"] = 0
202
220
  if isinstance(kline, list) and len(kline) >= 2:
203
221
  closes = pd.Series([float(k["close"]) for k in kline if k.get("close") is not None])
204
222
  highs = [float(k["high"]) for k in kline if k.get("high") is not None]
@@ -210,17 +228,19 @@ def _enrich_one(symbol: str, market: str) -> dict:
210
228
  peak = max(highs)
211
229
  if peak > 0:
212
230
  result["high_250_proximity"] = round(float(closes.iloc[-1]) / peak, 4)
213
- rets = closes.pct_change().dropna().tail(20)
214
- if len(rets) >= 2:
231
+ valid_closes = closes[np.isfinite(closes) & (closes > 0)]
232
+ rets = valid_closes.pct_change().replace([np.inf, -np.inf], np.nan).dropna().tail(20)
233
+ result["vol20_samples"] = len(rets)
234
+ if len(rets) == 20:
215
235
  result["vol20"] = round(float(rets.std()) * float(np.sqrt(252)), 4)
216
236
  except Exception:
217
237
  pass
218
238
 
219
239
  if market == "A":
220
- fund = _fetch_json(["fundamental_context", symbol])
240
+ fund = collected["fundamental"]
221
241
  if isinstance(fund, dict) and "error" not in fund:
222
242
  prof = fund.get("profitability") or {}
223
- for key in ("roe", "gross_margin", "net_margin"):
243
+ for key in ("roe", "gross_margin", "net_margin", "report_date"):
224
244
  if prof.get(key) is not None:
225
245
  result[key] = prof[key]
226
246
  growth = fund.get("growth") or {}
@@ -228,14 +248,14 @@ def _enrich_one(symbol: str, market: str) -> dict:
228
248
  if growth.get(key) is not None:
229
249
  result[key] = growth[key]
230
250
  else:
231
- fin = _fetch_json(["financials", symbol])
251
+ fin = collected["fundamental"]
232
252
  if isinstance(fin, dict) and "error" not in fin:
233
- for key in ("roe", "profit_margin"):
253
+ for key in ("roe", "profit_margin", "report_date"):
234
254
  if fin.get(key) is not None:
235
255
  result[key] = fin[key]
236
256
 
237
257
  if market == "A":
238
- flow = _fetch_json(["capital_flow", symbol, "--mode", "summary"])
258
+ flow = collected["flow"]
239
259
  if isinstance(flow, dict) and "error" not in flow and flow.get("main_net_5d") is not None:
240
260
  result["main_net_5d"] = flow["main_net_5d"]
241
261
  return result
@@ -264,7 +284,11 @@ def _cross_sectional_scores(enrichments: dict) -> dict:
264
284
  margin = profit_margin
265
285
  elif profit_margin is not None:
266
286
  margin = margin.combine_first(profit_margin)
267
- add_group("quality", [_minmax(s) for s in (col("roe"), margin, col("net_margin")) if s is not None])
287
+ report_dates = fdf.get("report_date", pd.Series("unknown", index=fdf.index)).fillna("unknown")
288
+ add_group(
289
+ "quality",
290
+ [s.groupby(report_dates).transform(_minmax) for s in (col("roe"), margin, col("net_margin")) if s is not None],
291
+ )
268
292
  add_group(
269
293
  "growth", [_minmax(s.clip(-50, 100)) for s in (col("revenue_yoy"), col("net_income_yoy")) if s is not None]
270
294
  )
@@ -288,8 +312,8 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
288
312
  """Enrich candidates with L2 factors and rewrite each scores['final'] as composite_l2 × multiplier."""
289
313
  symbols = [c.get("symbol") for c in candidates]
290
314
  enrichments: dict = {}
291
- with ThreadPoolExecutor(max_workers=6) as executor:
292
- futures = {executor.submit(_enrich_one, sym, market): sym for sym in symbols if sym}
315
+ with ThreadPoolExecutor(max_workers=6) as collection, ThreadPoolExecutor(max_workers=6) as executor:
316
+ futures = {executor.submit(_enrich_one, sym, market, pool=collection): sym for sym in symbols if sym}
293
317
  for future, sym in futures.items():
294
318
  try:
295
319
  enrichments[sym] = future.result() or {}
@@ -301,7 +325,7 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
301
325
  for c in candidates:
302
326
  sym = c.get("symbol")
303
327
  groups = factor_scores.get(sym, {})
304
- c["enriched"] = bool(enrichments.get(sym))
328
+ c["enriched"] = bool(groups)
305
329
  scores = c["scores"]
306
330
  # scores are sanitized to None when non-finite; L2 math needs numbers
307
331
  components = {
@@ -325,6 +349,7 @@ def _apply_l2(candidates: list, market: str, multiplier: float) -> None:
325
349
  "ratio": round(total_weight, 4),
326
350
  "effective_weights": {g: L2_WEIGHTS[g] / total_weight for g in components},
327
351
  "effective_scores": components,
352
+ "vol20_samples": enrichments.get(sym, {}).get("vol20_samples"),
328
353
  }
329
354
 
330
355
 
@@ -442,6 +467,7 @@ def screen(market: str, top: int, config: dict | None, l2: bool = False) -> dict
442
467
  "volume",
443
468
  "turnover_rate",
444
469
  "pe",
470
+ "pe_basis",
445
471
  "pb",
446
472
  "market_cap",
447
473
  "volume_ratio",