@weaxs/openclaw-stock-analysis 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.
- package/README.md +1 -1
- package/dist/index.js +284 -19
- package/index.ts +214 -12
- package/openclaw.plugin.json +9 -1
- package/package.json +1 -1
- package/schemas/market_review_schema.json +12 -5
- package/schemas/report_schema.json +65 -1
- package/scripts/cli-timeout.mjs +27 -0
- package/scripts/json-input.mjs +26 -0
- package/skills/bull-trend/SKILL.md +11 -1
- package/skills/bull-trend/references/monthly-trend-strategy.md +47 -0
- package/skills/bull-trend/references/trend-horizons.md +43 -0
- package/skills/bull-trend/references/trend-temperature.md +57 -0
- package/skills/bull-trend/scripts/market_trend.py +78 -0
- package/skills/event-driven/SKILL.md +4 -0
- package/skills/growth-quality/SKILL.md +4 -0
- package/skills/market-review/SKILL.md +2 -0
- package/skills/stock-analysis/SKILL.md +17 -3
- package/skills/stock-analysis/references/stateless-analysis.md +66 -0
- package/skills/stock-screener/SKILL.md +5 -1
- package/skills/strategy-backtest/SKILL.md +19 -0
- package/templates/market_review/full.md.j2 +7 -0
- package/templates/stock_analysis/_evidence.md.j2 +11 -0
- package/templates/stock_analysis/brief.md.j2 +2 -0
- package/templates/stock_analysis/full.md.j2 +2 -0
- package/tools/_subproc.py +66 -11
- package/tools/backtest.py +143 -19
- package/tools/capabilities.py +12 -0
- package/tools/cli_budgets.json +27 -0
- package/tools/gather.py +4 -6
- package/tools/market_review.py +30 -9
- package/tools/screener.py +57 -15
- package/tools/signal_tracker.py +77 -6
- package/tools/stateless_analysis.py +630 -0
- package/tools/stock_data.py +90 -15
- package/tools/technical.py +186 -0
- package/tools/trend_strategy.py +365 -0
- package/tools/watchlist_context.py +18 -7
package/tools/backtest.py
CHANGED
|
@@ -17,7 +17,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
19
|
from stock_data import detect_market
|
|
20
|
-
from technical import
|
|
20
|
+
from technical import (
|
|
21
|
+
bollinger_series,
|
|
22
|
+
ema_series,
|
|
23
|
+
ma_series,
|
|
24
|
+
macd_series,
|
|
25
|
+
monthly_market_entry_series,
|
|
26
|
+
monthly_momentum_series,
|
|
27
|
+
rsi_series,
|
|
28
|
+
trend_score_series,
|
|
29
|
+
)
|
|
21
30
|
|
|
22
31
|
# --------------- Strategy Loading ---------------
|
|
23
32
|
|
|
@@ -70,9 +79,9 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
|
|
|
70
79
|
d1 = datetime.strptime(end, "%Y-%m-%d")
|
|
71
80
|
count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
82
|
+
data = run_tool(
|
|
83
|
+
"stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], preserve_errors=True
|
|
84
|
+
)
|
|
76
85
|
if data is None:
|
|
77
86
|
raise RuntimeError(f"Failed to fetch kline for {symbol}")
|
|
78
87
|
if isinstance(data, dict) and "error" in data:
|
|
@@ -175,6 +184,10 @@ def get_indicator(
|
|
|
175
184
|
"macd": lambda: compute_macd_hist(close),
|
|
176
185
|
"ma_diff": lambda: compute_ma_diff(close, period, fast, slow),
|
|
177
186
|
"ema_diff": lambda: compute_ema_diff(close, period, fast, slow),
|
|
187
|
+
"trend_score": lambda: trend_score_series(
|
|
188
|
+
df, fast if fast is not None else 20, slow if slow is not None else 60, period if period is not None else 14
|
|
189
|
+
),
|
|
190
|
+
"monthly_momentum": lambda: monthly_momentum_series(df, period if period is not None else 63),
|
|
178
191
|
"volume_ratio": lambda: compute_volume_ratio(df["volume"], period or 5),
|
|
179
192
|
"price_change": lambda: compute_price_change(close),
|
|
180
193
|
"bollinger_position": lambda: compute_bollinger_position(close, period or 20),
|
|
@@ -191,7 +204,7 @@ def get_indicator(
|
|
|
191
204
|
|
|
192
205
|
|
|
193
206
|
def check_condition(series: pd.Series, operator: str, value: float, idx: int) -> bool:
|
|
194
|
-
if idx <
|
|
207
|
+
if idx < 0 or idx >= len(series):
|
|
195
208
|
return False
|
|
196
209
|
cur = float(series.iloc[idx])
|
|
197
210
|
ops = {
|
|
@@ -203,6 +216,8 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
|
|
|
203
216
|
}
|
|
204
217
|
if operator in ops:
|
|
205
218
|
return ops[operator]
|
|
219
|
+
if idx == 0:
|
|
220
|
+
return False
|
|
206
221
|
prev = float(series.iloc[idx - 1])
|
|
207
222
|
if operator == "cross_above":
|
|
208
223
|
return prev <= value and cur > value
|
|
@@ -225,9 +240,11 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
|
|
|
225
240
|
reasons = []
|
|
226
241
|
for cond in conditions:
|
|
227
242
|
ind_name = cond["indicator"]
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
243
|
+
parameters = (cond.get("period"), cond.get("fast_period"), cond.get("slow_period"))
|
|
244
|
+
if ind_name.lower() in ("trend_score", "monthly_momentum"):
|
|
245
|
+
period, fast, slow = (int(v) if isinstance(v, str) else v for v in parameters)
|
|
246
|
+
else:
|
|
247
|
+
period, fast, slow = map(_coerce_int, parameters)
|
|
231
248
|
|
|
232
249
|
cache_key = f"{ind_name}_{period}_{fast}_{slow}"
|
|
233
250
|
if cache_key not in cache:
|
|
@@ -251,14 +268,28 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
|
|
|
251
268
|
# --------------- Simulation ---------------
|
|
252
269
|
|
|
253
270
|
|
|
254
|
-
def simulate(
|
|
271
|
+
def simulate(
|
|
272
|
+
df: pd.DataFrame, strategy: dict, capital: float, symbol: str, *, entry_gate: pd.Series | None = None
|
|
273
|
+
) -> dict:
|
|
255
274
|
if not math.isfinite(capital) or capital <= 0:
|
|
256
275
|
raise ValueError("capital must be finite and positive")
|
|
257
276
|
if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
|
|
258
277
|
raise ValueError("backtest prices must be finite and positive")
|
|
278
|
+
dates = pd.to_datetime(df["date"], errors="coerce")
|
|
279
|
+
if dates.isna().any() or dates.duplicated().any() or not dates.is_monotonic_increasing:
|
|
280
|
+
raise ValueError("backtest dates must be valid, unique and increasing")
|
|
281
|
+
if entry_gate is not None:
|
|
282
|
+
if not entry_gate.index.equals(df.index) or not (entry_gate.isna() | entry_gate.isin([True, False])).all():
|
|
283
|
+
raise ValueError("entry_gate must contain aligned booleans or unavailable values")
|
|
284
|
+
entry_gate = entry_gate.fillna(False).astype(bool)
|
|
259
285
|
market = detect_market(symbol)
|
|
260
286
|
lot_size = 100 if market == "A" else 1
|
|
261
287
|
sim_cfg = strategy.get("simulation", {})
|
|
288
|
+
execution = sim_cfg.get("execution", "next_open")
|
|
289
|
+
if execution not in ("next_open", "same_close"):
|
|
290
|
+
raise ValueError("simulation.execution must be next_open or same_close")
|
|
291
|
+
if execution == "next_open" and ("open" not in df or not np.isfinite(df["open"]).all() or (df["open"] <= 0).any()):
|
|
292
|
+
raise ValueError("next_open requires finite positive open prices")
|
|
262
293
|
slippage = float(sim_cfg.get("slippage", 0.001))
|
|
263
294
|
commission = float(sim_cfg.get("commission", 0.0015))
|
|
264
295
|
tax_raw = sim_cfg.get("stamp_tax") # sell-side only (e.g. A股印花税); absent/empty = 0 (disabled)
|
|
@@ -293,19 +324,45 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
293
324
|
trade_id = 0
|
|
294
325
|
commission_paid = 0.0
|
|
295
326
|
stamp_tax_paid = 0.0
|
|
327
|
+
pending = None
|
|
328
|
+
blocked_orders = 0
|
|
329
|
+
|
|
330
|
+
def tradable(row, side):
|
|
331
|
+
if "volume" in row and (not pd.notna(row["volume"]) or row["volume"] <= 0):
|
|
332
|
+
return False
|
|
333
|
+
limit = row.get("limit_up" if side == "buy" else "limit_down")
|
|
334
|
+
# shortcut: limits need supplied historical prices; add verified limit series before enforcing provider bars.
|
|
335
|
+
return not (
|
|
336
|
+
limit is not None
|
|
337
|
+
and pd.notna(limit)
|
|
338
|
+
and row.get("high") == row.get("low")
|
|
339
|
+
and abs(float(row["high"]) - float(limit)) < 0.005
|
|
340
|
+
)
|
|
296
341
|
|
|
297
342
|
for i in range(1, len(df)):
|
|
298
343
|
row = df.iloc[i]
|
|
299
344
|
date_str = str(row["date"].date()) if hasattr(row["date"], "date") else str(row["date"])
|
|
300
|
-
|
|
345
|
+
signal_idx = i if execution == "same_close" else i - 1
|
|
346
|
+
price = float(df["close"].iloc[signal_idx])
|
|
347
|
+
fill_price = float(row["close"] if execution == "same_close" else row["open"])
|
|
348
|
+
signal_date = str(pd.Timestamp(df["date"].iloc[signal_idx]).date())
|
|
301
349
|
|
|
302
350
|
if holding is None:
|
|
303
|
-
hit, reason = evaluate_conditions(df, entry_conditions, entry_logic,
|
|
304
|
-
if
|
|
305
|
-
|
|
351
|
+
hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, signal_idx, cache)
|
|
352
|
+
if pending:
|
|
353
|
+
hit, reason, signal_date = True, pending[0], pending[1]
|
|
354
|
+
if entry_gate is not None and not entry_gate.iloc[signal_idx]:
|
|
355
|
+
hit, pending = False, None
|
|
356
|
+
if hit and not tradable(row, "buy"):
|
|
357
|
+
pending = (reason, signal_date)
|
|
358
|
+
blocked_orders += 1
|
|
359
|
+
if hit and tradable(row, "buy"):
|
|
360
|
+
pending = None
|
|
361
|
+
buy_price = fill_price * (1 + slippage)
|
|
306
362
|
buy_amount = cash * size_frac
|
|
307
363
|
shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
|
|
308
364
|
if shares <= 0:
|
|
365
|
+
equity_curve.append({"date": date_str, "equity": round(cash, 2)})
|
|
309
366
|
continue
|
|
310
367
|
cost = shares * buy_price * (1 + commission)
|
|
311
368
|
commission_paid += shares * buy_price * commission
|
|
@@ -317,13 +374,14 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
317
374
|
"buy_price": round(buy_price, 4),
|
|
318
375
|
"shares": shares,
|
|
319
376
|
"cost": cost,
|
|
320
|
-
"peak_close":
|
|
377
|
+
"peak_close": float(row["close"]),
|
|
321
378
|
}
|
|
322
379
|
trades.append(
|
|
323
380
|
{
|
|
324
381
|
"id": trade_id,
|
|
325
382
|
"type": "buy",
|
|
326
383
|
"date": date_str,
|
|
384
|
+
"signal_date": signal_date,
|
|
327
385
|
"price": round(buy_price, 2),
|
|
328
386
|
"shares": shares,
|
|
329
387
|
"amount": round(cost, 2),
|
|
@@ -348,10 +406,16 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
348
406
|
elif pnl_pct >= take_profit:
|
|
349
407
|
sell, reason = True, f"止盈触发 ({round(pnl_pct * 100, 1)}% >= {round(take_profit * 100, 1)}%)"
|
|
350
408
|
elif exit_conditions:
|
|
351
|
-
sell, reason = evaluate_conditions(df, exit_conditions, exit_logic,
|
|
352
|
-
|
|
353
|
-
if
|
|
354
|
-
|
|
409
|
+
sell, reason = evaluate_conditions(df, exit_conditions, exit_logic, signal_idx, cache)
|
|
410
|
+
|
|
411
|
+
if pending:
|
|
412
|
+
sell, reason, signal_date = True, pending[0], pending[1]
|
|
413
|
+
if sell and not tradable(row, "sell"):
|
|
414
|
+
pending = (reason, signal_date)
|
|
415
|
+
blocked_orders += 1
|
|
416
|
+
if sell and tradable(row, "sell"):
|
|
417
|
+
pending = None
|
|
418
|
+
sell_price = fill_price * (1 - slippage)
|
|
355
419
|
gross = holding["shares"] * sell_price
|
|
356
420
|
proceeds = gross * (1 - commission - stamp_tax)
|
|
357
421
|
commission_paid += gross * commission
|
|
@@ -371,6 +435,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
371
435
|
"id": trade_id_sell,
|
|
372
436
|
"type": "sell",
|
|
373
437
|
"date": date_str,
|
|
438
|
+
"signal_date": signal_date,
|
|
374
439
|
"price": round(sell_price, 2),
|
|
375
440
|
"shares": holding["shares"],
|
|
376
441
|
"amount": round(proceeds, 2),
|
|
@@ -384,7 +449,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
384
449
|
|
|
385
450
|
equity = cash
|
|
386
451
|
if holding:
|
|
387
|
-
equity += holding["shares"] *
|
|
452
|
+
equity += holding["shares"] * float(row["close"])
|
|
388
453
|
equity_curve.append({"date": date_str, "equity": round(equity, 2)})
|
|
389
454
|
|
|
390
455
|
if holding:
|
|
@@ -396,6 +461,22 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
396
461
|
"trades": trades,
|
|
397
462
|
"equity_curve": equity_curve,
|
|
398
463
|
"final_equity": equity_curve[-1]["equity"] if equity_curve else capital,
|
|
464
|
+
"execution": {
|
|
465
|
+
"model": execution,
|
|
466
|
+
"blocked_orders": blocked_orders,
|
|
467
|
+
"unfilled_order": pending is not None,
|
|
468
|
+
"limitations": [
|
|
469
|
+
"Stops are close signals, not intraday guaranteed stop prices",
|
|
470
|
+
"Historical price limits are enforced only when limit_up/limit_down are supplied",
|
|
471
|
+
"Queue priority, partial fills, market impact and settlement are not simulated",
|
|
472
|
+
"Adjusted OHLC are synthetic prices; corporate actions and cash dividends are not simulated separately",
|
|
473
|
+
]
|
|
474
|
+
+ (
|
|
475
|
+
["same_close uses the signal day's close and can overstate executable returns"]
|
|
476
|
+
if execution == "same_close"
|
|
477
|
+
else []
|
|
478
|
+
),
|
|
479
|
+
},
|
|
399
480
|
"costs": {
|
|
400
481
|
"slippage": slippage,
|
|
401
482
|
"commission": commission,
|
|
@@ -406,6 +487,48 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
406
487
|
}
|
|
407
488
|
|
|
408
489
|
|
|
490
|
+
def simulate_monthly_market(
|
|
491
|
+
df: pd.DataFrame, market: pd.DataFrame, capital: float, symbol: str, start: str, allocation: float = 1.0
|
|
492
|
+
) -> dict:
|
|
493
|
+
"""Fixed research protocol: three drifting sleeves, market-gated entries and explicit warmup."""
|
|
494
|
+
if isinstance(allocation, bool) or not math.isfinite(allocation) or not 0 < allocation <= 1:
|
|
495
|
+
raise ValueError("allocation must be finite and in (0, 1]")
|
|
496
|
+
if not math.isfinite(capital) or capital <= 0:
|
|
497
|
+
raise ValueError("capital must be finite and positive")
|
|
498
|
+
start_date = pd.Timestamp(start)
|
|
499
|
+
dates = pd.to_datetime(df["date"], errors="coerce")
|
|
500
|
+
before = dates[(dates < start_date) & (df["volume"] > 0)] if "volume" in df else dates[dates < start_date]
|
|
501
|
+
if pd.isna(start_date) or before.empty or not (dates >= start_date).any():
|
|
502
|
+
raise ValueError("start requires both prehistory and evaluation bars")
|
|
503
|
+
sleeve_capital = math.floor(capital * allocation / 3 * 100) / 100
|
|
504
|
+
curves, trades, paid = [], [], 0.0
|
|
505
|
+
for horizon in (21, 63, 252):
|
|
506
|
+
gate = monthly_market_entry_series(df, market, horizon, activation=before.iloc[-1])
|
|
507
|
+
condition = {"indicator": "monthly_momentum", "period": horizon, "value": 0}
|
|
508
|
+
strategy = {
|
|
509
|
+
"entry": {"conditions": [{**condition, "operator": ">"}]},
|
|
510
|
+
"exit": {"conditions": [{**condition, "operator": "<"}], "stop_loss": -1, "take_profit": 1e9},
|
|
511
|
+
"simulation": {"execution": "next_open", "commission": 0.001, "slippage": 0.001, "stamp_tax": 0},
|
|
512
|
+
}
|
|
513
|
+
result = simulate(df, strategy, sleeve_capital, symbol, entry_gate=gate)
|
|
514
|
+
curves.append(pd.Series({row["date"]: row["equity"] for row in result["equity_curve"]}))
|
|
515
|
+
trades.extend({**trade, "horizon": horizon} for trade in result["trades"])
|
|
516
|
+
paid += result["costs"]["commission_paid"]
|
|
517
|
+
total = pd.concat(curves, axis=1).sum(axis=1) + capital - sleeve_capital * 3
|
|
518
|
+
return {
|
|
519
|
+
"model": "monthly_momentum_market_entry",
|
|
520
|
+
"validation_status": "not_validated",
|
|
521
|
+
"experimental": True,
|
|
522
|
+
"allocation": allocation,
|
|
523
|
+
"start": str(start_date.date()),
|
|
524
|
+
"warmup_start": str(dates.iloc[0].date()),
|
|
525
|
+
"trades": sorted(trades, key=lambda trade: (trade["date"], trade["horizon"])),
|
|
526
|
+
"equity_curve": [{"date": date, "equity": round(float(value), 2)} for date, value in total.items()],
|
|
527
|
+
"final_equity": round(float(total.iloc[-1]), 2),
|
|
528
|
+
"costs": {"commission": 0.001, "slippage": 0.001, "stamp_tax": 0, "commission_paid": round(paid, 2)},
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
|
|
409
532
|
# --------------- Metrics ---------------
|
|
410
533
|
|
|
411
534
|
|
|
@@ -680,6 +803,7 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
|
|
|
680
803
|
"initial_capital": capital,
|
|
681
804
|
"final_capital": final,
|
|
682
805
|
"costs": sim["costs"],
|
|
806
|
+
"execution": sim["execution"],
|
|
683
807
|
"metrics": metrics,
|
|
684
808
|
"trades": sim["trades"],
|
|
685
809
|
"equity_curve": sample_curve(sim["equity_curve"]),
|
package/tools/capabilities.py
CHANGED
|
@@ -18,6 +18,18 @@ ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
|
|
|
18
18
|
|
|
19
19
|
# tool_name -> {markets: [supported markets], reason: str for unsupported}
|
|
20
20
|
TOOL_MATRIX = {
|
|
21
|
+
"analyze_trend_strategy": {
|
|
22
|
+
"markets": ["A", "HK", "US"],
|
|
23
|
+
"reason": "Monthly trend strategy supports A/HK/US pools only",
|
|
24
|
+
},
|
|
25
|
+
# Explicit-input analytics; data acquisition retains each provider's market limits.
|
|
26
|
+
"compare_stocks": {"markets": ALL_MARKETS, "reason": None},
|
|
27
|
+
"analyze_disclosure_changes": {"markets": ALL_MARKETS, "reason": None},
|
|
28
|
+
"analyze_portfolio_risk": {"markets": ALL_MARKETS, "reason": None},
|
|
29
|
+
"calculate_position_size": {"markets": ALL_MARKETS, "reason": None},
|
|
30
|
+
"analyze_event_study": {"markets": ALL_MARKETS, "reason": None},
|
|
31
|
+
"validate_backtest": {"markets": ALL_MARKETS, "reason": None},
|
|
32
|
+
"compare_analysis_snapshots": {"markets": ALL_MARKETS, "reason": None},
|
|
21
33
|
"get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
|
|
22
34
|
"get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
|
|
23
35
|
"get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
"trend_strategy.py:fetch": 660
|
|
27
|
+
}
|
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
|
-
|
|
17
|
-
|
|
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=
|
|
75
|
+
raw = _run("screener.py", args, timeout=command_timeout("screener.py", args))
|
|
78
76
|
return {"screen_results": _parse_json(raw)}
|
|
79
77
|
|
|
80
78
|
|
package/tools/market_review.py
CHANGED
|
@@ -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 = [
|
|
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":
|
|
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
|
|
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
|
-
|
|
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 =
|
|
52
|
-
"""
|
|
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(
|
|
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
|
|
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"] =
|
|
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"] =
|
|
155
|
+
df["liquidity_score"] = pd.concat(liq_parts, axis=1).mean(axis=1) if liq_parts else np.nan
|
|
151
156
|
|
|
152
|
-
|
|
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
|
-
|
|
302
|
-
"
|
|
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(
|
|
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
|
|