@weaxs/openclaw-stock-analysis 0.2.9 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tools/backtest.py CHANGED
@@ -72,12 +72,15 @@ def _substitute(s: str, params: dict):
72
72
  # --------------- Data Fetching ---------------
73
73
 
74
74
 
75
- def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame:
75
+ def fetch_kline(
76
+ symbol: str, start: str | None, end: str | None, include_warmup: bool = False, warmup_bars: int = 0
77
+ ) -> pd.DataFrame:
76
78
  count = 500
77
79
  if start and end:
78
80
  d0 = datetime.strptime(start, "%Y-%m-%d")
79
81
  d1 = datetime.strptime(end, "%Y-%m-%d")
80
82
  count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
83
+ count += warmup_bars
81
84
 
82
85
  data = run_tool(
83
86
  "stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)], preserve_errors=True
@@ -94,7 +97,7 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
94
97
  df["date"] = pd.to_datetime(df["date"], errors="coerce")
95
98
  df = df.sort_values("date").reset_index(drop=True)
96
99
 
97
- if start:
100
+ if start and not include_warmup:
98
101
  df = df[df["date"] >= start]
99
102
  if end:
100
103
  df = df[df["date"] <= end]
@@ -104,16 +107,14 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
104
107
  # --------------- Indicators ---------------
105
108
  #
106
109
  # Series math lives in technical.py (ma_series/ema_series/macd_series/rsi_series/
107
- # bollinger_series); the wrappers below only add backtest-specific NaN defaults
108
- # (rsi → 50, bollinger position → 0.5) so strategy evaluation never sees NaN.
110
+ # bollinger_series); warmup remains NaN so it cannot create trading conditions.
109
111
 
110
112
 
111
113
  def compute_rsi(close: pd.Series, period: int = 14) -> pd.Series:
112
- return rsi_series(close, period).fillna(50)
114
+ return rsi_series(close, period)
113
115
 
114
116
 
115
- # Pure aliases (identical signatures); the other wrappers below add
116
- # backtest-specific NaN defaults or pick one leg of the MACD tuple.
117
+ # Pure aliases (identical signatures); the other wrappers below pick one leg of the MACD tuple.
117
118
  compute_ma = ma_series
118
119
  compute_ema = ema_series
119
120
 
@@ -131,8 +132,8 @@ def compute_macd_hist(close: pd.Series) -> pd.Series:
131
132
 
132
133
 
133
134
  def compute_volume_ratio(volume: pd.Series, period: int = 5) -> pd.Series:
134
- ma = ma_series(volume, period)
135
- return (volume / ma.replace(0, np.nan)).fillna(1)
135
+ previous_average = ma_series(volume, period).shift(1)
136
+ return volume / previous_average.replace(0, np.nan)
136
137
 
137
138
 
138
139
  def compute_price_change(close: pd.Series) -> pd.Series:
@@ -142,7 +143,7 @@ def compute_price_change(close: pd.Series) -> pd.Series:
142
143
  def compute_bollinger_position(close: pd.Series, period: int = 20) -> pd.Series:
143
144
  upper, mid, lower = bollinger_series(close, period)
144
145
  width = (upper - lower).replace(0, np.nan)
145
- return ((close - lower) / width).fillna(0.5)
146
+ return (close - lower) / width
146
147
 
147
148
 
148
149
  def _resolve_ma_pair(fast: int | None, slow: int | None, period: int | None, default: tuple) -> tuple:
@@ -171,26 +172,45 @@ def compute_ema_diff(
171
172
  return compute_ema(close, fast) - compute_ema(close, slow)
172
173
 
173
174
 
175
+ CONDITION_OPERATORS = {">", "<", ">=", "<=", "==", "cross_above", "cross_below"}
176
+ DEFAULT_INDICATOR_PERIODS = {
177
+ "rsi": 14,
178
+ "ma": 20,
179
+ "ema": 20,
180
+ "monthly_momentum": 63,
181
+ "volume_ratio": 5,
182
+ "bollinger_position": 20,
183
+ }
184
+ DEFAULT_TREND_PERIODS = (20, 60, 14)
185
+
186
+
174
187
  def get_indicator(
175
188
  df: pd.DataFrame, name: str, period: int | None = None, fast: int | None = None, slow: int | None = None
176
189
  ) -> pd.Series:
177
190
  close = df["close"]
178
191
  dispatch = {
179
- "rsi": lambda: compute_rsi(close, period or 14),
180
- "ma": lambda: compute_ma(close, period or 20),
181
- "ema": lambda: compute_ema(close, period or 20),
192
+ "rsi": lambda: compute_rsi(close, period or DEFAULT_INDICATOR_PERIODS["rsi"]),
193
+ "ma": lambda: compute_ma(close, period or DEFAULT_INDICATOR_PERIODS["ma"]),
194
+ "ema": lambda: compute_ema(close, period or DEFAULT_INDICATOR_PERIODS["ema"]),
182
195
  "macd_dif": lambda: compute_macd_dif(close),
183
196
  "macd_dea": lambda: compute_macd_dea(close),
184
197
  "macd": lambda: compute_macd_hist(close),
185
198
  "ma_diff": lambda: compute_ma_diff(close, period, fast, slow),
186
199
  "ema_diff": lambda: compute_ema_diff(close, period, fast, slow),
187
200
  "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
201
+ df,
202
+ fast if fast is not None else DEFAULT_TREND_PERIODS[0],
203
+ slow if slow is not None else DEFAULT_TREND_PERIODS[1],
204
+ period if period is not None else DEFAULT_TREND_PERIODS[2],
205
+ ),
206
+ "monthly_momentum": lambda: monthly_momentum_series(
207
+ df, period or DEFAULT_INDICATOR_PERIODS["monthly_momentum"]
189
208
  ),
190
- "monthly_momentum": lambda: monthly_momentum_series(df, period if period is not None else 63),
191
- "volume_ratio": lambda: compute_volume_ratio(df["volume"], period or 5),
209
+ "volume_ratio": lambda: compute_volume_ratio(df["volume"], period or DEFAULT_INDICATOR_PERIODS["volume_ratio"]),
192
210
  "price_change": lambda: compute_price_change(close),
193
- "bollinger_position": lambda: compute_bollinger_position(close, period or 20),
211
+ "bollinger_position": lambda: compute_bollinger_position(
212
+ close, period or DEFAULT_INDICATOR_PERIODS["bollinger_position"]
213
+ ),
194
214
  "close": lambda: close,
195
215
  "volume": lambda: df["volume"].astype(float),
196
216
  }
@@ -203,10 +223,11 @@ def get_indicator(
203
223
  # --------------- Condition Evaluation ---------------
204
224
 
205
225
 
206
- def check_condition(series: pd.Series, operator: str, value: float, idx: int) -> bool:
226
+ def check_condition(series: pd.Series | np.ndarray, operator: str, value: float, idx: int) -> bool:
207
227
  if idx < 0 or idx >= len(series):
208
228
  return False
209
- cur = float(series.iloc[idx])
229
+ values = series.iloc if isinstance(series, pd.Series) else series
230
+ cur = float(values[idx])
210
231
  ops = {
211
232
  ">": cur > value,
212
233
  "<": cur < value,
@@ -218,7 +239,7 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
218
239
  return ops[operator]
219
240
  if idx == 0:
220
241
  return False
221
- prev = float(series.iloc[idx - 1])
242
+ prev = float(values[idx - 1])
222
243
  if operator == "cross_above":
223
244
  return prev <= value and cur > value
224
245
  if operator == "cross_below":
@@ -226,38 +247,81 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
226
247
  return False
227
248
 
228
249
 
229
- def _coerce_int(v):
230
- if isinstance(v, str):
231
- try:
232
- return int(float(v))
233
- except (ValueError, TypeError):
234
- return None
235
- return v
250
+ def _condition_parameters(cond: dict) -> tuple:
251
+ values = []
252
+ for key in ("period", "fast_period", "slow_period"):
253
+ if key not in cond:
254
+ values.append(None)
255
+ continue
256
+ value = cond[key]
257
+ if isinstance(value, str) and re.fullmatch(r"[0-9]+", value):
258
+ value = int(value)
259
+ if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
260
+ raise ValueError(f"{key} must be a positive integer or integer string, got {value!r}")
261
+ values.append(value)
262
+ return tuple(values)
263
+
264
+
265
+ def _condition_value(cond: dict) -> float:
266
+ raw = cond.get("value")
267
+ if isinstance(raw, bool):
268
+ raise ValueError(f"condition value must be a finite number, got {raw!r}")
269
+ try:
270
+ value = float(raw)
271
+ except (TypeError, ValueError, OverflowError) as e:
272
+ raise ValueError(f"condition value must be a finite number, got {raw!r}") from e
273
+ if not math.isfinite(value):
274
+ raise ValueError(f"condition value must be a finite number, got {raw!r}")
275
+ return value
276
+
277
+
278
+ def _strategy_warmup_bars(strategy: dict) -> int:
279
+ lookback = 0
280
+ for side in ("entry", "exit"):
281
+ for cond in strategy.get(side, {}).get("conditions", []):
282
+ name = cond["indicator"].lower()
283
+ period, fast, slow = _condition_parameters(cond)
284
+ if name in ("ma_diff", "ema_diff"):
285
+ defaults = (5, 20) if name == "ma_diff" else (12, 26)
286
+ fast, slow = _resolve_ma_pair(fast, slow, period, defaults)
287
+ bars = max(fast, slow)
288
+ elif name == "trend_score":
289
+ bars = max(slow or DEFAULT_TREND_PERIODS[1], (period or DEFAULT_TREND_PERIODS[2]) + 1)
290
+ elif name in ("macd_dif", "macd_dea", "macd"):
291
+ bars = 26 if name == "macd_dif" else 34
292
+ elif name == "price_change":
293
+ bars = 2
294
+ else:
295
+ bars = period or DEFAULT_INDICATOR_PERIODS.get(name, 1)
296
+ if name in ("rsi", "monthly_momentum", "volume_ratio"):
297
+ bars += 1
298
+ lookback = max(lookback, bars + (cond.get("operator") in ("cross_above", "cross_below")))
299
+ return lookback
300
+
301
+
302
+ def _condition_series(df: pd.DataFrame, cond: dict, cache: dict) -> tuple[np.ndarray, str]:
303
+ if cond.get("operator") not in CONDITION_OPERATORS:
304
+ raise ValueError(f"Unknown condition operator: {cond.get('operator')}")
305
+ ind_name = cond["indicator"]
306
+ period, fast, slow = _condition_parameters(cond)
307
+ cache_key = f"{ind_name}_{period}_{fast}_{slow}"
308
+ if cache_key not in cache:
309
+ cache[cache_key] = get_indicator(df, ind_name, period, fast, slow).to_numpy()
310
+ label = f"{ind_name}({fast},{slow})" if fast or slow else f"{ind_name}({period})" if period else ind_name
311
+ return cache[cache_key], label
236
312
 
237
313
 
238
314
  def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int, cache: dict) -> tuple[bool, str]:
239
315
  results = []
240
316
  reasons = []
241
317
  for cond in conditions:
242
- ind_name = cond["indicator"]
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)
248
-
249
- cache_key = f"{ind_name}_{period}_{fast}_{slow}"
250
- if cache_key not in cache:
251
- cache[cache_key] = get_indicator(df, ind_name, period, fast, slow)
252
- series = cache[cache_key]
253
-
318
+ series, label = _condition_series(df, cond, cache)
254
319
  op = cond["operator"]
255
- val = float(cond["value"])
320
+ val = _condition_value(cond)
256
321
  hit = check_condition(series, op, val, idx)
257
322
  results.append(hit)
258
323
  if hit:
259
- cur_val = round(float(series.iloc[idx]), 2)
260
- label = f"{ind_name}({fast},{slow})" if fast or slow else f"{ind_name}({period})" if period else ind_name
324
+ cur_val = round(float(series[idx]), 2)
261
325
  reasons.append(f"{label} = {cur_val} {op} {val}")
262
326
 
263
327
  if logic == "all":
@@ -265,23 +329,67 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
265
329
  return any(results), "; ".join(reasons)
266
330
 
267
331
 
332
+ def _entry_warmup_warnings(df: pd.DataFrame, strategy: dict, start: str, execution: str) -> list[str]:
333
+ dates = pd.to_datetime(df["date"], errors="coerce")
334
+ window = np.flatnonzero((dates >= start).to_numpy())
335
+ first_bar = max(1, int(window[0]))
336
+ signal_idx = first_bar if execution == "same_close" else first_bar - 1
337
+ warnings = []
338
+ cache = {}
339
+ for cond in strategy.get("entry", {}).get("conditions", []):
340
+ series, label = _condition_series(df, cond, cache)
341
+ indices = [signal_idx]
342
+ if cond["operator"] in ("cross_above", "cross_below"):
343
+ indices.append(signal_idx - 1)
344
+ if any(idx < 0 or pd.isna(series[idx]) for idx in indices):
345
+ signal_date = str(pd.Timestamp(df["date"].iloc[signal_idx]).date())
346
+ warnings.append(
347
+ f"Entry indicator is unavailable: {label} at first window signal {signal_date}; check warmup and data quality"
348
+ )
349
+ return warnings
350
+
351
+
268
352
  # --------------- Simulation ---------------
269
353
 
270
354
 
355
+ def _validate_adjustment_basis(df: pd.DataFrame) -> None:
356
+ if "adjusted" not in df:
357
+ return
358
+ bases = set(df["adjusted"].fillna("unknown").astype(str))
359
+ if not bases or not bases <= {"qfq", "total_return", "none"}:
360
+ raise ValueError("adjusted contains unknown adjustment basis; expected qfq, total_return or none")
361
+ if len(bases) != 1:
362
+ raise ValueError("adjusted must use a single adjustment basis: qfq, total_return or none")
363
+
364
+
271
365
  def simulate(
272
- df: pd.DataFrame, strategy: dict, capital: float, symbol: str, *, entry_gate: pd.Series | None = None
366
+ df: pd.DataFrame,
367
+ strategy: dict,
368
+ capital: float,
369
+ symbol: str,
370
+ *,
371
+ entry_gate: pd.Series | None = None,
372
+ start: str | None = None,
273
373
  ) -> dict:
274
374
  if not math.isfinite(capital) or capital <= 0:
275
375
  raise ValueError("capital must be finite and positive")
376
+ _validate_adjustment_basis(df)
276
377
  if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
277
378
  raise ValueError("backtest prices must be finite and positive")
278
379
  dates = pd.to_datetime(df["date"], errors="coerce")
279
380
  if dates.isna().any() or dates.duplicated().any() or not dates.is_monotonic_increasing:
280
381
  raise ValueError("backtest dates must be valid, unique and increasing")
382
+ first_bar = 1
383
+ if start is not None:
384
+ start_date = pd.Timestamp(start)
385
+ window_bars = np.flatnonzero((dates >= start_date).to_numpy())
386
+ if pd.isna(start_date) or not len(window_bars):
387
+ raise ValueError("start must be a valid date within the backtest data")
388
+ first_bar = max(1, int(window_bars[0]))
281
389
  if entry_gate is not None:
282
390
  if not entry_gate.index.equals(df.index) or not (entry_gate.isna() | entry_gate.isin([True, False])).all():
283
391
  raise ValueError("entry_gate must contain aligned booleans or unavailable values")
284
- entry_gate = entry_gate.fillna(False).astype(bool)
392
+ entry_gate = entry_gate.fillna(False).astype(bool).to_numpy()
285
393
  market = detect_market(symbol)
286
394
  lot_size = 100 if market == "A" else 1
287
395
  sim_cfg = strategy.get("simulation", {})
@@ -306,17 +414,31 @@ def simulate(
306
414
  size_frac = float(position_cfg.get("size", 1.0))
307
415
  if not 0 <= size_frac <= 1:
308
416
  raise ValueError("position.size must be a fraction in [0, 1]")
417
+ if position_cfg.get("max_positions", 1) != 1:
418
+ raise ValueError("position.max_positions must be 1 for the single-position engine")
309
419
  stop_loss = float(exit_.get("stop_loss", -0.10))
310
420
  take_profit = float(exit_.get("take_profit", 0.50))
311
421
  ts_val = exit_.get("trailing_stop")
312
422
  trailing_stop = float(ts_val) if ts_val is not None else None
423
+ if not math.isfinite(stop_loss) or stop_loss >= 0:
424
+ raise ValueError("exit.stop_loss must be a finite negative fraction")
425
+ if not math.isfinite(take_profit) or take_profit <= 0:
426
+ raise ValueError("exit.take_profit must be a finite positive fraction")
427
+ if trailing_stop is not None and (not math.isfinite(trailing_stop) or not 0 < trailing_stop < 1):
428
+ raise ValueError("exit.trailing_stop must be a finite fraction in (0, 1)")
313
429
 
314
430
  entry_conditions = entry.get("conditions", [])
315
431
  entry_logic = entry.get("logic", "all")
316
432
  exit_conditions = exit_.get("conditions", [])
317
433
  exit_logic = exit_.get("logic", "any")
434
+ for side, logic in (("entry", entry_logic), ("exit", exit_logic)):
435
+ if logic not in ("all", "any"):
436
+ raise ValueError(f"{side}.logic must be all or any")
318
437
 
319
438
  cache: dict = {}
439
+ for cond in entry_conditions + exit_conditions:
440
+ _condition_series(df, cond, cache)
441
+ _condition_value(cond)
320
442
  trades = []
321
443
  equity_curve = []
322
444
  holding = None
@@ -339,19 +461,26 @@ def simulate(
339
461
  and abs(float(row["high"]) - float(limit)) < 0.005
340
462
  )
341
463
 
342
- for i in range(1, len(df)):
343
- row = df.iloc[i]
344
- date_str = str(row["date"].date()) if hasattr(row["date"], "date") else str(row["date"])
464
+ bars = df.to_dict("records")
465
+ closes = df["close"].to_numpy()
466
+ date_strings = [str(value.date()) if hasattr(value, "date") else str(value) for value in df["date"]]
467
+ signal_dates = dates.dt.strftime("%Y-%m-%d").tolist()
468
+ for i in range(first_bar, len(df)):
469
+ row = bars[i]
470
+ date_str = date_strings[i]
345
471
  signal_idx = i if execution == "same_close" else i - 1
346
- price = float(df["close"].iloc[signal_idx])
472
+ price = float(closes[signal_idx])
347
473
  fill_price = float(row["close"] if execution == "same_close" else row["open"])
348
- signal_date = str(pd.Timestamp(df["date"].iloc[signal_idx]).date())
474
+ signal_date = signal_dates[signal_idx]
349
475
 
350
476
  if holding is None:
351
477
  hit, reason = evaluate_conditions(df, entry_conditions, entry_logic, signal_idx, cache)
352
478
  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]:
479
+ if hit:
480
+ reason, signal_date = pending
481
+ else:
482
+ pending = None
483
+ if entry_gate is not None and not entry_gate[signal_idx]:
355
484
  hit, pending = False, None
356
485
  if hit and not tradable(row, "buy"):
357
486
  pending = (reason, signal_date)
@@ -453,7 +582,7 @@ def simulate(
453
582
  equity_curve.append({"date": date_str, "equity": round(equity, 2)})
454
583
 
455
584
  if holding:
456
- last_price = float(df["close"].iloc[-1])
585
+ last_price = float(closes[-1])
457
586
  equity = cash + holding["shares"] * last_price
458
587
  equity_curve[-1]["equity"] = round(equity, 2)
459
588
 
@@ -560,8 +689,10 @@ def compute_metrics(trades: list, equity_curve: list, initial: float, final: flo
560
689
 
561
690
  win_rate = len(winning) / total_trades if total_trades else 0
562
691
  avg_win = np.mean([t["pnl"] for t in winning]) if winning else 0
563
- avg_loss = abs(np.mean([t["pnl"] for t in losing])) if losing else 1
692
+ avg_loss = abs(np.mean([t["pnl"] for t in losing])) if losing else 0
564
693
  pl_ratio = avg_win / avg_loss if avg_loss > 0 else float("inf")
694
+ if not winning:
695
+ pl_ratio = float("nan")
565
696
 
566
697
  avg_days = np.mean([t.get("holding_days", 0) for t in sell_trades]) if sell_trades else 0
567
698
 
@@ -601,11 +732,12 @@ def diagnose(metrics: dict) -> dict:
601
732
  weaknesses.append(f"胜率偏低 ({round(wr * 100, 1)}%)")
602
733
 
603
734
  plr = metrics["profit_loss_ratio"]
604
- if plr >= 2:
605
- strengths.append(f"盈亏比优秀 ({plr}:1)")
606
- elif plr < 1:
607
- weaknesses.append(f"盈亏比不足 ({plr}:1)")
608
- suggestions.append("考虑提高止盈目标或收紧止损")
735
+ if plr is not None and not math.isnan(plr):
736
+ if plr >= 2:
737
+ strengths.append(f"盈亏比优秀 ({plr}:1)")
738
+ elif plr < 1:
739
+ weaknesses.append(f"盈亏比不足 ({plr}:1)")
740
+ suggestions.append("考虑提高止盈目标或收紧止损")
609
741
 
610
742
  mdd = abs(metrics["max_drawdown"])
611
743
  if mdd > 0.2:
@@ -655,7 +787,7 @@ def diagnose_advanced(metrics: dict, trades: list, equity_curve: list, strategy:
655
787
  1
656
788
  for t in sell_trades
657
789
  if (t.get("reason") or "").startswith(("stop_loss", "止损触发", "移动止损触发"))
658
- or (t.get("pnl_pct", 0) is not None and t.get("pnl_pct", 0) <= stop_loss * 100 + 0.5)
790
+ or (t.get("pnl_pct", 0) is not None and t.get("pnl_pct", 0) <= stop_loss + 0.005)
659
791
  )
660
792
  stop_ratio = stop_hits / max(1, len(sell_trades))
661
793
 
@@ -754,16 +886,31 @@ def sample_curve(curve: list, max_points: int = 200) -> list:
754
886
  # --------------- Entry Points ---------------
755
887
 
756
888
 
757
- def _benchmark_comparison(symbol: str, start: str, end: str, strategy_return: float) -> dict:
889
+ def _benchmark_comparison(
890
+ symbol: str, start: str, end: str, strategy_return: float, execution: str = "next_open"
891
+ ) -> dict:
758
892
  """Buy-and-hold benchmark over the same window; degrades to an error note on fetch failure."""
759
893
  try:
760
894
  df = fetch_kline(symbol, start, end)
895
+ _validate_adjustment_basis(df)
761
896
  except Exception as e:
762
897
  return {"symbol": symbol, "error": str(e)}
763
898
  if len(df) < 2:
764
899
  return {"symbol": symbol, "error": f"Insufficient benchmark data: {len(df)} rows"}
765
- first, last = float(df["close"].iloc[0]), float(df["close"].iloc[-1])
766
- total_return = (last - first) / first if first else 0.0
900
+ dates = pd.to_datetime(df["date"], errors="coerce")
901
+ if dates.isna().any():
902
+ return {"symbol": symbol, "error": "Benchmark dates must be valid"}
903
+ actual_start, actual_end = str(dates.iloc[0].date()), str(dates.iloc[-1].date())
904
+ if (actual_start, actual_end) != (start, end):
905
+ return {
906
+ "symbol": symbol,
907
+ "error": f"Benchmark data covers {actual_start} to {actual_end}; requested {start} to {end}",
908
+ }
909
+ entry_column = "open" if execution == "next_open" else "close"
910
+ first, last = float(df[entry_column].iloc[0]), float(df["close"].iloc[-1])
911
+ if not all(math.isfinite(price) and price > 0 for price in (first, last)):
912
+ return {"symbol": symbol, "error": "Benchmark first and last prices must be finite and positive"}
913
+ total_return = (last - first) / first
767
914
  return {
768
915
  "symbol": symbol,
769
916
  "total_return": round(total_return, 4),
@@ -779,18 +926,21 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
779
926
  if not start:
780
927
  start = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
781
928
 
782
- df = fetch_kline(symbol, start, end)
783
- if len(df) < 30:
784
- return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
929
+ df = fetch_kline(symbol, start, end, True, _strategy_warmup_bars(strategy))
930
+ evaluation = df[(df["date"] >= start) & (df["date"] <= end)]
931
+ if len(evaluation) < 30:
932
+ return {"error": f"Insufficient data: {len(evaluation)} rows (need at least 30)"}
785
933
 
786
934
  requested_period = f"{start} to {end}"
787
- actual_start = df["date"].iloc[0].strftime("%Y-%m-%d")
788
- actual_end = df["date"].iloc[-1].strftime("%Y-%m-%d")
935
+ actual_start = evaluation["date"].iloc[0].strftime("%Y-%m-%d")
936
+ actual_end = evaluation["date"].iloc[-1].strftime("%Y-%m-%d")
789
937
  warnings = []
790
938
  if actual_start != start or actual_end != end:
791
939
  warnings.append(f"Actual data covers {actual_start} to {actual_end}; requested {requested_period}")
792
940
  start, end = actual_start, actual_end
793
- sim = simulate(df, strategy, capital, symbol)
941
+ execution = strategy.get("simulation", {}).get("execution", "next_open")
942
+ warnings.extend(_entry_warmup_warnings(df, strategy, start, execution))
943
+ sim = simulate(df, strategy, capital, symbol, start=start)
794
944
  final = sim["final_equity"]
795
945
  metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, final, start, end)
796
946
 
@@ -812,7 +962,10 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
812
962
  }
813
963
  benchmark_symbol = strategy.get("benchmark")
814
964
  if benchmark_symbol:
815
- result["benchmark"] = _benchmark_comparison(str(benchmark_symbol), start, end, metrics["total_return"])
965
+ benchmark_start = sim["equity_curve"][0]["date"] if sim["equity_curve"] else start
966
+ result["benchmark"] = _benchmark_comparison(
967
+ str(benchmark_symbol), benchmark_start, end, metrics["total_return"], execution
968
+ )
816
969
  return result
817
970
 
818
971
 
@@ -825,7 +978,9 @@ def evaluate_result(path: str) -> dict:
825
978
  final = data.get("final_capital", initial)
826
979
  period = data.get("period", "unknown")
827
980
  parts = period.split(" to ") if " to " in period else [period, period]
828
- metrics = compute_metrics(trades, curve, initial, final, parts[0], parts[1])
981
+ metrics = data.get("metrics")
982
+ if metrics is None:
983
+ metrics = compute_metrics(trades, curve, initial, final, parts[0], parts[1])
829
984
  data["metrics"] = metrics
830
985
  data["diagnosis"] = diagnose(metrics)
831
986
  return data
@@ -28,6 +28,7 @@ TOOL_MATRIX = {
28
28
  "compare_stocks": {"markets": ALL_MARKETS, "reason": None},
29
29
  "analyze_disclosure_changes": {"markets": ALL_MARKETS, "reason": None},
30
30
  "analyze_portfolio_risk": {"markets": ALL_MARKETS, "reason": None},
31
+ "plan_portfolio_rebalance": {"markets": ALL_MARKETS, "reason": None},
31
32
  "calculate_position_size": {"markets": ALL_MARKETS, "reason": None},
32
33
  "analyze_event_study": {"markets": ALL_MARKETS, "reason": None},
33
34
  "validate_backtest": {"markets": ALL_MARKETS, "reason": None},
package/tools/gather.py CHANGED
@@ -2,13 +2,16 @@
2
2
  """Unified data gathering — parallel subprocess calls to tools for skill scripts."""
3
3
 
4
4
  import argparse
5
+ import copy
5
6
  import json
6
7
  import sys
8
+ import threading
7
9
  from concurrent.futures import ThreadPoolExecutor
8
10
  from pathlib import Path
9
11
 
10
12
  sys.path.insert(0, str(Path(__file__).resolve().parent))
11
13
  from _subproc import command_timeout, json_safe, run_tool, utf8_stdio # noqa: E402
14
+ from risk_screening import screen_risk # noqa: E402
12
15
  from stock_data import detect_market # noqa: E402
13
16
  from technical import analyze_records # noqa: E402
14
17
 
@@ -31,15 +34,68 @@ def _parse_json(raw: str | None):
31
34
  return raw
32
35
 
33
36
 
34
- def _run_all(tasks: dict, symbol: str) -> dict:
35
- with ThreadPoolExecutor(max_workers=len(tasks)) as pool:
36
- futures = {pool.submit(_run, script, args): key for key, (script, args) in tasks.items()}
37
- result = {key: _parse_json(f.result()) for f, key in futures.items()}
38
- result["technical"] = analyze_records(symbol, result["kline"]) if result["kline"] is not None else None
37
+ class Collection:
38
+ """One invocation's exact CLI requests, shared across stock continuations."""
39
+
40
+ def __init__(self):
41
+ self.pool = ThreadPoolExecutor(max_workers=6)
42
+ self.lock = threading.Lock()
43
+ self.tasks = {}
44
+
45
+ def __enter__(self):
46
+ return self
47
+
48
+ def __exit__(self, *exc):
49
+ self.pool.shutdown(wait=True)
50
+
51
+ def submit(self, script, args, timeout=None):
52
+ key = (script, tuple(args), timeout if timeout is not None else command_timeout(script, args))
53
+ with self.lock:
54
+ if key not in self.tasks:
55
+
56
+ def collect():
57
+ raw = _run(script, args, **({"timeout": timeout} if timeout is not None else {}))
58
+ return _parse_json(raw)
59
+
60
+ self.tasks[key] = self.pool.submit(collect)
61
+ return self.tasks[key]
62
+
63
+ def json(self, script, args, timeout=None):
64
+ return copy.deepcopy(self.submit(script, args, timeout).result())
65
+
66
+
67
+ def _run_all(tasks: dict, symbol: str, *, session=None, with_risk=False) -> dict:
68
+ if session is None:
69
+ with Collection() as session:
70
+ return _run_all(tasks, symbol, session=session, with_risk=with_risk)
71
+ for script, args in tasks.values():
72
+ session.submit(script, args)
73
+ with ThreadPoolExecutor(max_workers=2) as continuations:
74
+
75
+ def analyze():
76
+ records = session.json(*tasks["kline"])
77
+ return analyze_records(symbol, records) if records is not None else None
78
+
79
+ technical = continuations.submit(analyze)
80
+ risk = None
81
+ if with_risk:
82
+ inputs = {"quote": session.submit(*tasks["quote"]), "technical": technical}
83
+
84
+ def collect_risk():
85
+ try:
86
+ return screen_risk(symbol, inputs=inputs, run=session.json)
87
+ except Exception as exc:
88
+ return {"error": f"Risk analysis failed ({type(exc).__name__})"}
89
+
90
+ risk = continuations.submit(collect_risk)
91
+ result = {key: session.json(script, args) for key, (script, args) in tasks.items()}
92
+ result["technical"] = technical.result()
93
+ if risk is not None:
94
+ result["risk"] = risk.result()
39
95
  return result
40
96
 
41
97
 
42
- def gather_analysis(symbol: str) -> dict:
98
+ def gather_analysis(symbol: str, *, session=None) -> dict:
43
99
  market = detect_market(symbol)
44
100
  tasks = {
45
101
  "quote": ("stock_data.py", ["quote", symbol]),
@@ -49,11 +105,10 @@ def gather_analysis(symbol: str) -> dict:
49
105
  "chip_distribution": ("stock_data.py", ["chip_distribution", symbol]),
50
106
  "news": ("search_intel.py", ["search", _news_query(symbol)]),
51
107
  "social_sentiment": ("search_intel.py", ["sentiment", symbol]),
52
- "risk": ("risk_screening.py", ["screen", symbol]),
53
108
  }
54
109
  if market in ("A", "HK", "US"):
55
110
  tasks["regime"] = ("market_regime.py", ["detect", market])
56
- result = _run_all(tasks, symbol)
111
+ result = _run_all(tasks, symbol, session=session, with_risk=True)
57
112
  if market not in ("A", "HK", "US"):
58
113
  result["regime"] = {"error": f"Market regime unavailable for {market}"}
59
114
  return result