@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.
Files changed (38) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +284 -19
  3. package/index.ts +214 -12
  4. package/openclaw.plugin.json +9 -1
  5. package/package.json +1 -1
  6. package/schemas/market_review_schema.json +12 -5
  7. package/schemas/report_schema.json +65 -1
  8. package/scripts/cli-timeout.mjs +27 -0
  9. package/scripts/json-input.mjs +26 -0
  10. package/skills/bull-trend/SKILL.md +11 -1
  11. package/skills/bull-trend/references/monthly-trend-strategy.md +47 -0
  12. package/skills/bull-trend/references/trend-horizons.md +43 -0
  13. package/skills/bull-trend/references/trend-temperature.md +57 -0
  14. package/skills/bull-trend/scripts/market_trend.py +78 -0
  15. package/skills/event-driven/SKILL.md +4 -0
  16. package/skills/growth-quality/SKILL.md +4 -0
  17. package/skills/market-review/SKILL.md +2 -0
  18. package/skills/stock-analysis/SKILL.md +17 -3
  19. package/skills/stock-analysis/references/stateless-analysis.md +66 -0
  20. package/skills/stock-screener/SKILL.md +5 -1
  21. package/skills/strategy-backtest/SKILL.md +19 -0
  22. package/templates/market_review/full.md.j2 +7 -0
  23. package/templates/stock_analysis/_evidence.md.j2 +11 -0
  24. package/templates/stock_analysis/brief.md.j2 +2 -0
  25. package/templates/stock_analysis/full.md.j2 +2 -0
  26. package/tools/_subproc.py +66 -11
  27. package/tools/backtest.py +143 -19
  28. package/tools/capabilities.py +12 -0
  29. package/tools/cli_budgets.json +27 -0
  30. package/tools/gather.py +4 -6
  31. package/tools/market_review.py +30 -9
  32. package/tools/screener.py +57 -15
  33. package/tools/signal_tracker.py +77 -6
  34. package/tools/stateless_analysis.py +630 -0
  35. package/tools/stock_data.py +90 -15
  36. package/tools/technical.py +186 -0
  37. package/tools/trend_strategy.py +365 -0
  38. package/tools/watchlist_context.py +18 -7
