在历史逐笔数据上回测交易策略
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.