stock-analysis-plugin 0.2.6 → 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.
package/tools/backtest.py CHANGED
@@ -70,9 +70,9 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
70
70
  d1 = datetime.strptime(end, "%Y-%m-%d")
71
71
  count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
72
72
 
73
- # run_tool gives venv-python + a bounded timeout + None on any failure
74
- # (non-zero exit, empty/invalid stdout); callers turn that into clean JSON.
75
- 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
+ )
76
76
  if data is None:
77
77
  raise RuntimeError(f"Failed to fetch kline for {symbol}")
78
78
  if isinstance(data, dict) and "error" in data:
@@ -191,7 +191,7 @@ def get_indicator(
191
191
 
192
192
 
193
193
  def check_condition(series: pd.Series, operator: str, value: float, idx: int) -> bool:
194
- if idx < 1 or idx >= len(series):
194
+ if idx < 0 or idx >= len(series):
195
195
  return False
196
196
  cur = float(series.iloc[idx])
197
197
  ops = {
@@ -203,6 +203,8 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
203
203
  }
204
204
  if operator in ops:
205
205
  return ops[operator]
206
+ if idx == 0:
207
+ return False
206
208
  prev = float(series.iloc[idx - 1])
207
209
  if operator == "cross_above":
208
210
  return prev <= value and cur > value
@@ -256,9 +258,17 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
256
258
  raise ValueError("capital must be finite and positive")
257
259
  if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
258
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")
259
264
  market = detect_market(symbol)
260
265
  lot_size = 100 if market == "A" else 1
261
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")
262
272
  slippage = float(sim_cfg.get("slippage", 0.001))
263
273
  commission = float(sim_cfg.get("commission", 0.0015))
264
274
  tax_raw = sim_cfg.get("stamp_tax") # sell-side only (e.g. A股印花税); absent/empty = 0 (disabled)
@@ -293,19 +303,43 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
293
303
  trade_id = 0
294
304
  commission_paid = 0.0
295
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
+ )
296
320
 
297
321
  for i in range(1, len(df)):
298
322
  row = df.iloc[i]
299
323
  date_str = str(row["date"].date()) if hasattr(row["date"], "date") else str(row["date"])
300
- 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())
301
328
 
302
329
  if holding is None:
303
- hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, i, cache)
304
- if hit:
305
- 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)
306
339
  buy_amount = cash * size_frac
307
340
  shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
308
341
  if shares <= 0:
342
+ equity_curve.append({"date": date_str, "equity": round(cash, 2)})
309
343
  continue
310
344
  cost = shares * buy_price * (1 + commission)
311
345
  commission_paid += shares * buy_price * commission
@@ -317,13 +351,14 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
317
351
  "buy_price": round(buy_price, 4),
318
352
  "shares": shares,
319
353
  "cost": cost,
320
- "peak_close": price,
354
+ "peak_close": float(row["close"]),
321
355
  }
322
356
  trades.append(
323
357
  {
324
358
  "id": trade_id,
325
359
  "type": "buy",
326
360
  "date": date_str,
361
+ "signal_date": signal_date,
327
362
  "price": round(buy_price, 2),
328
363
  "shares": shares,
329
364
  "amount": round(cost, 2),
@@ -348,10 +383,16 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
348
383
  elif pnl_pct >= take_profit:
349
384
  sell, reason = True, f"止盈触发 ({round(pnl_pct * 100, 1)}% >= {round(take_profit * 100, 1)}%)"
350
385
  elif exit_conditions:
351
- sell, reason = evaluate_conditions(df, exit_conditions, exit_logic, i, cache)
352
-
353
- if sell:
354
- 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)
355
396
  gross = holding["shares"] * sell_price
356
397
  proceeds = gross * (1 - commission - stamp_tax)
357
398
  commission_paid += gross * commission
@@ -371,6 +412,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
371
412
  "id": trade_id_sell,
372
413
  "type": "sell",
373
414
  "date": date_str,
415
+ "signal_date": signal_date,
374
416
  "price": round(sell_price, 2),
375
417
  "shares": holding["shares"],
376
418
  "amount": round(proceeds, 2),
@@ -384,7 +426,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
384
426
 
385
427
  equity = cash
386
428
  if holding:
387
- equity += holding["shares"] * price
429
+ equity += holding["shares"] * float(row["close"])
388
430
  equity_curve.append({"date": date_str, "equity": round(equity, 2)})
389
431
 
390
432
  if holding:
@@ -396,6 +438,22 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
396
438
  "trades": trades,
397
439
  "equity_curve": equity_curve,
398
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
+ },
399
457
  "costs": {
400
458
  "slippage": slippage,
401
459
  "commission": commission,
@@ -680,6 +738,7 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
680
738
  "initial_capital": capital,
681
739
  "final_capital": final,
682
740
  "costs": sim["costs"],
741
+ "execution": sim["execution"],
683
742
  "metrics": metrics,
684
743
  "trades": sim["trades"],
685
744
  "equity_curve": sample_curve(sim["equity_curve"]),
@@ -18,6 +18,14 @@ 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},
21
29
  "get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
22
30
  "get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
23
31
  "get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
@@ -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,15 +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
13
  from technical import analyze_records # noqa: E402
14
14
 
15
15
 
16
- # gather fans out the heavier CLIs, so the default timeout stays 60s; raw stdout
17
- # wanted here (parsing is _parse_json's job); def keeps timeout kw-passable.
18
- def _run(script, args, timeout=60):
19
- 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)
20
18
 
21
19
 
22
20
  def _news_query(symbol: str) -> str:
@@ -74,7 +72,7 @@ def gather_screen(market: str = "A", top: int = 20, config: str | None = None) -
74
72
  args = ["screen", "--market", market, "--top", str(top)]
75
73
  if config:
76
74
  args += ["--config", config]
77
- raw = _run("screener.py", args, timeout=120)
75
+ raw = _run("screener.py", args, timeout=command_timeout("screener.py", args))
78
76
  return {"screen_results": _parse_json(raw)}
79
77
 
80
78
 
@@ -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