@@ -0,0 +1,365 @@
1
+ #!/usr/bin/env python3
2
+ """Stateless monthly signal plans with SMA200 eligibility and volatility budgets."""
3
+
4
+ import argparse
5
+ import json
6
+ import math
7
+ import re
8
+ import stat
9
+ import sys
10
+ from concurrent.futures import ThreadPoolExecutor, as_completed
11
+ from pathlib import Path
12
+
13
+ import numpy as np
14
+ import pandas as pd
15
+
16
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
17
+
18
+ from _subproc import run_tool, utf8_stdio
19
+ from stock_data import detect_market
20
+ from technical import _trend_prices, ma_series
21
+
22
+ MODEL = "sma200_risk_budget"
23
+ ALLOCATION = 0.8704
24
+
25
+
26
+ def iso_date(value):
27
+ if not isinstance(value, str) or not re.fullmatch(r"\d{4}-\d{2}-\d{2}", value):
28
+ raise ValueError("as_of and price dates require YYYY-MM-DD")
29
+ day = pd.Timestamp(value)
30
+ if pd.isna(day) or str(day.date()) != value:
31
+ raise ValueError("Invalid date")
32
+ return day
33
+
34
+
35
+ def symbol_set(payload, key, names):
36
+ values = payload.get(key, [])
37
+ if not isinstance(values, list) or any(type(n) is not str for n in values):
38
+ raise ValueError(f"{key} must be a list of unique pool symbols")
39
+ if len(values) != len(set(values)) or not set(values) <= names:
40
+ raise ValueError(f"{key} must contain unique pool symbols")
41
+ return set(values)
42
+
43
+
44
+ def symbol_identity(name, market):
45
+ return int(name[:-3]) if market == "HK" else name.replace(".", "-")
46
+
47
+
48
+ def pool_members(symbols, market):
49
+ patterns = {"A": r"\d{6}", "HK": r"\d{4,5}\.HK", "US": r"[A-Z][A-Z0-9.-]{0,14}"}
50
+ if market not in patterns:
51
+ raise ValueError("Rule62 supports a single A, HK or US stock pool")
52
+ if not isinstance(symbols, list) or not 1 <= len(symbols) <= 100:
53
+ raise ValueError("A pool requires 1..100 distinct stocks")
54
+ if any(type(n) is not str or not re.fullmatch(patterns[market], n) or detect_market(n) != market for n in symbols):
55
+ raise ValueError("Symbols must be stocks from the declared single market")
56
+ names = sorted(symbols, key=lambda n: symbol_identity(n, market))
57
+ if len({symbol_identity(n, market) for n in names}) != len(names):
58
+ raise ValueError("Duplicate symbol aliases")
59
+ return names
60
+
61
+
62
+ def pool_state(payload, names):
63
+ held = symbol_set(payload, "held_symbols", set(names))
64
+ pending = symbol_set(payload, "pending_exit_symbols", set(names))
65
+ if not pending <= held:
66
+ raise ValueError("pending_exit_symbols must be real held_symbols")
67
+ slots = payload.get("slots", math.ceil(len(names) / 3))
68
+ if type(slots) not in (int, float) or not 1 <= slots <= len(names) or int(slots) != slots or len(held) > slots:
69
+ raise ValueError("slots requires an integer in 1..N and cannot be below actual held count")
70
+ return held, pending, int(slots)
71
+
72
+
73
+ def analyze(payload):
74
+ allowed = {"market", "as_of", "histories", "slots", "held_symbols", "pending_exit_symbols"}
75
+ if not isinstance(payload, dict) or set(payload) - allowed:
76
+ raise ValueError("Expected strategy input with only documented fields; corporate actions are not supported")
77
+ market = payload["market"]
78
+ as_of = iso_date(payload["as_of"])
79
+ histories = payload["histories"]
80
+ if not isinstance(histories, dict):
81
+ raise ValueError("histories requires distinct stocks")
82
+ names = pool_members(list(histories), market)
83
+ held, pending, slots = pool_state(payload, names)
84
+ stocks, calendar, known_vol = {}, set(), {}
85
+ for name in names:
86
+ records = histories[name]
87
+ if not isinstance(records, list) or not 1 <= len(records) <= 20000:
88
+ raise ValueError("Each history requires 1..20000 dated price rows")
89
+ for row in records:
90
+ if not isinstance(row, dict) or not {"date", "close", "volume"} <= row.keys():
91
+ raise ValueError("Each price row requires date, close and volume")
92
+ iso_date(row["date"])
93
+ if any(type(row[k]) not in (int, float) for k in ("close", "volume")):
94
+ raise ValueError("close and volume must be numbers, excluding booleans")
95
+ frame = pd.DataFrame(records)
96
+ frame["date"] = pd.to_datetime(frame.date)
97
+ # Validate chronology before cutting the prefix; never silently sort duplicate/bad input.
98
+ _trend_prices(frame)
99
+ frame = frame.loc[frame.date <= as_of]
100
+ if frame.empty:
101
+ raise ValueError("Every pool member needs history at or before as_of")
102
+ close, dates = _trend_prices(frame)
103
+ calendar.update(dates)
104
+ average = ma_series(close, 200)
105
+ returns = np.log(close).diff()
106
+ sigma = returns.rolling(63).std(ddof=1)
107
+ sigma = sigma.mask(returns.rolling(63).min().eq(returns.rolling(63).max()), 0.0)
108
+ available = not dates.empty and dates.iloc[-1] == as_of
109
+
110
+ def last(series, available=available):
111
+ value = series.iloc[-1] if available and not series.empty else np.nan
112
+ return float(value) if np.isfinite(value) else None
113
+
114
+ score = last(np.log(close) - np.log(average))
115
+ volatility = last(sigma)
116
+ if volatility is not None and volatility > 0:
117
+ known_vol[name] = volatility
118
+ basis = str(frame.adjusted.iloc[-1]) if "adjusted" in frame and pd.notna(frame.adjusted.iloc[-1]) else "unknown"
119
+ stocks[name] = {
120
+ "quote_available": bool(available),
121
+ "quote_as_of": str(dates.iloc[-1].date()) if not dates.empty else None,
122
+ "price_basis": basis,
123
+ "valid_bars": len(close),
124
+ "close": last(close),
125
+ "sma200": last(average),
126
+ "sma200_21_bars_ago": last(average.shift(21)),
127
+ "distance": score,
128
+ "sigma63": volatility,
129
+ "rising_sma200": bool(available and len(close) >= 221 and average.iloc[-1] > average.shift(21).iloc[-1]),
130
+ "new_entry_eligible": False,
131
+ "action": "wait",
132
+ "reason": "not_monthly_observation",
133
+ "new_budget_fraction": 0.0,
134
+ }
135
+ stocks[name]["new_entry_eligible"] = bool(
136
+ score is not None and score > 0 and stocks[name]["rising_sma200"] and name in known_vol
137
+ )
138
+ month_dates = [day for day in calendar if day.to_period("M") == as_of.to_period("M")]
139
+ monthly = bool(month_dates and min(month_dates) == as_of)
140
+ selected = []
141
+ reference = float(np.median(list(known_vol.values()))) if known_vol else None
142
+ insufficient_held = any(stocks[n]["quote_available"] and stocks[n]["sma200"] is None for n in held - pending)
143
+ status = "monthly_observation" if monthly else "waiting_for_monthly_observation"
144
+ if insufficient_held:
145
+ status = "insufficient_held_history"
146
+ if monthly and not insufficient_held:
147
+ retained = [
148
+ n for n in names if n in held - pending and (not stocks[n]["quote_available"] or stocks[n]["distance"] > 0)
149
+ ]
150
+ ranked = sorted(
151
+ (n for n in names if n not in held and stocks[n]["new_entry_eligible"]),
152
+ key=lambda n: (-stocks[n]["distance"], symbol_identity(n, market)),
153
+ )
154
+ selected = retained + ranked[: max(0, slots - len(retained))]
155
+ for name, row in stocks.items():
156
+ if name in retained:
157
+ row.update(
158
+ action="hold",
159
+ reason="positive_stock_trend" if row["quote_available"] else "unobservable_held_reserves_slot",
160
+ )
161
+ elif name in held - pending:
162
+ row.update(action="exit_next_valid_open", reason="monthly_sma200_nonpositive")
163
+ elif name in selected:
164
+ row.update(
165
+ action="entry_candidate_next_valid_open",
166
+ reason="rising_sma200_ranked_vacancy",
167
+ new_budget_fraction=ALLOCATION / slots * min(1.0, reference / known_vol[name]),
168
+ )
169
+ else:
170
+ row["reason"] = "capacity_reserved" if row["new_entry_eligible"] else "entry_conditions_not_met"
171
+ for name in pending:
172
+ stocks[name].update(action="exit_pending", reason="previously_locked_exit")
173
+ return {
174
+ "model": MODEL,
175
+ "validation_status": "not_validated",
176
+ "kind": "monthly_signal_plan",
177
+ "market": market,
178
+ "as_of": str(as_of.date()),
179
+ "status": status,
180
+ "observation_date": str(as_of.date()) if monthly else None,
181
+ "last_available_date": str(max(calendar).date()) if calendar else None,
182
+ "portfolio_state": "explicit_held_symbols" if held else "initially_flat",
183
+ "slots": slots,
184
+ "allocation": ALLOCATION,
185
+ "reference_volatility": reference,
186
+ "selected": selected,
187
+ "stocks": stocks,
188
+ "execution": {
189
+ "requires_available_cash_and_capacity": True,
190
+ "budget_basis": "next_execution_batch_open_nav_times_new_budget_fraction_including_buy_costs",
191
+ "budget_is_not_target_weight": True,
192
+ "existing_holdings_are_not_resized": True,
193
+ "sell_before_buy": True,
194
+ "fills_or_quantities_calculated": False,
195
+ },
196
+ "validation": {
197
+ "development_gates": "passed",
198
+ "expanded_and_historical_pools": "failed",
199
+ "independent_validation": "not_completed",
200
+ },
201
+ "warnings": [
202
+ "Caller must provide complete daily history through a closed as_of bar, consistent adjusted prices and the real holdings at as_of. Missing pool members change ranking and the volatility median.",
203
+ "Only the first observed positive-volume stock-pool close of the month creates new plans. Mid-month output does not replay past decisions or reconstruct orders.",
204
+ "Budgets are conditional cash limits, not share quantities, target weights or fills. Locked exits require actual execution before cash/capacity is reused; no daily re-entry or averaging down.",
205
+ "Corporate-action conversion, raw-share lots, settlement, trading limits and fees require separate execution checks. No account is stored and no orders are submitted.",
206
+ "Development gates passed only on the development sample. Expanded and historical pools failed; allocation .8704 was fitted. This is not the private Trend Animal temperature or a forecast of fat-tail returns.",
207
+ ],
208
+ }
209
+
210
+
211
+ def latest_closed_date(market, now=None):
212
+ import exchange_calendars as xcals
213
+
214
+ calendar = xcals.get_calendar({"A": "XSHG", "HK": "XHKG", "US": "XNYS"}[market])
215
+ now = pd.Timestamp.now(tz="UTC") if now is None else pd.Timestamp(now)
216
+ if now.tzinfo is None:
217
+ raise ValueError("Current time must include a timezone")
218
+ local_date = str(now.tz_convert(calendar.tz).date())
219
+ session = calendar.date_to_session(local_date, direction="previous")
220
+ # XHKG calendars end continuous trading; its closing auction can run ten more minutes.
221
+ close = calendar.session_close(session) + pd.Timedelta(minutes=10 if market == "HK" else 0)
222
+ if now <= close:
223
+ session = calendar.previous_session(session)
224
+ return str(session.date())
225
+
226
+
227
+ def request_symbol(name):
228
+ name = name.strip().upper()
229
+ if re.fullmatch(r"\d{4,5}\.HK", name):
230
+ return f"{int(name[:-3]):04d}.HK"
231
+ return name.replace(".", "-") if detect_market(name) == "US" else name
232
+
233
+
234
+ def _fetch_daily_history(symbol, count):
235
+ return run_tool("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], timeout=30)
236
+
237
+
238
+ def fetch_plan(request):
239
+ allowed = {"symbols", "as_of", "held_symbols", "pending_exit_symbols", "slots"}
240
+ if not isinstance(request, dict) or set(request) - allowed:
241
+ raise ValueError("Expected symbols and optional as_of/held_symbols/pending_exit_symbols/slots")
242
+ symbols = request["symbols"]
243
+ if not isinstance(symbols, list) or not 1 <= len(symbols) <= 100 or any(type(n) is not str for n in symbols):
244
+ raise ValueError("symbols must be a nonempty list of stock codes")
245
+ symbols = [request_symbol(n) for n in symbols]
246
+ markets = {detect_market(n) for n in symbols}
247
+ if len(markets) != 1:
248
+ raise ValueError("A pool must contain a single market")
249
+ market = markets.pop()
250
+ names = pool_members(symbols, market)
251
+ state = {}
252
+ for key in ("held_symbols", "pending_exit_symbols"):
253
+ values = request.get(key, [])
254
+ if not isinstance(values, list) or any(type(n) is not str for n in values):
255
+ raise ValueError("Held and pending symbols require lists of codes")
256
+ state[key] = [request_symbol(n) for n in values]
257
+ if "slots" in request:
258
+ state["slots"] = request["slots"]
259
+ pool_state(state, names)
260
+ try:
261
+ closed = latest_closed_date(market)
262
+ except Exception:
263
+ return {
264
+ "error": "Exchange calendar unavailable; cannot identify a completed daily bar",
265
+ "error_code": "calendar_unavailable",
266
+ }
267
+ as_of = iso_date(request.get("as_of", closed))
268
+ if as_of > iso_date(closed):
269
+ raise ValueError("as_of must not be later than the most recently completed exchange session")
270
+ count = 360 + (iso_date(closed) - as_of).days
271
+ if count > 20000 or count * len(names) > 200000:
272
+ raise ValueError("Requested date exceeds the supported price history")
273
+ histories, failed = {}, []
274
+ with ThreadPoolExecutor(max_workers=min(6, len(names))) as pool:
275
+ futures = {pool.submit(_fetch_daily_history, name, count): name for name in names}
276
+ for future in as_completed(futures):
277
+ name = futures[future]
278
+ try:
279
+ records = future.result()
280
+ except Exception:
281
+ records = None
282
+ if not isinstance(records, list) or not records or len(records) > count:
283
+ failed.append(name)
284
+ else:
285
+ histories[name] = records
286
+ if failed:
287
+ return {
288
+ "error": "Cannot load the complete stock pool; no strategy plan was produced",
289
+ "error_code": "incomplete_pool",
290
+ "failed_symbols": sorted(failed),
291
+ }
292
+ try:
293
+ result = analyze({"market": market, "as_of": str(as_of.date()), "histories": histories, **state})
294
+ except (KeyError, TypeError, ValueError, OverflowError):
295
+ return {
296
+ "error": "Fetched daily prices failed validation; no strategy plan was produced",
297
+ "error_code": "invalid_market_data",
298
+ }
299
+ result["data_acquisition"] = {
300
+ "source": "stock_data.py:kline",
301
+ "symbols": names,
302
+ "requested_count": count,
303
+ "latest_closed_session": closed,
304
+ }
305
+ result["warnings"][0] = (
306
+ "Daily histories were fetched for the entire requested pool. Verify provider adjustment and quote dates; held_symbols must describe real holdings at as_of."
307
+ )
308
+ return result
309
+
310
+
311
+ def unique_object(pairs):
312
+ result = {}
313
+ for key, value in pairs:
314
+ if key in result:
315
+ raise ValueError("Duplicate JSON object key")
316
+ result[key] = value
317
+ return result
318
+
319
+
320
+ class Parser(argparse.ArgumentParser):
321
+ def error(self, message):
322
+ raise ValueError(message)
323
+
324
+
325
+ def main():
326
+ utf8_stdio()
327
+ parser = Parser(description=__doc__)
328
+ parser.add_argument(
329
+ "action", nargs="?", choices=["fetch"], help="Fetch prices from stock codes; omit only for offline snapshots"
330
+ )
331
+ parser.add_argument("--input", required=True, help="Internal UTF-8 request JSON or offline snapshot file")
332
+ try:
333
+ args = parser.parse_args()
334
+ except ValueError:
335
+ print(
336
+ json.dumps({"error": "Invalid command arguments; use --help for usage", "error_code": "invalid_arguments"})
337
+ )
338
+ return 1
339
+ try:
340
+ path = Path(args.input)
341
+ info = path.stat()
342
+ limit = 32 * 1024 * 1024
343
+ if not stat.S_ISREG(info.st_mode) or info.st_size > limit:
344
+ raise ValueError("Input requires a regular JSON file up to 32 MiB")
345
+ with path.open("rb") as source:
346
+ raw = source.read(limit + 1)
347
+ if len(raw) > limit:
348
+ raise ValueError("Input JSON exceeds 32 MiB")
349
+ payload = json.loads(raw.decode("utf-8"), object_pairs_hook=unique_object)
350
+ result = fetch_plan(payload) if args.action == "fetch" else analyze(payload)
351
+ except (KeyError, TypeError, ValueError, OverflowError, RecursionError):
352
+ result = {
353
+ "error": "Invalid stock-pool request; use one market and check codes, dates, holdings and slots"
354
+ if args.action == "fetch"
355
+ else "Invalid trend strategy input; check documented dates, symbols, prices, holdings and slots",
356
+ "error_code": "invalid_request" if args.action == "fetch" else "invalid_snapshot",
357
+ }
358
+ except OSError:
359
+ result = {"error": "Cannot read trend strategy input JSON file"}
360
+ print(json.dumps(result, ensure_ascii=False, allow_nan=False))
361
+ return 1 if "error" in result else 0
362
+
363
+
364
+ if __name__ == "__main__":
365
+ raise SystemExit(main())
@@ -14,7 +14,7 @@ from pathlib import Path
14
14
 
15
15
  sys.path.insert(0, str(Path(__file__).resolve().parent))
16
16
  from _subproc import run_tool as _run_json # noqa: E402
17
- from _subproc import utf8_stdio
17
+ from _subproc import scrub_secrets, utf8_stdio
18
18
  from stock_data import detect_market # noqa: E402
19
19
  from watchlist import analyze_watchlist, has_analysis_data # noqa: E402
20
20
 
@@ -46,8 +46,15 @@ def _build_item(symbol: str, data: dict | None) -> dict:
46
46
  risk = data.get("risk") or {}
47
47
 
48
48
  # anomalies — separate call (watchlist gather doesn't include them)
49
- anom_result = _run_json("anomaly_detect.py", ["detect", symbol]) or {}
50
- anomalies = anom_result.get("anomalies", []) if isinstance(anom_result, dict) else []
49
+ anom_result = _run_json("anomaly_detect.py", ["detect", symbol], preserve_errors=True)
50
+ anomaly_ok = (
51
+ isinstance(anom_result, dict) and "error" not in anom_result and isinstance(anom_result.get("anomalies"), list)
52
+ )
53
+ anomalies = anom_result["anomalies"] if anomaly_ok else []
54
+ anomaly_error = None
55
+ if not anomaly_ok:
56
+ error = anom_result.get("error") if isinstance(anom_result, dict) else None
57
+ anomaly_error = scrub_secrets(error) if isinstance(error, str) else "Anomaly data unavailable"
51
58
 
52
59
  score = tech.get("signal_score")
53
60
  trend = (tech.get("trend") or {}).get("overall") or "unknown"
@@ -72,7 +79,10 @@ def _build_item(symbol: str, data: dict | None) -> dict:
72
79
  "anomalies": [
73
80
  {"type": a.get("type"), "severity": a.get("severity"), "direction": a.get("direction")} for a in anomalies
74
81
  ],
75
- "anomaly_count": len(anomalies),
82
+ "anomaly_count": len(anomalies) if anomaly_ok else None,
83
+ "anomaly_status": "ok" if anomaly_ok else "unavailable",
84
+ "anomaly_error": anomaly_error,
85
+ "warnings": [f"Anomaly detection unavailable: {anomaly_error}"] if anomaly_error else [],
76
86
  "next_tools": _suggest_next_tools(market, trend, anomalies, risk_level),
77
87
  }
