在历史逐笔數據上回測交易策略
1. 在哪里获取數據
Three quality tiers for backtest data, in order of cheap-and-rough to gold-standard:
- Public minute candles (free). 可从 CoinGecko、交易所 API(有速率限制)或 CSV 转储获取。適合對方向性论点做合理性检验,對执行成本敏感的策略则毫无用處。
- 成交逐笔數據(便宜或免費)。 每一笔成交均包含時间戳、价格、规模、方向。交易所以分塊文件发布。捕捉价格形成但不反映流动性(您不知道什么没成交)。
- 完整 L2 订单簿重建(昂贵)。 每一次订单的新增、修改、撤销、成交。让您能模拟把自己的订单挂在真實订单簿上。如果您的策略對滑点敏感,这是必需的。bitexasia 對达到一定月交易量門槛的帳戶免費提供 L2 归档;商业供應商對其余his.
2. 前视偏差
The single most common mistake in backtests. Look-ahead happens when your strategy uses information that wouldn't have been available at the time of the decision.
The obvious version
Computing a rolling 20-period mean and using it on the same row. The mean uses the current row's value to compute itself.
The subtle version
Using exchange's published "open" price for a 1-minute candle as your entry price. Real entry would have been somewhere inside that minute, not at the open. If your strategy is "buy at open if some condition," your real fills are systematically worse.
The deeply hidden version
幸存者偏差。您在当前的前 100 名币種上回測,这排除了所有已退市的資產。从「前 100 名」中挑選的策略历史上受益于从未持有下一个 FTX。Kaiko 或 CryptoCompare 等付费供應商的归档中包含已退市資產 — 為此付费,不要伪造。
3. 执行模拟
Even with perfect data and no look-ahead, your simulated fills are optimistic if you assume you'd have hit the touch every time. Two corrections:
- 滑点 model. 對于市价单,在下单時刻扫过订单簿。對于限价单,只有当對手报价穿过您的价格時才成交 and 并且在您之后有足够成交量来满足您的规模。
- 時延模型。 Assume your order arrives 50–250 ms after you decided to send it. The book has moved by then. If your strategy edge dies inside that window, the strategy doesn't have edge.
4. 加密交易中真正重要的指標
夏普比率没问題,但在加密领域有另外四项更重要:
- 最大回撤。 最严重的高点到低点跌幅有多深?即便长期夏普看起来不错,加密策略经常出现 60–70% 的回撤。如果您在心理上无法承受这種回撤,这个策略就不適合您。
- 回撤持續時间。 比深度更糟糕的是持續時间。30 天就能从 −40% 中恢複的策略尚可接受;耗時 18 个月的则不可。
- Calmar 比率。 年度ised return / max drawdown. A 30% return with 60% max drawdown is half as good as a 20% return with 20% max drawdown.
- 尾部風險与资金成本。 持有杠杆头寸的成本是资金费率 + 偶发尾部事件清算。要看净收益,不是毛收益。
5. 样本外驗證
Split your data: train on the first 70%, validate on a held-out 30%. Tune parameters on train. Test on validation only once. If validation performance is markedly worse than train, you overfit. Re-think and start over — don't tune on validation.
向前滚动分析(walk-forward analysis)更佳:沿時间向前滚动训練/驗證窗口,每一步重新训練。让您看到在市場狀態变化時策略表现如何演化。
6. 實盘前先模拟交易
在投入真實资金之前,请在我们的 sandbox API environment for at least a month. The sandbox uses live price feeds with simulated fills — closes the gap between backtest and reality without risking real money.
For order types and execution mechanics see the 現貨交易 and 槓桿交易 pages.