search_eth_twap_robustness_10y.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. from __future__ import annotations
  2. import argparse
  3. import multiprocessing
  4. import sys
  5. from concurrent.futures import ProcessPoolExecutor, as_completed
  6. from pathlib import Path
  7. import pandas as pd
  8. ROOT = Path(__file__).resolve().parents[1]
  9. sys.path.insert(0, str(ROOT))
  10. from scripts.explore_ultrashort import (
  11. CANDLE_CACHE_DIR,
  12. LEVERAGE,
  13. _format_ts,
  14. annualized_metrics_from_equity,
  15. build_rsi2_long_guarded_price_twap_candidate,
  16. cost_adjusted_trade_equity_frame,
  17. history_bars_for_years,
  18. load_cached_candles,
  19. max_drawdown_from_equity,
  20. )
  21. SYMBOL = "ETH-USDT-SWAP"
  22. BAR = "15m"
  23. YEARS = 10.0
  24. MAX_HOLD_BARS = 48
  25. OUTPUT_DIR = Path("reports/eth-exploration")
  26. COSTS = {
  27. "maker_maker": 0.0012,
  28. "maker_taker": 0.0021,
  29. "taker_taker": 0.0030,
  30. }
  31. FILL_BUFFERS = (0.0, 0.0002, 0.0005)
  32. HORIZONS = (
  33. ("3y", pd.DateOffset(years=3)),
  34. ("1y", pd.DateOffset(years=1)),
  35. ("6m", pd.DateOffset(months=6)),
  36. ("3m", pd.DateOffset(months=3)),
  37. )
  38. CANDLES = None
  39. def base_candidate_specs() -> list[dict[str, object]]:
  40. return [
  41. {
  42. "candidate_id": "x45-v3",
  43. "trend_sma": 60,
  44. "rsi_threshold": 5.0,
  45. "exit_rsi": 45.0,
  46. "stop_loss_pct": 0.008,
  47. "max_hold_bars": MAX_HOLD_BARS,
  48. "entry_offsets": (0.0015, 0.004, 0.007),
  49. "entry_valid_bars": 3,
  50. },
  51. {
  52. "candidate_id": "x45-v4",
  53. "trend_sma": 60,
  54. "rsi_threshold": 5.0,
  55. "exit_rsi": 45.0,
  56. "stop_loss_pct": 0.008,
  57. "max_hold_bars": MAX_HOLD_BARS,
  58. "entry_offsets": (0.0015, 0.004, 0.007),
  59. "entry_valid_bars": 4,
  60. },
  61. {
  62. "candidate_id": "x55-v2",
  63. "trend_sma": 60,
  64. "rsi_threshold": 5.0,
  65. "exit_rsi": 55.0,
  66. "stop_loss_pct": 0.008,
  67. "max_hold_bars": MAX_HOLD_BARS,
  68. "entry_offsets": (0.0015, 0.004, 0.007),
  69. "entry_valid_bars": 2,
  70. },
  71. {
  72. "candidate_id": "x55-v3",
  73. "trend_sma": 60,
  74. "rsi_threshold": 5.0,
  75. "exit_rsi": 55.0,
  76. "stop_loss_pct": 0.008,
  77. "max_hold_bars": MAX_HOLD_BARS,
  78. "entry_offsets": (0.0015, 0.004, 0.007),
  79. "entry_valid_bars": 3,
  80. },
  81. {
  82. "candidate_id": "r3x50",
  83. "trend_sma": 60,
  84. "rsi_threshold": 3.0,
  85. "exit_rsi": 50.0,
  86. "stop_loss_pct": 0.012,
  87. "max_hold_bars": MAX_HOLD_BARS,
  88. "entry_offsets": (0.003, 0.006, 0.009),
  89. "entry_valid_bars": 4,
  90. },
  91. ]
  92. def candidate_specs() -> list[dict[str, object]]:
  93. specs: list[dict[str, object]] = []
  94. for base in base_candidate_specs():
  95. for fill_buffer in FILL_BUFFERS:
  96. specs.append({**base, "fill_buffer": fill_buffer})
  97. return specs
  98. def init_worker(candles: list[object]) -> None:
  99. global CANDLES
  100. CANDLES = candles
  101. def offset_label(entry_offsets: tuple[float, ...]) -> str:
  102. return "-".join(f"{value:.4f}" for value in entry_offsets)
  103. def markdown_table(frame: pd.DataFrame, columns: list[str]) -> str:
  104. rows = [["" if pd.isna(value) else str(value) for value in row] for row in frame[columns].itertuples(index=False, name=None)]
  105. return "\n".join(
  106. [
  107. "| " + " | ".join(columns) + " |",
  108. "| " + " | ".join("---" for _ in columns) + " |",
  109. *["| " + " | ".join(row) + " |" for row in rows],
  110. ]
  111. )
  112. def horizon_metrics(frame: pd.DataFrame, last_ts: int) -> list[dict[str, object]]:
  113. rows: list[dict[str, object]] = []
  114. end_time = pd.to_datetime(last_ts, unit="ms", utc=True)
  115. for label, offset in HORIZONS:
  116. cutoff = end_time - offset
  117. before_cutoff = frame[frame["ts"] <= cutoff]
  118. if len(before_cutoff):
  119. start_equity = float(before_cutoff["equity"].iloc[-1])
  120. start_time = cutoff
  121. horizon_frame = pd.concat(
  122. [
  123. pd.DataFrame([{"ts": start_time, "equity": start_equity}]),
  124. frame[frame["ts"] > cutoff][["ts", "equity"]],
  125. ],
  126. ignore_index=True,
  127. )
  128. else:
  129. horizon_frame = frame[["ts", "equity"]].copy()
  130. start_time = pd.Timestamp(horizon_frame["ts"].iloc[0])
  131. rows.append(
  132. {
  133. "horizon": label,
  134. "horizon_start": start_time.strftime("%Y-%m-%d %H:%M"),
  135. "horizon_end": end_time.strftime("%Y-%m-%d %H:%M"),
  136. **annualized_metrics_from_equity(
  137. horizon_frame,
  138. int(start_time.timestamp() * 1000),
  139. last_ts,
  140. ),
  141. }
  142. )
  143. return rows
  144. def rolling_window_stats(frame: pd.DataFrame, last_ts: int) -> list[dict[str, object]]:
  145. daily = frame.set_index("ts")["equity"].resample("1D").last().ffill().dropna()
  146. rows: list[dict[str, object]] = []
  147. for label, days in (("rolling_1y", 365), ("rolling_30d", 30)):
  148. windows: list[dict[str, object]] = []
  149. for end_index in range(days, len(daily)):
  150. window = daily.iloc[end_index - days : end_index + 1]
  151. total_return = float(window.iloc[-1] / window.iloc[0] - 1.0)
  152. max_drawdown = max_drawdown_from_equity([float(value) for value in window])
  153. years = days / 365.0
  154. annualized_return = (1.0 + total_return) ** (1.0 / years) - 1.0 if total_return > -1.0 else 0.0
  155. windows.append(
  156. {
  157. "window_start": window.index[0].strftime("%Y-%m-%d"),
  158. "window_end": window.index[-1].strftime("%Y-%m-%d"),
  159. "rolling_total_return": total_return,
  160. "rolling_annualized_return": annualized_return,
  161. "rolling_max_drawdown": max_drawdown,
  162. "rolling_calmar": annualized_return / max_drawdown if max_drawdown else 0.0,
  163. }
  164. )
  165. worst_return = min(windows, key=lambda row: row["rolling_total_return"])
  166. worst_drawdown = max(windows, key=lambda row: row["rolling_max_drawdown"])
  167. rows.append(
  168. {
  169. "window": label,
  170. "window_days": days,
  171. "sample_end": _format_ts(last_ts),
  172. "worst_return_start": worst_return["window_start"],
  173. "worst_return_end": worst_return["window_end"],
  174. "worst_rolling_total_return": worst_return["rolling_total_return"],
  175. "worst_rolling_annualized_return": worst_return["rolling_annualized_return"],
  176. "worst_return_window_max_drawdown": worst_return["rolling_max_drawdown"],
  177. "worst_drawdown_start": worst_drawdown["window_start"],
  178. "worst_drawdown_end": worst_drawdown["window_end"],
  179. "worst_rolling_max_drawdown": worst_drawdown["rolling_max_drawdown"],
  180. "worst_drawdown_window_total_return": worst_drawdown["rolling_total_return"],
  181. }
  182. )
  183. return rows
  184. def evaluate_spec(spec: dict[str, object]) -> tuple[list[dict[str, object]], list[dict[str, object]], list[dict[str, object]], str, int]:
  185. if CANDLES is None:
  186. raise RuntimeError("candles are not initialized")
  187. candles = CANDLES
  188. entry_offsets = tuple(float(value) for value in spec["entry_offsets"])
  189. candidate = build_rsi2_long_guarded_price_twap_candidate(
  190. int(spec["trend_sma"]),
  191. float(spec["rsi_threshold"]),
  192. float(spec["exit_rsi"]),
  193. float(spec["stop_loss_pct"]),
  194. int(spec["max_hold_bars"]),
  195. entry_offsets,
  196. int(spec["entry_valid_bars"]),
  197. float(spec["fill_buffer"]),
  198. )
  199. result = candidate.run(candles=candles, leverage=LEVERAGE, warmup_bars=candidate.warmup_bars)
  200. gross_years = (candles[-1].ts - candles[0].ts) / 86_400_000 / 365
  201. gross_annualized = (1.0 + result.total_return) ** (1.0 / gross_years) - 1.0 if result.total_return > -1.0 else 0.0
  202. total_rows: list[dict[str, object]] = []
  203. horizon_rows: list[dict[str, object]] = []
  204. rolling_rows: list[dict[str, object]] = []
  205. for cost_label, roundtrip_cost in COSTS.items():
  206. net_equity = cost_adjusted_trade_equity_frame(result, roundtrip_cost)
  207. base_row = {
  208. "symbol": SYMBOL,
  209. "bar": BAR,
  210. "cost_model": cost_label,
  211. "roundtrip_cost_on_margin": roundtrip_cost,
  212. "candidate_id": spec["candidate_id"],
  213. "name": candidate.name,
  214. "first_candle": _format_ts(candles[0].ts),
  215. "last_candle": _format_ts(candles[-1].ts),
  216. "actual_bars": len(candles),
  217. "trades": result.trade_count,
  218. "gross_total_return": result.total_return,
  219. "gross_annualized_return": gross_annualized,
  220. "gross_max_drawdown_mark_to_market": result.max_drawdown,
  221. "trend_sma": spec["trend_sma"],
  222. "rsi_threshold": spec["rsi_threshold"],
  223. "exit_rsi": spec["exit_rsi"],
  224. "stop_loss_pct": spec["stop_loss_pct"],
  225. "max_hold_bars": spec["max_hold_bars"],
  226. "entry_offsets": offset_label(entry_offsets),
  227. "entry_valid_bars": spec["entry_valid_bars"],
  228. "fill_buffer": spec["fill_buffer"],
  229. }
  230. total_rows.append({**base_row, **annualized_metrics_from_equity(net_equity, candles[0].ts, candles[-1].ts)})
  231. for horizon_row in horizon_metrics(net_equity, candles[-1].ts):
  232. horizon_rows.append({**base_row, **horizon_row})
  233. for rolling_row in rolling_window_stats(net_equity, candles[-1].ts):
  234. rolling_rows.append({**base_row, **rolling_row})
  235. return total_rows, horizon_rows, rolling_rows, candidate.name, result.trade_count
  236. def run_search(workers: int) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
  237. cached, _ = load_cached_candles(CANDLE_CACHE_DIR, SYMBOL, BAR)
  238. candles = cached[-history_bars_for_years(BAR, YEARS) :]
  239. specs = candidate_specs()
  240. total_rows: list[dict[str, object]] = []
  241. horizon_rows: list[dict[str, object]] = []
  242. rolling_rows: list[dict[str, object]] = []
  243. with ProcessPoolExecutor(max_workers=workers, mp_context=multiprocessing.get_context("fork"), initializer=init_worker, initargs=(candles,)) as executor:
  244. futures = [executor.submit(evaluate_spec, spec) for spec in specs]
  245. for index, future in enumerate(as_completed(futures), start=1):
  246. spec_totals, spec_horizons, spec_rolling, name, trade_count = future.result()
  247. total_rows.extend(spec_totals)
  248. horizon_rows.extend(spec_horizons)
  249. rolling_rows.extend(spec_rolling)
  250. print(f"{index}/{len(specs)} {name} trades={trade_count}", flush=True)
  251. totals = pd.DataFrame(total_rows)
  252. horizons = pd.DataFrame(horizon_rows)
  253. rolling = pd.DataFrame(rolling_rows)
  254. ranked = rank_candidates(totals, horizons, rolling)
  255. totals = totals.sort_values(["cost_model", "net_calmar", "net_annualized_return"], ascending=[True, False, False])
  256. horizons = horizons.sort_values(["cost_model", "horizon", "net_calmar", "net_annualized_return"], ascending=[True, True, False, False])
  257. rolling = rolling.sort_values(["cost_model", "window", "worst_rolling_total_return"], ascending=[True, True, False])
  258. return totals, horizons, rolling, ranked
  259. def rank_candidates(totals: pd.DataFrame, horizons: pd.DataFrame, rolling: pd.DataFrame) -> pd.DataFrame:
  260. key_columns = ["candidate_id", "name", "fill_buffer"]
  261. maker_taker_totals = totals[totals["cost_model"] == "maker_taker"].copy()
  262. horizon_pivot = horizons[horizons["cost_model"] == "maker_taker"].pivot_table(
  263. index=key_columns,
  264. columns="horizon",
  265. values=["net_annualized_return", "net_max_drawdown", "net_calmar"],
  266. aggfunc="first",
  267. observed=False,
  268. )
  269. horizon_pivot.columns = [f"{metric}_{horizon}" for metric, horizon in horizon_pivot.columns]
  270. rolling_pivot = rolling[rolling["cost_model"] == "maker_taker"].pivot_table(
  271. index=key_columns,
  272. columns="window",
  273. values=["worst_rolling_total_return", "worst_rolling_max_drawdown"],
  274. aggfunc="first",
  275. observed=False,
  276. )
  277. rolling_pivot.columns = [f"{metric}_{window}" for metric, window in rolling_pivot.columns]
  278. ranked = maker_taker_totals.merge(horizon_pivot.reset_index(), on=key_columns).merge(rolling_pivot.reset_index(), on=key_columns)
  279. ranked["min_recent_calmar"] = ranked[["net_calmar_1y", "net_calmar_6m", "net_calmar_3m"]].min(axis=1)
  280. ranked["min_recent_annualized"] = ranked[["net_annualized_return_1y", "net_annualized_return_6m", "net_annualized_return_3m"]].min(axis=1)
  281. ranked["max_recent_drawdown"] = ranked[["net_max_drawdown_1y", "net_max_drawdown_6m", "net_max_drawdown_3m"]].max(axis=1)
  282. ranked["rolling_1y_positive"] = ranked["worst_rolling_total_return_rolling_1y"] > 0.0
  283. all_costs = totals.pivot_table(
  284. index=key_columns,
  285. columns="cost_model",
  286. values="net_annualized_return",
  287. aggfunc="first",
  288. observed=False,
  289. )
  290. all_costs["all_cost_positive_10y"] = all_costs.min(axis=1) > 0.0
  291. ranked = ranked.merge(all_costs[["all_cost_positive_10y"]].reset_index(), on=key_columns)
  292. ranked = ranked.sort_values(
  293. [
  294. "rolling_1y_positive",
  295. "min_recent_calmar",
  296. "net_calmar_3y",
  297. "worst_rolling_total_return_rolling_1y",
  298. "net_annualized_return",
  299. ],
  300. ascending=False,
  301. )
  302. return ranked
  303. def markdown_summary(totals: pd.DataFrame, horizons: pd.DataFrame, rolling: pd.DataFrame, ranked: pd.DataFrame) -> str:
  304. top = ranked.head(8)
  305. best = top.iloc[0] if len(top) else None
  306. top_names = set(top["name"])
  307. top_buffers = set(zip(top["name"], top["fill_buffer"]))
  308. total_top = totals[(totals["cost_model"] == "maker_taker") & (totals["name"].isin(top_names))].sort_values(["name", "fill_buffer"])
  309. horizon_top = horizons[(horizons["cost_model"] == "maker_taker") & (horizons.apply(lambda row: (row["name"], row["fill_buffer"]) in top_buffers, axis=1))].sort_values(["name", "fill_buffer", "horizon"])
  310. rolling_top = rolling[(rolling["cost_model"] == "maker_taker") & (rolling.apply(lambda row: (row["name"], row["fill_buffer"]) in top_buffers, axis=1))].sort_values(["name", "fill_buffer", "window"])
  311. should_include = bool(
  312. best is not None
  313. and float(best["min_recent_calmar"]) > 0.0
  314. and float(best["worst_rolling_total_return_rolling_1y"]) > 0.0
  315. and bool(best["all_cost_positive_10y"])
  316. )
  317. decision = "建议纳入主报告" if should_include else "暂不建议纳入主报告"
  318. if best is None:
  319. decision_line = "没有候选完成排名。"
  320. else:
  321. decision_line = (
  322. f"{decision}: top 候选 `{best['name']}` fill_buffer={float(best['fill_buffer']):.4f}; "
  323. f"maker_taker 10y annualized={float(best['net_annualized_return']):.4f}, "
  324. f"10y maxDD={float(best['net_max_drawdown']):.4f}, "
  325. f"3y Calmar={float(best['net_calmar_3y']):.4f}, "
  326. f"min recent Calmar={float(best['min_recent_calmar']):.4f}, "
  327. f"worst rolling 1y return={float(best['worst_rolling_total_return_rolling_1y']):.4f}."
  328. )
  329. return "\n".join(
  330. [
  331. "# ETH TWAP robustness 10y",
  332. "",
  333. "Scope: targeted robustness review only. Base candidates are the ETH TWAP refine top candidates requested by parameter family; no broad grid search.",
  334. "",
  335. "Robustness dimensions: fill_buffer 0/0.0002/0.0005, three cost models, continuous 10y backtest sliced into 3y/1y/6m/3m, plus rolling 365D and 30D worst return/drawdown statistics.",
  336. "",
  337. f"Decision: {decision_line}",
  338. "",
  339. "## Top maker_taker robustness ranking",
  340. "",
  341. markdown_table(
  342. top,
  343. [
  344. "candidate_id",
  345. "name",
  346. "trades",
  347. "fill_buffer",
  348. "net_annualized_return",
  349. "net_max_drawdown",
  350. "net_calmar",
  351. "net_calmar_3y",
  352. "min_recent_calmar",
  353. "max_recent_drawdown",
  354. "worst_rolling_total_return_rolling_1y",
  355. "worst_rolling_max_drawdown_rolling_1y",
  356. "worst_rolling_total_return_rolling_30d",
  357. "worst_rolling_max_drawdown_rolling_30d",
  358. "rolling_1y_positive",
  359. "all_cost_positive_10y",
  360. ],
  361. ),
  362. "",
  363. "## Top maker_taker 10y totals",
  364. "",
  365. markdown_table(
  366. total_top,
  367. [
  368. "candidate_id",
  369. "name",
  370. "trades",
  371. "fill_buffer",
  372. "net_annualized_return",
  373. "net_max_drawdown",
  374. "net_calmar",
  375. "net_sharpe_daily",
  376. ],
  377. ),
  378. "",
  379. "## Top recent horizons",
  380. "",
  381. markdown_table(
  382. horizon_top,
  383. [
  384. "candidate_id",
  385. "name",
  386. "fill_buffer",
  387. "horizon",
  388. "net_total_return",
  389. "net_annualized_return",
  390. "net_max_drawdown",
  391. "net_calmar",
  392. ],
  393. ),
  394. "",
  395. "## Top rolling worst windows",
  396. "",
  397. markdown_table(
  398. rolling_top,
  399. [
  400. "candidate_id",
  401. "name",
  402. "fill_buffer",
  403. "window",
  404. "worst_return_start",
  405. "worst_return_end",
  406. "worst_rolling_total_return",
  407. "worst_return_window_max_drawdown",
  408. "worst_drawdown_start",
  409. "worst_drawdown_end",
  410. "worst_rolling_max_drawdown",
  411. ],
  412. ),
  413. "",
  414. ]
  415. )
  416. def main() -> int:
  417. parser = argparse.ArgumentParser()
  418. parser.add_argument("--workers", type=int, default=6)
  419. args = parser.parse_args()
  420. OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
  421. totals, horizons, rolling, ranked = run_search(args.workers)
  422. totals_path = OUTPUT_DIR / "eth-twap-robustness-10y-totals.csv"
  423. horizons_path = OUTPUT_DIR / "eth-twap-robustness-10y-horizons.csv"
  424. rolling_path = OUTPUT_DIR / "eth-twap-robustness-10y-rolling.csv"
  425. ranked_path = OUTPUT_DIR / "eth-twap-robustness-10y-ranked.csv"
  426. summary_path = OUTPUT_DIR / "eth-twap-robustness-10y-summary.md"
  427. totals.to_csv(totals_path, index=False)
  428. horizons.to_csv(horizons_path, index=False)
  429. rolling.to_csv(rolling_path, index=False)
  430. ranked.to_csv(ranked_path, index=False)
  431. summary_path.write_text(markdown_summary(totals, horizons, rolling, ranked), encoding="utf-8")
  432. print(f"wrote {totals_path}")
  433. print(f"wrote {horizons_path}")
  434. print(f"wrote {rolling_path}")
  435. print(f"wrote {ranked_path}")
  436. print(f"wrote {summary_path}")
  437. print(ranked.head(8).to_string(index=False))
  438. return 0
  439. if __name__ == "__main__":
  440. raise SystemExit(main())