78
88
 
@@ -100,11 +110,12 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
100
110
  "fetched_at": datetime.now(timezone.utc).isoformat(),
101
111
  "freshness": "realtime_or_latest_trade_day",
102
112
  "fallback_used": False,
103
- "warnings": [],
113
+ "warnings": [f"{i['symbol']}: {warning}" for i in items for warning in i.get("warnings", [])],
104
114
  },
105
115
  "summary": {
106
116
  "total": len(symbols),
107
- "success": sum(1 for i in items if "error" not in i),
117
+ "success": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "ok"),
118
+ "partial": sum(1 for i in items if "error" not in i and i.get("anomaly_status") == "unavailable"),
108
119
  "failed": sum(1 for i in items if "error" in i),
109
120
  "high_risk_count": sum(1 for i in items if i.get("risk_level") == "high"),
110
121
  "veto_count": sum(1 for i in items if i.get("veto_buy")),
@@ -116,7 +127,7 @@ def build_context(symbols: list[str], include_market_review: bool = False, worke
116
127
  markets = sorted({i.get("market") for i in items if i.get("market")})
117
128
  review = {}
118
129
  for m in markets:
119
- r = _run_json("market_review.py", ["review", "--market", m], timeout=60)
130
+ r = _run_json("market_review.py", ["review", "--market", m])
120
131
  if r:
121
132
  review[m] = r
122
133
  output["market_review"] = review