openclaw-stock-analysis 0.2.2 → 0.2.6
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/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/index.js +228 -30
- package/index.ts +246 -33
- package/openclaw.plugin.json +13 -1
- package/package.json +1 -1
- package/schemas/report_schema.json +3 -3
- package/scripts/ci-resolve-llm-host.mjs +29 -0
- package/scripts/setup-python.mjs +11 -13
- package/skills/bull-trend/SKILL.md +34 -10
- package/skills/bull-trend/references/fat-tails.md +91 -0
- package/skills/bull-trend/scripts/tail_analysis.py +132 -0
- package/skills/event-driven/SKILL.md +8 -0
- package/skills/growth-quality/SKILL.md +8 -0
- package/skills/hot-theme/SKILL.md +3 -2
- package/skills/stock-analysis/SKILL.md +31 -3
- package/skills/stock-screener/SKILL.md +21 -1
- package/skills/strategy-backtest/SKILL.md +15 -5
- package/skills/wisburg-research/SKILL.md +2 -0
- package/strategies/examples/bottom_volume.yaml +5 -0
- package/strategies/examples/box_oscillation.yaml +5 -0
- package/strategies/examples/bull_trend.yaml +11 -3
- package/strategies/examples/dragon_head.yaml +4 -2
- package/strategies/examples/emotion_cycle.yaml +5 -0
- package/strategies/examples/ma_crossover.yaml +10 -3
- package/strategies/examples/rsi_oversold.yaml +5 -0
- package/strategies/examples/shrink_pullback.yaml +7 -1
- package/strategies/examples/volume_breakout.yaml +7 -1
- package/templates/stock_analysis/brief.md.j2 +4 -1
- package/templates/stock_analysis/full.md.j2 +6 -3
- package/tools/_cache.py +61 -0
- package/tools/_subproc.py +49 -0
- package/tools/alert_rules.py +51 -13
- package/tools/anomaly_detect.py +24 -35
- package/tools/backtest.py +202 -104
- package/tools/capabilities.py +16 -5
- package/tools/diagnostics.py +12 -6
- package/tools/gather.py +33 -19
- package/tools/import_parser.py +2 -5
- package/tools/lixinger.py +100 -0
- package/tools/market_regime.py +34 -38
- package/tools/market_review.py +57 -9
- package/tools/name_resolver.py +2 -5
- package/tools/pattern.py +2 -5
- package/tools/position_context.py +22 -7
- package/tools/report_renderer.py +12 -6
- package/tools/requirements.txt +2 -1
- package/tools/risk_screening.py +73 -39
- package/tools/screener.py +90 -20
- package/tools/search_intel.py +5 -13
- package/tools/sec_edgar.py +346 -0
- package/tools/sec_holders.py +263 -0
- package/tools/signal_tracker.py +144 -25
- package/tools/stock_data.py +600 -186
- package/tools/technical.py +152 -42
- package/tools/trading_calendar.py +78 -18
- package/tools/value_data.py +1527 -0
- package/tools/volume_analysis.py +2 -5
- package/tools/watchlist.py +26 -6
- package/tools/watchlist_context.py +9 -8
package/tools/backtest.py
CHANGED
|
@@ -6,7 +6,6 @@ import json
|
|
|
6
6
|
import math
|
|
7
7
|
import os
|
|
8
8
|
import re
|
|
9
|
-
import subprocess
|
|
10
9
|
import sys
|
|
11
10
|
from datetime import datetime, timedelta
|
|
12
11
|
|
|
@@ -14,6 +13,12 @@ import numpy as np
|
|
|
14
13
|
import pandas as pd
|
|
15
14
|
import yaml
|
|
16
15
|
|
|
16
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
17
|
+
|
|
18
|
+
from _subproc import json_safe, run_tool, utf8_stdio
|
|
19
|
+
from stock_data import detect_market
|
|
20
|
+
from technical import bollinger_series, ema_series, ma_series, macd_series, rsi_series
|
|
21
|
+
|
|
17
22
|
# --------------- Strategy Loading ---------------
|
|
18
23
|
|
|
19
24
|
|
|
@@ -28,6 +33,8 @@ def load_strategy(path: str) -> dict:
|
|
|
28
33
|
def _resolve_refs(obj, params: dict):
|
|
29
34
|
if isinstance(obj, dict):
|
|
30
35
|
for k, v in obj.items():
|
|
36
|
+
if k == "benchmark":
|
|
37
|
+
continue # benchmark is a symbol string ("000300"), never a {param} reference
|
|
31
38
|
if isinstance(v, str):
|
|
32
39
|
obj[k] = _substitute(v, params)
|
|
33
40
|
else:
|
|
@@ -57,20 +64,17 @@ def _substitute(s: str, params: dict):
|
|
|
57
64
|
|
|
58
65
|
|
|
59
66
|
def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame:
|
|
60
|
-
script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stock_data.py")
|
|
61
67
|
count = 500
|
|
62
68
|
if start and end:
|
|
63
69
|
d0 = datetime.strptime(start, "%Y-%m-%d")
|
|
64
70
|
d1 = datetime.strptime(end, "%Y-%m-%d")
|
|
65
|
-
count = max((d1 - d0).days + 60, 300)
|
|
71
|
+
count = max((datetime.now() - d0).days + 60, (d1 - d0).days + 60, 300)
|
|
66
72
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
)
|
|
73
|
-
data = json.loads(r.stdout)
|
|
73
|
+
# run_tool gives venv-python + a bounded timeout + None on any failure
|
|
74
|
+
# (non-zero exit, empty/invalid stdout); callers turn that into clean JSON.
|
|
75
|
+
data = run_tool("stock_data.py", ["kline", symbol, "--period", "daily", "--count", str(count)])
|
|
76
|
+
if data is None:
|
|
77
|
+
raise RuntimeError(f"Failed to fetch kline for {symbol}")
|
|
74
78
|
if isinstance(data, dict) and "error" in data:
|
|
75
79
|
raise RuntimeError(data["error"])
|
|
76
80
|
|
|
@@ -88,39 +92,37 @@ def fetch_kline(symbol: str, start: str | None, end: str | None) -> pd.DataFrame
|
|
|
88
92
|
return df.reset_index(drop=True)
|
|
89
93
|
|
|
90
94
|
|
|
91
|
-
# --------------- Indicators
|
|
95
|
+
# --------------- Indicators ---------------
|
|
96
|
+
#
|
|
97
|
+
# Series math lives in technical.py (ma_series/ema_series/macd_series/rsi_series/
|
|
98
|
+
# bollinger_series); the wrappers below only add backtest-specific NaN defaults
|
|
99
|
+
# (rsi → 50, bollinger position → 0.5) so strategy evaluation never sees NaN.
|
|
92
100
|
|
|
93
101
|
|
|
94
102
|
def compute_rsi(close: pd.Series, period: int = 14) -> pd.Series:
|
|
95
|
-
|
|
96
|
-
gain = delta.where(delta > 0, 0.0).rolling(period).mean()
|
|
97
|
-
loss = (-delta.where(delta < 0, 0.0)).rolling(period).mean()
|
|
98
|
-
rs = gain / loss.replace(0, np.nan)
|
|
99
|
-
return (100 - 100 / (1 + rs)).fillna(50)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def compute_ma(close: pd.Series, period: int) -> pd.Series:
|
|
103
|
-
return close.rolling(period).mean()
|
|
103
|
+
return rsi_series(close, period).fillna(50)
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
# Pure aliases (identical signatures); the other wrappers below add
|
|
107
|
+
# backtest-specific NaN defaults or pick one leg of the MACD tuple.
|
|
108
|
+
compute_ma = ma_series
|
|
109
|
+
compute_ema = ema_series
|
|
108
110
|
|
|
109
111
|
|
|
110
112
|
def compute_macd_dif(close: pd.Series) -> pd.Series:
|
|
111
|
-
return
|
|
113
|
+
return macd_series(close)[0]
|
|
112
114
|
|
|
113
115
|
|
|
114
116
|
def compute_macd_dea(close: pd.Series) -> pd.Series:
|
|
115
|
-
return
|
|
117
|
+
return macd_series(close)[1]
|
|
116
118
|
|
|
117
119
|
|
|
118
120
|
def compute_macd_hist(close: pd.Series) -> pd.Series:
|
|
119
|
-
return
|
|
121
|
+
return macd_series(close)[2]
|
|
120
122
|
|
|
121
123
|
|
|
122
124
|
def compute_volume_ratio(volume: pd.Series, period: int = 5) -> pd.Series:
|
|
123
|
-
ma = volume
|
|
125
|
+
ma = ma_series(volume, period)
|
|
124
126
|
return (volume / ma.replace(0, np.nan)).fillna(1)
|
|
125
127
|
|
|
126
128
|
|
|
@@ -129,40 +131,40 @@ def compute_price_change(close: pd.Series) -> pd.Series:
|
|
|
129
131
|
|
|
130
132
|
|
|
131
133
|
def compute_bollinger_position(close: pd.Series, period: int = 20) -> pd.Series:
|
|
132
|
-
mid = close
|
|
133
|
-
std = close.rolling(period).std()
|
|
134
|
-
upper = mid + 2 * std
|
|
135
|
-
lower = mid - 2 * std
|
|
134
|
+
upper, mid, lower = bollinger_series(close, period)
|
|
136
135
|
width = (upper - lower).replace(0, np.nan)
|
|
137
136
|
return ((close - lower) / width).fillna(0.5)
|
|
138
137
|
|
|
139
138
|
|
|
140
|
-
def
|
|
141
|
-
"""
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
slow = period % 100
|
|
139
|
+
def _resolve_ma_pair(fast: int | None, slow: int | None, period: int | None, default: tuple) -> tuple:
|
|
140
|
+
"""Explicit fast/slow periods win; a legacy encoded `period` (510 = MA5-MA10,
|
|
141
|
+
500 = MA5-MA20 via the slow==0 → fast*4 fallback) still decodes for old YAMLs."""
|
|
142
|
+
if fast is None and slow is None and period and period >= 100:
|
|
143
|
+
fast, slow = period // 100, period % 100
|
|
145
144
|
if slow == 0:
|
|
146
145
|
slow = fast * 4
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
return fast, slow
|
|
147
|
+
return fast or default[0], slow or default[1]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def compute_ma_diff(
|
|
151
|
+
close: pd.Series, period: int | None = None, fast: int | None = None, slow: int | None = None
|
|
152
|
+
) -> pd.Series:
|
|
153
|
+
"""Difference between two MAs. Prefer explicit fast/slow; `period` keeps the legacy encoding."""
|
|
154
|
+
fast, slow = _resolve_ma_pair(fast, slow, period, (5, 20))
|
|
150
155
|
return compute_ma(close, fast) - compute_ma(close, slow)
|
|
151
156
|
|
|
152
157
|
|
|
153
|
-
def compute_ema_diff(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if slow == 0:
|
|
158
|
-
slow = fast * 4
|
|
159
|
-
else:
|
|
160
|
-
fast = 12
|
|
161
|
-
slow = 26
|
|
158
|
+
def compute_ema_diff(
|
|
159
|
+
close: pd.Series, period: int | None = None, fast: int | None = None, slow: int | None = None
|
|
160
|
+
) -> pd.Series:
|
|
161
|
+
fast, slow = _resolve_ma_pair(fast, slow, period, (12, 26))
|
|
162
162
|
return compute_ema(close, fast) - compute_ema(close, slow)
|
|
163
163
|
|
|
164
164
|
|
|
165
|
-
def get_indicator(
|
|
165
|
+
def get_indicator(
|
|
166
|
+
df: pd.DataFrame, name: str, period: int | None = None, fast: int | None = None, slow: int | None = None
|
|
167
|
+
) -> pd.Series:
|
|
166
168
|
close = df["close"]
|
|
167
169
|
dispatch = {
|
|
168
170
|
"rsi": lambda: compute_rsi(close, period or 14),
|
|
@@ -171,8 +173,8 @@ def get_indicator(df: pd.DataFrame, name: str, period: int | None = None) -> pd.
|
|
|
171
173
|
"macd_dif": lambda: compute_macd_dif(close),
|
|
172
174
|
"macd_dea": lambda: compute_macd_dea(close),
|
|
173
175
|
"macd": lambda: compute_macd_hist(close),
|
|
174
|
-
"ma_diff": lambda: compute_ma_diff(close, period),
|
|
175
|
-
"ema_diff": lambda: compute_ema_diff(close, period),
|
|
176
|
+
"ma_diff": lambda: compute_ma_diff(close, period, fast, slow),
|
|
177
|
+
"ema_diff": lambda: compute_ema_diff(close, period, fast, slow),
|
|
176
178
|
"volume_ratio": lambda: compute_volume_ratio(df["volume"], period or 5),
|
|
177
179
|
"price_change": lambda: compute_price_change(close),
|
|
178
180
|
"bollinger_position": lambda: compute_bollinger_position(close, period or 20),
|
|
@@ -209,21 +211,27 @@ def check_condition(series: pd.Series, operator: str, value: float, idx: int) ->
|
|
|
209
211
|
return False
|
|
210
212
|
|
|
211
213
|
|
|
214
|
+
def _coerce_int(v):
|
|
215
|
+
if isinstance(v, str):
|
|
216
|
+
try:
|
|
217
|
+
return int(float(v))
|
|
218
|
+
except (ValueError, TypeError):
|
|
219
|
+
return None
|
|
220
|
+
return v
|
|
221
|
+
|
|
222
|
+
|
|
212
223
|
def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int, cache: dict) -> tuple[bool, str]:
|
|
213
224
|
results = []
|
|
214
225
|
reasons = []
|
|
215
226
|
for cond in conditions:
|
|
216
227
|
ind_name = cond["indicator"]
|
|
217
|
-
period = cond.get("period")
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
period = None
|
|
223
|
-
|
|
224
|
-
cache_key = f"{ind_name}_{period}"
|
|
228
|
+
period = _coerce_int(cond.get("period"))
|
|
229
|
+
fast = _coerce_int(cond.get("fast_period"))
|
|
230
|
+
slow = _coerce_int(cond.get("slow_period"))
|
|
231
|
+
|
|
232
|
+
cache_key = f"{ind_name}_{period}_{fast}_{slow}"
|
|
225
233
|
if cache_key not in cache:
|
|
226
|
-
cache[cache_key] = get_indicator(df, ind_name, period)
|
|
234
|
+
cache[cache_key] = get_indicator(df, ind_name, period, fast, slow)
|
|
227
235
|
series = cache[cache_key]
|
|
228
236
|
|
|
229
237
|
op = cond["operator"]
|
|
@@ -232,7 +240,7 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
|
|
|
232
240
|
results.append(hit)
|
|
233
241
|
if hit:
|
|
234
242
|
cur_val = round(float(series.iloc[idx]), 2)
|
|
235
|
-
label = f"{ind_name}({period})" if period else ind_name
|
|
243
|
+
label = f"{ind_name}({fast},{slow})" if fast or slow else f"{ind_name}({period})" if period else ind_name
|
|
236
244
|
reasons.append(f"{label} = {cur_val} {op} {val}")
|
|
237
245
|
|
|
238
246
|
if logic == "all":
|
|
@@ -244,18 +252,33 @@ def evaluate_conditions(df: pd.DataFrame, conditions: list, logic: str, idx: int
|
|
|
244
252
|
|
|
245
253
|
|
|
246
254
|
def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> dict:
|
|
247
|
-
|
|
255
|
+
if not math.isfinite(capital) or capital <= 0:
|
|
256
|
+
raise ValueError("capital must be finite and positive")
|
|
257
|
+
if not np.isfinite(df["close"]).all() or (df["close"] <= 0).any():
|
|
258
|
+
raise ValueError("backtest prices must be finite and positive")
|
|
259
|
+
market = detect_market(symbol)
|
|
248
260
|
lot_size = 100 if market == "A" else 1
|
|
249
261
|
sim_cfg = strategy.get("simulation", {})
|
|
250
262
|
slippage = float(sim_cfg.get("slippage", 0.001))
|
|
251
263
|
commission = float(sim_cfg.get("commission", 0.0015))
|
|
264
|
+
tax_raw = sim_cfg.get("stamp_tax") # sell-side only (e.g. A股印花税); absent/empty = 0 (disabled)
|
|
265
|
+
stamp_tax = float(0.0 if tax_raw is None else tax_raw)
|
|
266
|
+
for name, rate in (("slippage", slippage), ("commission", commission), ("stamp_tax", stamp_tax)):
|
|
267
|
+
if not 0 <= rate < 1:
|
|
268
|
+
raise ValueError(f"simulation.{name} must be a rate in [0, 1), got {rate}")
|
|
269
|
+
if commission + stamp_tax >= 1:
|
|
270
|
+
raise ValueError(f"simulation commission + stamp_tax must be < 1, got {commission + stamp_tax}")
|
|
252
271
|
|
|
253
272
|
entry = strategy.get("entry", {})
|
|
254
273
|
exit_ = strategy.get("exit", {})
|
|
255
274
|
position_cfg = strategy.get("position", {})
|
|
256
275
|
size_frac = float(position_cfg.get("size", 1.0))
|
|
276
|
+
if not 0 <= size_frac <= 1:
|
|
277
|
+
raise ValueError("position.size must be a fraction in [0, 1]")
|
|
257
278
|
stop_loss = float(exit_.get("stop_loss", -0.10))
|
|
258
279
|
take_profit = float(exit_.get("take_profit", 0.50))
|
|
280
|
+
ts_val = exit_.get("trailing_stop")
|
|
281
|
+
trailing_stop = float(ts_val) if ts_val is not None else None
|
|
259
282
|
|
|
260
283
|
entry_conditions = entry.get("conditions", [])
|
|
261
284
|
entry_logic = entry.get("logic", "all")
|
|
@@ -268,6 +291,8 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
268
291
|
holding = None
|
|
269
292
|
cash = capital
|
|
270
293
|
trade_id = 0
|
|
294
|
+
commission_paid = 0.0
|
|
295
|
+
stamp_tax_paid = 0.0
|
|
271
296
|
|
|
272
297
|
for i in range(1, len(df)):
|
|
273
298
|
row = df.iloc[i]
|
|
@@ -279,10 +304,11 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
279
304
|
if hit:
|
|
280
305
|
buy_price = price * (1 + slippage)
|
|
281
306
|
buy_amount = cash * size_frac
|
|
282
|
-
shares = int(buy_amount / buy_price / lot_size) * lot_size
|
|
307
|
+
shares = int(buy_amount / (buy_price * (1 + commission)) / lot_size) * lot_size
|
|
283
308
|
if shares <= 0:
|
|
284
309
|
continue
|
|
285
310
|
cost = shares * buy_price * (1 + commission)
|
|
311
|
+
commission_paid += shares * buy_price * commission
|
|
286
312
|
cash -= cost
|
|
287
313
|
trade_id += 1
|
|
288
314
|
holding = {
|
|
@@ -291,6 +317,7 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
291
317
|
"buy_price": round(buy_price, 4),
|
|
292
318
|
"shares": shares,
|
|
293
319
|
"cost": cost,
|
|
320
|
+
"peak_close": price,
|
|
294
321
|
}
|
|
295
322
|
trades.append(
|
|
296
323
|
{
|
|
@@ -305,11 +332,19 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
305
332
|
)
|
|
306
333
|
else:
|
|
307
334
|
pnl_pct = (price - holding["buy_price"]) / holding["buy_price"]
|
|
335
|
+
peak_close = max(holding["peak_close"], price)
|
|
336
|
+
holding["peak_close"] = peak_close
|
|
308
337
|
sell = False
|
|
309
338
|
reason = ""
|
|
310
339
|
|
|
311
340
|
if pnl_pct <= stop_loss:
|
|
312
341
|
sell, reason = True, f"止损触发 ({round(pnl_pct * 100, 1)}% <= {round(stop_loss * 100, 1)}%)"
|
|
342
|
+
elif trailing_stop is not None and price <= peak_close * (1 - trailing_stop):
|
|
343
|
+
drawdown = (peak_close - price) / peak_close
|
|
344
|
+
sell, reason = (
|
|
345
|
+
True,
|
|
346
|
+
f"移动止损触发 (较高点回撤 {round(drawdown * 100, 1)}% >= {round(trailing_stop * 100, 1)}%)",
|
|
347
|
+
)
|
|
313
348
|
elif pnl_pct >= take_profit:
|
|
314
349
|
sell, reason = True, f"止盈触发 ({round(pnl_pct * 100, 1)}% >= {round(take_profit * 100, 1)}%)"
|
|
315
350
|
elif exit_conditions:
|
|
@@ -317,7 +352,10 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
317
352
|
|
|
318
353
|
if sell:
|
|
319
354
|
sell_price = price * (1 - slippage)
|
|
320
|
-
|
|
355
|
+
gross = holding["shares"] * sell_price
|
|
356
|
+
proceeds = gross * (1 - commission - stamp_tax)
|
|
357
|
+
commission_paid += gross * commission
|
|
358
|
+
stamp_tax_paid += gross * stamp_tax
|
|
321
359
|
cash += proceeds
|
|
322
360
|
pnl = proceeds - holding["cost"]
|
|
323
361
|
days = 0
|
|
@@ -358,6 +396,13 @@ def simulate(df: pd.DataFrame, strategy: dict, capital: float, symbol: str) -> d
|
|
|
358
396
|
"trades": trades,
|
|
359
397
|
"equity_curve": equity_curve,
|
|
360
398
|
"final_equity": equity_curve[-1]["equity"] if equity_curve else capital,
|
|
399
|
+
"costs": {
|
|
400
|
+
"slippage": slippage,
|
|
401
|
+
"commission": commission,
|
|
402
|
+
"stamp_tax": stamp_tax,
|
|
403
|
+
"commission_paid": round(commission_paid, 2),
|
|
404
|
+
"stamp_tax_paid": round(stamp_tax_paid, 2),
|
|
405
|
+
},
|
|
361
406
|
}
|
|
362
407
|
|
|
363
408
|
|
|
@@ -379,7 +424,7 @@ def compute_metrics(trades: list, equity_curve: list, initial: float, final: flo
|
|
|
379
424
|
years = 1
|
|
380
425
|
annual_return = (1 + total_return) ** (1 / max(years, 0.01)) - 1 if total_return > -1 else total_return
|
|
381
426
|
|
|
382
|
-
equities = pd.Series([e["equity"] for e in equity_curve], dtype=float)
|
|
427
|
+
equities = pd.Series([initial, *[e["equity"] for e in equity_curve]], dtype=float)
|
|
383
428
|
peak = equities.cummax()
|
|
384
429
|
dd = (equities - peak) / peak
|
|
385
430
|
max_drawdown = float(dd.min()) if len(dd) > 0 else 0
|
|
@@ -486,7 +531,7 @@ def diagnose_advanced(metrics: dict, trades: list, equity_curve: list, strategy:
|
|
|
486
531
|
stop_hits = sum(
|
|
487
532
|
1
|
|
488
533
|
for t in sell_trades
|
|
489
|
-
if (t.get("reason") or "").startswith("stop_loss")
|
|
534
|
+
if (t.get("reason") or "").startswith(("stop_loss", "止损触发", "移动止损触发"))
|
|
490
535
|
or (t.get("pnl_pct", 0) is not None and t.get("pnl_pct", 0) <= stop_loss * 100 + 0.5)
|
|
491
536
|
)
|
|
492
537
|
stop_ratio = stop_hits / max(1, len(sell_trades))
|
|
@@ -586,6 +631,23 @@ def sample_curve(curve: list, max_points: int = 200) -> list:
|
|
|
586
631
|
# --------------- Entry Points ---------------
|
|
587
632
|
|
|
588
633
|
|
|
634
|
+
def _benchmark_comparison(symbol: str, start: str, end: str, strategy_return: float) -> dict:
|
|
635
|
+
"""Buy-and-hold benchmark over the same window; degrades to an error note on fetch failure."""
|
|
636
|
+
try:
|
|
637
|
+
df = fetch_kline(symbol, start, end)
|
|
638
|
+
except Exception as e:
|
|
639
|
+
return {"symbol": symbol, "error": str(e)}
|
|
640
|
+
if len(df) < 2:
|
|
641
|
+
return {"symbol": symbol, "error": f"Insufficient benchmark data: {len(df)} rows"}
|
|
642
|
+
first, last = float(df["close"].iloc[0]), float(df["close"].iloc[-1])
|
|
643
|
+
total_return = (last - first) / first if first else 0.0
|
|
644
|
+
return {
|
|
645
|
+
"symbol": symbol,
|
|
646
|
+
"total_return": round(total_return, 4),
|
|
647
|
+
"excess_return": round(strategy_return - total_return, 4),
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
|
|
589
651
|
def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str | None, capital: float) -> dict:
|
|
590
652
|
strategy = load_strategy(strategy_path)
|
|
591
653
|
|
|
@@ -598,22 +660,36 @@ def run_backtest(strategy_path: str, symbol: str, start: str | None, end: str |
|
|
|
598
660
|
if len(df) < 30:
|
|
599
661
|
return {"error": f"Insufficient data: {len(df)} rows (need at least 30)"}
|
|
600
662
|
|
|
663
|
+
requested_period = f"{start} to {end}"
|
|
664
|
+
actual_start = df["date"].iloc[0].strftime("%Y-%m-%d")
|
|
665
|
+
actual_end = df["date"].iloc[-1].strftime("%Y-%m-%d")
|
|
666
|
+
warnings = []
|
|
667
|
+
if actual_start != start or actual_end != end:
|
|
668
|
+
warnings.append(f"Actual data covers {actual_start} to {actual_end}; requested {requested_period}")
|
|
669
|
+
start, end = actual_start, actual_end
|
|
601
670
|
sim = simulate(df, strategy, capital, symbol)
|
|
602
671
|
final = sim["final_equity"]
|
|
603
672
|
metrics = compute_metrics(sim["trades"], sim["equity_curve"], capital, final, start, end)
|
|
604
673
|
|
|
605
|
-
|
|
674
|
+
result = {
|
|
606
675
|
"strategy": strategy.get("name", "unnamed"),
|
|
607
676
|
"symbol": symbol,
|
|
608
677
|
"period": f"{start} to {end}",
|
|
678
|
+
"requested_period": requested_period,
|
|
679
|
+
"warnings": warnings,
|
|
609
680
|
"initial_capital": capital,
|
|
610
681
|
"final_capital": final,
|
|
682
|
+
"costs": sim["costs"],
|
|
611
683
|
"metrics": metrics,
|
|
612
684
|
"trades": sim["trades"],
|
|
613
685
|
"equity_curve": sample_curve(sim["equity_curve"]),
|
|
614
686
|
"diagnosis": diagnose(metrics),
|
|
615
687
|
"diagnostics": diagnose_advanced(metrics, sim["trades"], sim["equity_curve"], strategy),
|
|
616
688
|
}
|
|
689
|
+
benchmark_symbol = strategy.get("benchmark")
|
|
690
|
+
if benchmark_symbol:
|
|
691
|
+
result["benchmark"] = _benchmark_comparison(str(benchmark_symbol), start, end, metrics["total_return"])
|
|
692
|
+
return result
|
|
617
693
|
|
|
618
694
|
|
|
619
695
|
def evaluate_result(path: str) -> dict:
|
|
@@ -633,41 +709,59 @@ def evaluate_result(path: str) -> dict:
|
|
|
633
709
|
|
|
634
710
|
# --------------- Signal Evaluation ---------------
|
|
635
711
|
|
|
712
|
+
|
|
713
|
+
def _signal_context(df: pd.DataFrame) -> dict:
|
|
714
|
+
"""Precompute every indicator series the signal checkers need, once per
|
|
715
|
+
evaluate_signal call (the old lambdas recomputed them per bar — O(n²))."""
|
|
716
|
+
close = df["close"]
|
|
717
|
+
return {
|
|
718
|
+
"dif": compute_macd_dif(close),
|
|
719
|
+
"dea": compute_macd_dea(close),
|
|
720
|
+
"rsi14": compute_rsi(close, 14),
|
|
721
|
+
"ma5": compute_ma(close, 5),
|
|
722
|
+
"ma20": compute_ma(close, 20),
|
|
723
|
+
"close": close,
|
|
724
|
+
"high": df["high"],
|
|
725
|
+
"low": df["low"],
|
|
726
|
+
"volume": df["volume"],
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
|
|
636
730
|
SIGNAL_DEFINITIONS = {
|
|
637
|
-
"macd_golden_cross": lambda
|
|
731
|
+
"macd_golden_cross": lambda c, i: (
|
|
638
732
|
i >= 1
|
|
639
|
-
and float(
|
|
640
|
-
and float(
|
|
733
|
+
and float(c["dif"].iloc[i]) > float(c["dea"].iloc[i])
|
|
734
|
+
and float(c["dif"].iloc[i - 1]) <= float(c["dea"].iloc[i - 1])
|
|
641
735
|
),
|
|
642
|
-
"macd_death_cross": lambda
|
|
736
|
+
"macd_death_cross": lambda c, i: (
|
|
643
737
|
i >= 1
|
|
644
|
-
and float(
|
|
645
|
-
and float(
|
|
738
|
+
and float(c["dif"].iloc[i]) < float(c["dea"].iloc[i])
|
|
739
|
+
and float(c["dif"].iloc[i - 1]) >= float(c["dea"].iloc[i - 1])
|
|
646
740
|
),
|
|
647
|
-
"rsi_oversold": lambda
|
|
648
|
-
"rsi_overbought": lambda
|
|
649
|
-
"breakout_20d": lambda
|
|
741
|
+
"rsi_oversold": lambda c, i: float(c["rsi14"].iloc[i]) < 30,
|
|
742
|
+
"rsi_overbought": lambda c, i: float(c["rsi14"].iloc[i]) > 70,
|
|
743
|
+
"breakout_20d": lambda c, i: (
|
|
650
744
|
i >= 20
|
|
651
|
-
and float(
|
|
652
|
-
and float(
|
|
745
|
+
and float(c["close"].iloc[i]) > float(c["high"].iloc[i - 20 : i].max())
|
|
746
|
+
and float(c["close"].iloc[i - 1]) <= float(c["high"].iloc[i - 20 : i].max())
|
|
653
747
|
),
|
|
654
|
-
"breakdown_20d": lambda
|
|
748
|
+
"breakdown_20d": lambda c, i: (
|
|
655
749
|
i >= 20
|
|
656
|
-
and float(
|
|
657
|
-
and float(
|
|
750
|
+
and float(c["close"].iloc[i]) < float(c["low"].iloc[i - 20 : i].min())
|
|
751
|
+
and float(c["close"].iloc[i - 1]) >= float(c["low"].iloc[i - 20 : i].min())
|
|
658
752
|
),
|
|
659
|
-
"volume_surge": lambda
|
|
660
|
-
i >= 20 and float(
|
|
753
|
+
"volume_surge": lambda c, i: (
|
|
754
|
+
i >= 20 and float(c["volume"].iloc[i]) > 2.0 * float(c["volume"].iloc[i - 20 : i].mean())
|
|
661
755
|
),
|
|
662
|
-
"ma_golden_cross": lambda
|
|
756
|
+
"ma_golden_cross": lambda c, i: (
|
|
663
757
|
i >= 20
|
|
664
|
-
and float(
|
|
665
|
-
and float(
|
|
758
|
+
and float(c["ma5"].iloc[i]) > float(c["ma20"].iloc[i])
|
|
759
|
+
and float(c["ma5"].iloc[i - 1]) <= float(c["ma20"].iloc[i - 1])
|
|
666
760
|
),
|
|
667
|
-
"ma_death_cross": lambda
|
|
761
|
+
"ma_death_cross": lambda c, i: (
|
|
668
762
|
i >= 20
|
|
669
|
-
and float(
|
|
670
|
-
and float(
|
|
763
|
+
and float(c["ma5"].iloc[i]) < float(c["ma20"].iloc[i])
|
|
764
|
+
and float(c["ma5"].iloc[i - 1]) >= float(c["ma20"].iloc[i - 1])
|
|
671
765
|
),
|
|
672
766
|
}
|
|
673
767
|
|
|
@@ -686,10 +780,11 @@ def evaluate_signal(symbol: str, signal_name: str, forward_days: list[int] = Non
|
|
|
686
780
|
return {"error": f"Insufficient data: {len(df)} rows"}
|
|
687
781
|
|
|
688
782
|
max_fwd = max(forward_days)
|
|
783
|
+
ctx = _signal_context(df)
|
|
689
784
|
occurrences = []
|
|
690
785
|
for i in range(20, len(df) - max_fwd):
|
|
691
786
|
try:
|
|
692
|
-
if checker(
|
|
787
|
+
if checker(ctx, i):
|
|
693
788
|
entry_price = float(df["close"].iloc[i])
|
|
694
789
|
date_str = (
|
|
695
790
|
str(df["date"].iloc[i].date()) if hasattr(df["date"].iloc[i], "date") else str(df["date"].iloc[i])
|
|
@@ -742,23 +837,26 @@ def main():
|
|
|
742
837
|
parser.print_help()
|
|
743
838
|
sys.exit(1)
|
|
744
839
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
840
|
+
try:
|
|
841
|
+
if args.command == "run":
|
|
842
|
+
result = run_backtest(args.strategy, args.symbol, args.start, args.end, args.capital)
|
|
843
|
+
elif args.command == "evaluate":
|
|
844
|
+
result = evaluate_result(args.result)
|
|
845
|
+
elif args.command == "evaluate_signal":
|
|
846
|
+
fwd = [int(x) for x in args.forward.split(",")]
|
|
847
|
+
result = evaluate_signal(args.symbol, args.signal, fwd, args.lookback)
|
|
848
|
+
else:
|
|
849
|
+
result = {"error": f"Unknown command: {args.command}"}
|
|
850
|
+
except Exception as e:
|
|
851
|
+
# A traceback would leave stdout empty; hosts need valid JSON even on failure.
|
|
852
|
+
print(json.dumps({"error": str(e)}, ensure_ascii=False))
|
|
853
|
+
sys.exit(1)
|
|
754
854
|
|
|
755
|
-
print(json.dumps(result, ensure_ascii=False, default=str))
|
|
855
|
+
print(json.dumps(json_safe(result), ensure_ascii=False, default=str))
|
|
856
|
+
if "error" in result:
|
|
857
|
+
sys.exit(1)
|
|
756
858
|
|
|
757
859
|
|
|
758
860
|
if __name__ == "__main__":
|
|
759
|
-
|
|
760
|
-
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
761
|
-
for _s in (sys.stdout, sys.stderr):
|
|
762
|
-
if hasattr(_s, "reconfigure"):
|
|
763
|
-
_s.reconfigure(encoding="utf-8")
|
|
861
|
+
utf8_stdio()
|
|
764
862
|
main()
|
package/tools/capabilities.py
CHANGED
|
@@ -11,12 +11,25 @@ from datetime import datetime, timezone
|
|
|
11
11
|
from pathlib import Path
|
|
12
12
|
|
|
13
13
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
14
|
+
from _subproc import utf8_stdio
|
|
14
15
|
from stock_data import detect_market
|
|
15
16
|
|
|
16
17
|
ALL_MARKETS = ["A", "HK", "US", "JP", "KR", "TW"]
|
|
17
18
|
|
|
18
19
|
# tool_name -> {markets: [supported markets], reason: str for unsupported}
|
|
19
20
|
TOOL_MATRIX = {
|
|
21
|
+
"get_announcements": {"markets": ["A"], "reason": "cninfo A-share disclosures only"},
|
|
22
|
+
"get_us_holdings": {"markets": ["US"], "reason": "SEC manager holdings or verified issuer-CUSIP reverse lookup"},
|
|
23
|
+
"get_insider_trades": {"markets": ["US"], "reason": "SEC issuer Form 4 only"},
|
|
24
|
+
"get_valuation_history": {"markets": ["A", "HK"], "reason": "A/HK historical valuation providers only"},
|
|
25
|
+
"get_index_valuation": {"markets": ["A"], "reason": "Chinese index valuation only"},
|
|
26
|
+
"get_earnings_calendar": {
|
|
27
|
+
"markets": ["A", "HK"],
|
|
28
|
+
"reason": "A-share disclosed calendar / HK single-symbol provider schedule",
|
|
29
|
+
},
|
|
30
|
+
"get_shareholder_structure": {"markets": ["A", "HK"], "reason": "A-share shareholder reports / HK southbound only"},
|
|
31
|
+
"get_etf_details": {"markets": ["A"], "reason": "A-share ETF portfolio/NAV/size only"},
|
|
32
|
+
"get_dividends": {"markets": ["A", "HK"], "reason": "A/HK dividend history and report-year ranking"},
|
|
20
33
|
"get_kline": {"markets": ALL_MARKETS, "reason": None},
|
|
21
34
|
"get_quote": {"markets": ALL_MARKETS, "reason": None},
|
|
22
35
|
"get_news": {"markets": ALL_MARKETS, "reason": None},
|
|
@@ -42,6 +55,8 @@ TOOL_MATRIX = {
|
|
|
42
55
|
"get_limit_up_pool": {"markets": ["A"], "reason": "A-share only"},
|
|
43
56
|
"get_dragon_tiger": {"markets": ["A"], "reason": "A-share only"},
|
|
44
57
|
"get_hot_stocks": {"markets": ["A"], "reason": "A-share only"},
|
|
58
|
+
"get_margin_trading": {"markets": ["A"], "reason": "A-share only"},
|
|
59
|
+
"get_northbound_flow": {"markets": ["A"], "reason": "A-share only"},
|
|
45
60
|
"get_social_sentiment": {
|
|
46
61
|
"markets": ["A", "HK", "US"],
|
|
47
62
|
"reason": "A-share uses Eastmoney/Xueqiu; HK/US require SENTIMENT_API_KEY",
|
|
@@ -109,9 +124,5 @@ def main():
|
|
|
109
124
|
|
|
110
125
|
|
|
111
126
|
if __name__ == "__main__":
|
|
112
|
-
|
|
113
|
-
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
114
|
-
for _s in (sys.stdout, sys.stderr):
|
|
115
|
-
if hasattr(_s, "reconfigure"):
|
|
116
|
-
_s.reconfigure(encoding="utf-8")
|
|
127
|
+
utf8_stdio()
|
|
117
128
|
main()
|
package/tools/diagnostics.py
CHANGED
|
@@ -17,12 +17,22 @@ from datetime import datetime, timezone
|
|
|
17
17
|
from pathlib import Path
|
|
18
18
|
|
|
19
19
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
20
|
+
from _subproc import utf8_stdio # noqa: E402
|
|
20
21
|
from capabilities import ALL_MARKETS # noqa: E402
|
|
21
22
|
|
|
22
23
|
# provider -> (import_name, required_env_vars, markets, note)
|
|
23
24
|
PROVIDERS = {
|
|
24
25
|
"akshare": ("akshare", [], ["A"], None),
|
|
25
|
-
|
|
26
|
+
# tushare is consumed as a pure-HTTP API (requests.post to api.tushare.pro,
|
|
27
|
+
# see stock_data._kline_tushare) — the tushare package is not in requirements,
|
|
28
|
+
# so availability is the token, not a package probe
|
|
29
|
+
"tushare": ("requests", ["TUSHARE_TOKEN"], ["A"], "pure-HTTP API (api.tushare.pro); no tushare package needed"),
|
|
30
|
+
"lixinger": (
|
|
31
|
+
"requests",
|
|
32
|
+
["LIXINGER_TOKEN"],
|
|
33
|
+
["A"],
|
|
34
|
+
"optional index PE/PB history; subscription permissions not probed",
|
|
35
|
+
),
|
|
26
36
|
"efinance": ("efinance", [], ["A"], None),
|
|
27
37
|
"tencent": ("requests", [], ["A"], "qt.gtimg.cn quote/kline, no auth (issue #25)"),
|
|
28
38
|
"sina": (
|
|
@@ -150,9 +160,5 @@ def main():
|
|
|
150
160
|
|
|
151
161
|
|
|
152
162
|
if __name__ == "__main__":
|
|
153
|
-
|
|
154
|
-
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
155
|
-
for _s in (sys.stdout, sys.stderr):
|
|
156
|
-
if hasattr(_s, "reconfigure"):
|
|
157
|
-
_s.reconfigure(encoding="utf-8")
|
|
163
|
+
utf8_stdio()
|
|
158
164
|
main()
|