search_eth_bearish_price_proxy.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. from __future__ import annotations
  2. import argparse
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import pandas as pd
  6. DATA_DIR = Path("data/okx-candles")
  7. OUTPUT_DIR = Path("reports/eth-exploration")
  8. SYMBOL = "ETH-USDT-SWAP"
  9. BTC_SYMBOL = "BTC-USDT-SWAP"
  10. INITIAL_EQUITY = 10_000.0
  11. FEE = 0.0004
  12. ROUNDTRIP_FEE = FEE * 2
  13. HORIZONS = (
  14. ("full", None),
  15. ("3y", pd.DateOffset(years=3)),
  16. ("1y", pd.DateOffset(years=1)),
  17. ("6m", pd.DateOffset(months=6)),
  18. ("3m", pd.DateOffset(months=3)),
  19. )
  20. @dataclass(frozen=True)
  21. class Spec:
  22. family: str
  23. bar: str
  24. fast: int
  25. slow: int
  26. lookback: int
  27. threshold: float
  28. stop: float
  29. take: float
  30. hold: int
  31. gate: str
  32. @property
  33. def name(self) -> str:
  34. return (
  35. f"{self.family}-{self.bar}-f{self.fast}-s{self.slow}-lb{self.lookback}"
  36. f"-th{self.threshold:g}-sl{self.stop:g}-tp{self.take:g}-h{self.hold}-{self.gate}"
  37. )
  38. def load_frame(symbol: str) -> pd.DataFrame:
  39. path = DATA_DIR / symbol / "15m.csv"
  40. frame = pd.read_csv(path)
  41. frame["ts"] = pd.to_datetime(frame["ts"], unit="ms", utc=True)
  42. return frame.sort_values("ts").drop_duplicates("ts", keep="last").set_index("ts")
  43. def resample(frame: pd.DataFrame, bar: str) -> pd.DataFrame:
  44. rule = {"15m": "15min", "1H": "1h", "4H": "4h"}[bar]
  45. return (
  46. frame.resample(rule, label="left", closed="left")
  47. .agg(open=("open", "first"), high=("high", "max"), low=("low", "min"), close=("close", "last"), volume=("volume", "sum"))
  48. .dropna()
  49. )
  50. def rsi(close: pd.Series, length: int) -> pd.Series:
  51. diff = close.diff()
  52. gain = diff.clip(lower=0).ewm(alpha=1 / length, adjust=False).mean()
  53. loss = (-diff.clip(upper=0)).ewm(alpha=1 / length, adjust=False).mean()
  54. return 100 - 100 / (1 + gain / loss)
  55. def joined_frames(eth: pd.DataFrame, btc: pd.DataFrame) -> pd.DataFrame:
  56. return eth.join(btc[["close"]].rename(columns={"close": "btc_close"}), how="inner")
  57. def risk_gate(frame: pd.DataFrame, gate: str) -> pd.Series:
  58. eth = frame["close"]
  59. btc = frame["btc_close"]
  60. if gate == "none":
  61. return pd.Series(True, index=frame.index)
  62. if gate == "btc_riskoff":
  63. btc_slow = btc.rolling(120).mean()
  64. btc_drop = btc / btc.shift(24) - 1
  65. return (btc < btc_slow) & (btc_drop < -0.015)
  66. if gate == "eth_riskoff":
  67. eth_slow = eth.rolling(160).mean()
  68. eth_drop = eth / eth.shift(24) - 1
  69. return (eth < eth_slow) & (eth_drop < -0.012)
  70. raise ValueError(gate)
  71. def signals(spec: Spec, frame: pd.DataFrame) -> tuple[pd.Series, pd.Series, pd.Series]:
  72. close = frame["close"]
  73. high = frame["high"]
  74. low = frame["low"]
  75. open_ = frame["open"]
  76. fast = close.ewm(span=spec.fast, adjust=False).mean()
  77. slow = close.ewm(span=spec.slow, adjust=False).mean()
  78. ret = close / close.shift(spec.lookback) - 1
  79. range_pct = (high - low) / close
  80. range_rank = range_pct.rolling(160).rank(pct=True)
  81. vol_rank = frame["volume"].rolling(160).rank(pct=True)
  82. rsi14 = rsi(close, 14)
  83. gate = risk_gate(frame, spec.gate)
  84. if spec.family == "crash_follow":
  85. entry = gate & (close < slow) & (ret < -spec.threshold) & (range_rank > 0.75)
  86. exit_ = (close > fast) | (rsi14 > 52)
  87. side = pd.Series("short", index=frame.index)
  88. elif spec.family == "rebound_exhaustion":
  89. prior_drop = close / close.shift(spec.lookback * 2) - 1
  90. rebound = close / close.shift(spec.lookback) - 1
  91. weak_close = close < open_
  92. entry = gate & (close < slow) & (prior_drop < -spec.threshold * 1.4) & (rebound > spec.threshold * 0.45) & (high >= fast) & weak_close
  93. exit_ = close > slow
  94. side = pd.Series("short", index=frame.index)
  95. elif spec.family == "candle_funding_proxy":
  96. premium_proxy = close / slow - 1
  97. failed_high = (high / close - 1) > range_pct.rolling(80).median()
  98. entry = gate & (premium_proxy > spec.threshold) & (rsi14 > 62) & (vol_rank > 0.65) & failed_high & (close < open_)
  99. exit_ = (close < fast) | (rsi14 < 45)
  100. side = pd.Series("short", index=frame.index)
  101. elif spec.family == "riskoff_bidir":
  102. breakdown = gate & (close < slow) & (ret < -spec.threshold)
  103. capitulation_rebound = (close > fast) & (rsi14 < 35) & (range_rank > 0.75)
  104. entry = breakdown | capitulation_rebound
  105. exit_ = close > fast
  106. side = pd.Series("short", index=frame.index)
  107. side = side.mask(capitulation_rebound, "long")
  108. else:
  109. raise ValueError(spec.family)
  110. return entry.fillna(False), exit_.fillna(False), side
  111. def close_return(side: str, entry: float, exit_: float) -> float:
  112. gross = exit_ / entry - 1 if side == "long" else entry / exit_ - 1
  113. return gross - ROUNDTRIP_FEE
  114. def run_spec(spec: Spec, frame: pd.DataFrame) -> tuple[pd.Series, list[dict[str, object]]]:
  115. entry, exit_, side_signal = signals(spec, frame)
  116. fast = frame["close"].ewm(span=spec.fast, adjust=False).mean()
  117. warmup = max(spec.slow, 180, spec.lookback * 2) + 2
  118. equity = INITIAL_EQUITY
  119. position: dict[str, object] | None = None
  120. pending_entry: str | None = None
  121. pending_exit = False
  122. trades: list[dict[str, object]] = []
  123. curve: list[tuple[pd.Timestamp, float]] = []
  124. rows = list(frame.itertuples())
  125. for index in range(warmup, len(rows)):
  126. candle = rows[index]
  127. ts = frame.index[index]
  128. if pending_exit and position is not None:
  129. net = close_return(str(position["side"]), float(position["entry_price"]), float(candle.open))
  130. equity *= 1 + net
  131. trades.append({"entry_time": position["entry_time"], "exit_time": ts, "side": position["side"], "return": net})
  132. position = None
  133. pending_exit = False
  134. if pending_entry and position is None and equity > 0:
  135. side = pending_entry
  136. position = {
  137. "side": side,
  138. "entry_time": ts,
  139. "entry_index": index,
  140. "entry_price": float(candle.open),
  141. "stop": float(candle.open) * (1 - spec.stop if side == "long" else 1 + spec.stop),
  142. "take": float(candle.open) * (1 + spec.take if side == "long" else 1 - spec.take),
  143. }
  144. pending_entry = None
  145. mark = equity
  146. if position is not None:
  147. side = str(position["side"])
  148. stop_hit = candle.low <= float(position["stop"]) if side == "long" else candle.high >= float(position["stop"])
  149. take_hit = candle.high >= float(position["take"]) if side == "long" else candle.low <= float(position["take"])
  150. if stop_hit or take_hit:
  151. price = float(position["stop"] if stop_hit else position["take"])
  152. net = close_return(side, float(position["entry_price"]), price)
  153. equity *= 1 + net
  154. trades.append({"entry_time": position["entry_time"], "exit_time": ts, "side": side, "return": net})
  155. position = None
  156. mark = equity
  157. else:
  158. gross = candle.close / float(position["entry_price"]) - 1 if side == "long" else float(position["entry_price"]) / candle.close - 1
  159. mark = equity * (1 + gross - FEE)
  160. curve.append((ts, mark))
  161. if index == len(rows) - 1 or equity <= 0:
  162. continue
  163. if position is None and bool(entry.iloc[index]):
  164. pending_entry = str(side_signal.iloc[index])
  165. elif position is not None:
  166. held = index - int(position["entry_index"])
  167. exit_now = bool(exit_.iloc[index])
  168. if spec.family == "riskoff_bidir" and position["side"] == "long":
  169. exit_now = bool(frame["close"].iloc[index] < fast.iloc[index])
  170. if exit_now or held >= spec.hold:
  171. pending_exit = True
  172. series = pd.Series({ts: value for ts, value in curve}).sort_index()
  173. daily = series.resample("1D").last().ffill()
  174. daily = pd.concat([pd.Series([INITIAL_EQUITY], index=[daily.index[0].normalize()]), daily]).sort_index()
  175. return daily.groupby(level=0).last(), trades
  176. def period_metrics(equity: pd.Series, trades: list[dict[str, object]], offset: pd.DateOffset | None) -> dict[str, object]:
  177. start = equity.index[0] if offset is None else equity.index[-1] - offset
  178. scoped = equity[equity.index >= start]
  179. scoped_trades = [trade for trade in trades if pd.Timestamp(trade["entry_time"]) >= scoped.index[0]]
  180. total = float(scoped.iloc[-1] / scoped.iloc[0] - 1)
  181. years = (scoped.index[-1] - scoped.index[0]).total_seconds() / 86_400 / 365
  182. annual = (1 + total) ** (1 / years) - 1 if total > -1 and years > 0 else 0.0
  183. drawdown = float(((scoped.cummax() - scoped) / scoped.cummax()).max())
  184. returns = [float(trade["return"]) for trade in scoped_trades]
  185. wins = [value for value in returns if value > 0]
  186. losses = [value for value in returns if value < 0]
  187. profit_factor = sum(wins) / abs(sum(losses)) if losses else (0.0 if not wins else 999.0)
  188. return {
  189. "total_return": total,
  190. "annualized_return": annual,
  191. "max_drawdown": drawdown,
  192. "win_rate": len(wins) / len(returns) if returns else 0.0,
  193. "profit_factor": profit_factor,
  194. "trades": len(returns),
  195. }
  196. def build_specs() -> list[Spec]:
  197. specs: list[Spec] = []
  198. trend_pairs = ((20, 120), (40, 240))
  199. risk_gates = ("btc_riskoff", "eth_riskoff")
  200. for bar in ("15m", "1H", "4H"):
  201. for fast, slow in trend_pairs:
  202. for lookback in (8, 24):
  203. for threshold in (0.02, 0.035):
  204. specs.append(Spec("crash_follow", bar, fast, slow, lookback, threshold, 0.02, 0.035, 48, "none"))
  205. for gate in risk_gates:
  206. specs.append(Spec("crash_follow", bar, fast, slow, lookback, threshold, 0.02, 0.06, 96, gate))
  207. specs.append(Spec("rebound_exhaustion", bar, fast, slow, lookback, threshold, 0.035, 0.035, 48, gate))
  208. specs.append(Spec("riskoff_bidir", bar, fast, slow, lookback, threshold, 0.02, 0.035, 48, gate))
  209. for threshold in (0.02, 0.035):
  210. specs.append(Spec("candle_funding_proxy", bar, fast, slow, 16, threshold, 0.02, 0.035, 48, "none"))
  211. specs.append(Spec("candle_funding_proxy", bar, fast, slow, 16, threshold, 0.02, 0.06, 96, "eth_riskoff"))
  212. return specs
  213. def row_for_spec(spec: Spec, equity: pd.Series, trades: list[dict[str, object]]) -> dict[str, object]:
  214. row: dict[str, object] = {
  215. "name": spec.name,
  216. "family": spec.family,
  217. "bar": spec.bar,
  218. "gate": spec.gate,
  219. "short_ratio": sum(1 for trade in trades if trade["side"] == "short") / len(trades) if trades else 0.0,
  220. }
  221. for label, offset in HORIZONS:
  222. metrics = period_metrics(equity, trades, offset)
  223. for key, value in metrics.items():
  224. row[f"{label}_{key}"] = value
  225. return row
  226. def markdown_table(frame: pd.DataFrame) -> str:
  227. def cell(value: object) -> str:
  228. if isinstance(value, float):
  229. return f"{value:.4f}"
  230. return str(value).replace("|", "\\|")
  231. rows = [list(frame.columns), ["---" for _ in frame.columns]]
  232. rows.extend(frame.astype(object).where(pd.notna(frame), "").values.tolist())
  233. return "\n".join("| " + " | ".join(cell(value) for value in row) + " |" for row in rows)
  234. def markdown_report(totals: pd.DataFrame, path: Path) -> str:
  235. top = totals.head(10)
  236. best_full = totals.iloc[0]
  237. positive = totals[
  238. (totals["full_total_return"] > 0)
  239. & (totals["3y_total_return"] > 0)
  240. & (totals["1y_total_return"] > 0)
  241. & (totals["6m_total_return"] > 0)
  242. & (totals["3m_total_return"] > 0)
  243. ].sort_values(["full_total_return", "3m_total_return"], ascending=[False, False])
  244. best_positive = positive.iloc[0] if len(positive) else best_full
  245. keep = [
  246. "name",
  247. "family",
  248. "bar",
  249. "gate",
  250. "short_ratio",
  251. "full_total_return",
  252. "full_annualized_return",
  253. "full_max_drawdown",
  254. "full_win_rate",
  255. "full_profit_factor",
  256. "full_trades",
  257. "3y_total_return",
  258. "1y_total_return",
  259. "6m_total_return",
  260. "3m_total_return",
  261. ]
  262. table = markdown_table(top[keep])
  263. period_rows = []
  264. for label, _ in HORIZONS:
  265. period_rows.append(
  266. {
  267. "period": label,
  268. "total_return": best_positive[f"{label}_total_return"],
  269. "annualized_return": best_positive[f"{label}_annualized_return"],
  270. "max_drawdown": best_positive[f"{label}_max_drawdown"],
  271. "win_rate": best_positive[f"{label}_win_rate"],
  272. "profit_factor": best_positive[f"{label}_profit_factor"],
  273. "trades": best_positive[f"{label}_trades"],
  274. }
  275. )
  276. period_table = markdown_table(pd.DataFrame(period_rows))
  277. positive_table = markdown_table(positive[keep].head(7)) if len(positive) else "No candidate was positive across all requested windows."
  278. verdict = (
  279. "worth continuing as a narrow crash-follow research branch, but not ready for live work"
  280. if len(positive) and best_positive["full_profit_factor"] > 1.1
  281. else "not worth continuing yet"
  282. )
  283. return (
  284. "# ETH Bearish Price-Proxy Search\n\n"
  285. f"Output: `{path}`\n\n"
  286. "Scope: read-only local OKX candles under `data/okx-candles`; ETH trades, BTC absolute risk-off filter only; no staged entry, no ETH/BTC relative momentum, no live path.\n\n"
  287. "Families: crash-follow short, rebound-exhaustion short, candle-only funding proxy short, and risk-off bidirectional with bearish bias.\n\n"
  288. f"Best full-sample candidate: `{best_full['name']}`; it fails the 3m window.\n\n"
  289. f"Best all-window-positive candidate: `{best_positive['name']}`. Verdict: {verdict}.\n\n"
  290. "## Best all-window-positive metrics\n\n"
  291. f"{period_table}\n\n"
  292. "## All-window-positive candidates\n\n"
  293. f"{positive_table}\n\n"
  294. "## Top 10\n\n"
  295. f"{table}\n"
  296. )
  297. def main() -> int:
  298. parser = argparse.ArgumentParser()
  299. parser.add_argument("--output-dir", type=Path, default=OUTPUT_DIR)
  300. parser.add_argument("--max-candidates", type=int, default=0)
  301. args = parser.parse_args()
  302. eth_15m = load_frame(SYMBOL)
  303. btc_15m = load_frame(BTC_SYMBOL)
  304. frames = {bar: joined_frames(resample(eth_15m, bar), resample(btc_15m, bar)) for bar in ("15m", "1H", "4H")}
  305. specs = build_specs()
  306. if args.max_candidates:
  307. specs = specs[: args.max_candidates]
  308. rows = []
  309. for index, spec in enumerate(specs, start=1):
  310. equity, trades = run_spec(spec, frames[spec.bar])
  311. rows.append(row_for_spec(spec, equity, trades))
  312. if index % 100 == 0:
  313. print(f"done {index}/{len(specs)}", flush=True)
  314. totals = pd.DataFrame(rows).sort_values(
  315. ["full_total_return", "3y_total_return", "1y_total_return", "full_profit_factor"],
  316. ascending=[False, False, False, False],
  317. )
  318. args.output_dir.mkdir(parents=True, exist_ok=True)
  319. totals_path = args.output_dir / "eth-bearish-price-proxy-totals.csv"
  320. report_path = args.output_dir / "eth-bearish-price-proxy-report.md"
  321. totals.to_csv(totals_path, index=False)
  322. report_path.write_text(markdown_report(totals, totals_path), encoding="utf-8")
  323. print(totals.head(10).to_string(index=False))
  324. print(f"wrote {totals_path} and {report_path}")
  325. return 0
  326. if __name__ == "__main__":
  327. raise SystemExit(main())