@weaxs/openclaw-stock-analysis 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/README.md +1 -1
- package/dist/index.js +268 -18
- package/index.ts +197 -11
- package/openclaw.plugin.json +8 -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/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 +9 -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 +73 -14
- package/tools/capabilities.py +8 -0
- package/tools/cli_budgets.json +26 -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/watchlist_context.py +18 -7
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
|
|
package/tools/signal_tracker.py
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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 {
|
|
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 = [
|
|
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},
|