stock-analysis-plugin 0.2.9 → 0.2.11
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 +36 -25
- package/package.json +16 -12
- package/pi/index.ts +251 -11
- package/scripts/profile-offline.py +127 -0
- package/scripts/profile-workflows.py +87 -0
- package/skills/growth-quality/SKILL.md +2 -0
- package/skills/stock-analysis/SKILL.md +14 -3
- package/skills/stock-analysis/references/stateless-analysis.md +16 -2
- package/skills/stock-screener/SKILL.md +4 -0
- package/skills/strategy-backtest/SKILL.md +8 -3
- package/tools/_cache.py +93 -21
- package/tools/_subproc.py +43 -8
- package/tools/alert_rules.py +63 -14
- package/tools/anomaly_detect.py +11 -6
- package/tools/backtest.py +225 -70
- package/tools/capabilities.py +1 -0
- package/tools/gather.py +63 -8
- package/tools/market_regime.py +26 -15
- package/tools/market_review.py +2 -2
- package/tools/name_resolver.py +16 -14
- package/tools/position_context.py +5 -2
- package/tools/requirements.txt +4 -4
- package/tools/risk_screening.py +40 -15
- package/tools/screener.py +44 -18
- package/tools/search_intel.py +95 -33
- package/tools/sec_holders.py +27 -25
- package/tools/signal_tracker.py +106 -54
- package/tools/stateless_analysis.py +600 -33
- package/tools/stock_data.py +366 -50
- package/tools/technical.py +42 -40
- package/tools/tiaa_disclosures.py +263 -0
- package/tools/trading_calendar.py +26 -21
- package/tools/value_data.py +6 -2
- package/tools/watchlist.py +24 -15
- package/tools/watchlist_context.py +63 -49
package/tools/alert_rules.py
CHANGED
|
@@ -140,28 +140,77 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
|
|
|
140
140
|
needs_anom = any(r.get("type") == "anomaly" for r in rules)
|
|
141
141
|
needs_risk = any(r.get("type") in ("risk_veto", "risk_level_at_least") for r in rules)
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
jobs
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
needs_quote = (
|
|
144
|
+
needs_anom
|
|
145
|
+
or needs_risk
|
|
146
|
+
or any(r.get("type") in ("price_below", "price_above", "change_pct_above", "change_pct_below") for r in rules)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Share one market snapshot while retaining 60-bar alerts and 120-bar risk analysis.
|
|
150
|
+
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
151
|
+
jobs = {}
|
|
152
|
+
if needs_quote:
|
|
153
|
+
quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
|
|
154
|
+
jobs["quote"] = quote_job
|
|
155
|
+
if needs_tech or needs_anom or needs_risk:
|
|
156
|
+
from technical import analyze_records
|
|
157
|
+
|
|
158
|
+
kline_job = pool.submit(
|
|
159
|
+
_run_json,
|
|
160
|
+
"stock_data.py",
|
|
161
|
+
["kline", symbol, "--period", "daily", "--count", "120" if needs_risk else "60"],
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
def analyze(count):
|
|
165
|
+
records = kline_job.result()
|
|
166
|
+
try:
|
|
167
|
+
return analyze_records(symbol, records[-count:] if isinstance(records, list) else records)
|
|
168
|
+
except Exception as exc:
|
|
169
|
+
return {"error": f"Technical analysis failed ({type(exc).__name__})"}
|
|
170
|
+
|
|
171
|
+
if needs_risk:
|
|
172
|
+
from risk_screening import screen_risk
|
|
173
|
+
|
|
174
|
+
technical_job = pool.submit(analyze, 120)
|
|
175
|
+
|
|
176
|
+
def collect_risk():
|
|
177
|
+
try:
|
|
178
|
+
return screen_risk(
|
|
179
|
+
symbol, inputs={"quote": quote_job, "technical": technical_job}, run=_run_json
|
|
180
|
+
)
|
|
181
|
+
except Exception as exc:
|
|
182
|
+
return {"error": f"Risk analysis failed ({type(exc).__name__})"}
|
|
183
|
+
|
|
184
|
+
jobs["risk"] = pool.submit(collect_risk)
|
|
185
|
+
if needs_tech:
|
|
186
|
+
jobs["tech"] = pool.submit(analyze, 60)
|
|
187
|
+
if needs_anom:
|
|
188
|
+
from anomaly_detect import detect_anomalies
|
|
189
|
+
|
|
190
|
+
def anomalies():
|
|
191
|
+
records = kline_job.result()
|
|
192
|
+
try:
|
|
193
|
+
return detect_anomalies(
|
|
194
|
+
symbol,
|
|
195
|
+
records=records[-60:] if isinstance(records, list) else records,
|
|
196
|
+
quote=quote_job.result(),
|
|
197
|
+
run=_run_json,
|
|
198
|
+
)
|
|
199
|
+
except Exception as exc:
|
|
200
|
+
return {"error": f"Anomaly analysis failed ({type(exc).__name__})"}
|
|
201
|
+
|
|
202
|
+
jobs["anom"] = pool.submit(anomalies)
|
|
203
|
+
fetched = {key: future.result() for key, future in jobs.items()}
|
|
155
204
|
fetched = {
|
|
156
205
|
key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
|
|
157
206
|
}
|
|
158
|
-
quote = fetched
|
|
207
|
+
quote = fetched.get("quote") or {}
|
|
159
208
|
tech = fetched.get("tech")
|
|
160
209
|
anom = fetched.get("anom")
|
|
161
210
|
risk = fetched.get("risk")
|
|
162
211
|
|
|
163
212
|
warnings = []
|
|
164
|
-
if not quote or quote.get("price") is None:
|
|
213
|
+
if needs_quote and (not quote or quote.get("price") is None):
|
|
165
214
|
warnings.append("quote fetch failed or missing price — price/change rules cannot fire")
|
|
166
215
|
if needs_tech and not tech:
|
|
167
216
|
warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
|
package/tools/anomaly_detect.py
CHANGED
|
@@ -323,12 +323,12 @@ def detect_limit_hit(symbol: str, quote: dict) -> list:
|
|
|
323
323
|
return anomalies
|
|
324
324
|
|
|
325
325
|
|
|
326
|
-
def detect_capital_flow_anomaly(symbol: str) -> list:
|
|
326
|
+
def detect_capital_flow_anomaly(symbol: str, *, run=None) -> list:
|
|
327
327
|
market = detect_market(symbol)
|
|
328
328
|
if market != "A":
|
|
329
329
|
return []
|
|
330
330
|
|
|
331
|
-
flow_data = _run_tool("stock_data.py", ["capital_flow", symbol, "--mode", "detail"])
|
|
331
|
+
flow_data = (run or _run_tool)("stock_data.py", ["capital_flow", symbol, "--mode", "detail"])
|
|
332
332
|
if not flow_data or not isinstance(flow_data, list) or len(flow_data) < 3:
|
|
333
333
|
return []
|
|
334
334
|
|
|
@@ -468,8 +468,12 @@ def detect_gap(df: pd.DataFrame) -> list:
|
|
|
468
468
|
# --------------- Orchestration ---------------
|
|
469
469
|
|
|
470
470
|
|
|
471
|
-
|
|
472
|
-
|
|
471
|
+
_UNSET = object()
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def detect_anomalies(symbol: str, *, records=_UNSET, quote=_UNSET, run=None) -> dict:
|
|
475
|
+
if records is _UNSET:
|
|
476
|
+
records = fetch_kline(symbol, "daily", 60)
|
|
473
477
|
if isinstance(records, dict) and "error" in records:
|
|
474
478
|
return {"symbol": symbol, "error": records.get("error", "Failed to fetch kline")}
|
|
475
479
|
if not records:
|
|
@@ -484,7 +488,8 @@ def detect_anomalies(symbol: str) -> dict:
|
|
|
484
488
|
low = df["low"]
|
|
485
489
|
volume = df["volume"]
|
|
486
490
|
|
|
487
|
-
|
|
491
|
+
if quote is _UNSET:
|
|
492
|
+
quote = _run_tool("stock_data.py", ["quote", symbol]) or {}
|
|
488
493
|
|
|
489
494
|
anomalies = []
|
|
490
495
|
anomalies.extend(detect_macd_cross(close))
|
|
@@ -501,7 +506,7 @@ def detect_anomalies(symbol: str) -> dict:
|
|
|
501
506
|
anomalies.extend(detect_limit_hit(symbol, quote))
|
|
502
507
|
anomalies.extend(detect_large_move(quote))
|
|
503
508
|
|
|
504
|
-
anomalies.extend(detect_capital_flow_anomaly(symbol))
|
|
509
|
+
anomalies.extend(detect_capital_flow_anomaly(symbol, **({"run": run} if run else {})))
|
|
505
510
|
|
|
506
511
|
severity_order = {"high": 0, "medium": 1, "low": 2}
|
|
507
512
|
anomalies.sort(key=lambda a: severity_order.get(a["severity"], 3))
|
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(
|
|
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);
|
|
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)
|
|
114
|
+
return rsi_series(close, period)
|
|
113
115
|
|
|
114
116
|
|
|
115
|
-
# Pure aliases (identical signatures); the other wrappers below
|
|
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
|
-
|
|
135
|
-
return
|
|
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 (
|
|
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
|
|
180
|
-
"ma": lambda: compute_ma(close, period or
|
|
181
|
-
"ema": lambda: compute_ema(close, period or
|
|
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,
|
|
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
|
-
"
|
|
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(
|
|
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
|
-
|
|
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(
|
|
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
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
|
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,
|
|
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
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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(
|
|
472
|
+
price = float(closes[signal_idx])
|
|
347
473
|
fill_price = float(row["close"] if execution == "same_close" else row["open"])
|
|
348
|
-
signal_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
|
|
354
|
-
|
|
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(
|
|
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
|
|
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
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
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
|
|
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(
|
|
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
|
-
|
|
766
|
-
|
|
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
|
-
|
|
784
|
-
|
|
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 =
|
|
788
|
-
actual_end =
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
package/tools/capabilities.py
CHANGED
|
@@ -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},
|