跳到主内容
@wquguru
精选86Roan研究与分析

量化回测四大技术:从过拟合到实盘验证

The 4 BACKTESTING TECHNIQUES behind WINNING Strategies:

原文
发到 X
推荐理由

提供了一套完整、可复现且经过实盘验证的回测方法论体系,直接解决量化策略开发中的过拟合痛点,对从事算法交易与策略研发的从业者具有极高的参考价值。

The 4 BACKTESTING TECHNIQUES behind WINNING Strategies:

制胜策略背后的 4 种回测技术:

i've spent the last 2 years running backtests on everything from mean reversion setups to volatility arbitrage to prediction market signals

过去两年,我对从均值回归设置到波动率套利再到预测市场信号的各种策略进行了回测。

some strategies survived and tbh most of them died and the difference was never the strategy itself, it was how i tested it

有些策略幸存了下来,说实话,大多数都失败了。差异从来不是策略本身,而是我测试它的方式。

a backtest is not proof your strategy works, it's a stress test to see how easily it breaks

回测并不能证明你的策略有效,它是一次压力测试,用来观察策略在何种情况下容易崩溃。

here are the 4 techniques i've actually run, what worked, what broke AND what i still use

以下是我实际运行过的 4 种技术、哪些有效、哪些失败,以及我至今仍在使用的方法。

technique 1: standard in-sample / out-of-sample split

技术 1:标准的样本内/样本外分割

verdict: broken by default, NEVER TRUST THIS

结论:默认失效,永远不要信任这种方法。

the setup is easy, take 5 years of data and train on the first 4, test on the last 1

设置很简单:取 5 年的数据,在前 4 年进行训练,在最后 1 年进行测试。

the problem is subtle - every time you tweak the strategy and re-run, you're peeking at the test data and after 30 iterations your "out-of-sample" is FULLY contaminated

问题很微妙——每次你调整策略并重新运行时,你都在窥探测试数据;经过 30 次迭代后,你的“样本外”数据已被完全污染。

the first strategy i ever backtested was a simple pairs trade between two energy stocks that showed a Sharpe of 2.1 on the standard split, so i deployed $2,000 of my own money and lost 40% of it in 3 months

我最早回测的策略是一只简单的两只能源股配对交易,在标准分割下夏普比率达到 2.1,于是我投入了 2,000 美元自有资金,却在 3 个月内损失了 40%。

going back later i realized i'd re-run that backtest 47 times during tuning, the test data was never really untouched

后来回顾时,我意识到我在调优过程中重跑了 47 次回测,测试数据从未真正未被触碰过。

use this only for a quick first look, NEVER as the final validation

仅用于快速初步查看,绝不要作为最终验证。

technique 2: walk-forward validation

技术 2:前向滚动验证(Walk-forward validation)

verdict: the real workhorse, this is what i actually use

结论:真正的中坚力量,这也是我实际使用的方法。

instead of splitting once, you slide a window through the data

与其只分割一次,不如让窗口在数据上滑动。

train on 2018-2020, test on 2021 train on 2019-2021, test on 2022 keep sliding

用 2018-2020 年训练,在 2021 年测试 用 2019-2021 年训练,在 2022 年测试 持续滑动

each test window is data the model has never seen and you get 5 or 6 test periods instead of JUST ONE

每个测试窗口都是模型从未见过的数据,你会得到 5 或 6 个测试期,而不仅仅是一个。

what this catches:

这种方法能捕捉到:

> strategies that only worked in one regime (the pattern shows up immediately) > parameters that shift wildly when retuned (unstable strategy, red flag) > strategies that survive across every window (this is real edge)

> 仅在单一市场状态下有效的策略(模式会立即显现) > 重新调参时剧烈波动的参数(不稳定的策略,红色警报) > 在每个窗口中都幸存下来的策略(这才是真正的优势)

at our fund we killed a stat arb strategy that showed Sharpe 2.4 on a standard split, but walk-forward revealed it worked beautifully in 2019-2020 and completely died in 2021-2022, the regime had shifted underneath us and it saved us months of losses

在我们基金里,我们淘汰了一个在标准分割下显示夏普比率为 2.4 的统计套利策略,但前向滚动验证揭示它在 2019-2020 年表现完美,而在 2021-2022 年完全失效。市场状态发生了转变,这为我们节省了数月的亏损。

but this is slower and more painful than a standard split and it's also the reason institutional backtests match live P&L :)

但这比标准分割更慢、更痛苦,这也是机构回测结果与实盘盈亏相符的原因 :)

technique 3: purged k-fold cross-validation

技术 3:净化 K 折交叉验证(Purged k-fold cross-validation)

verdict: fixes a hidden bug in walk-forward

结论:修复了前向滚动验证中的一个隐藏缺陷。

financial data has memory, today's price is not independent of yesterday's

金融数据具有记忆性,今天的价格并非独立于昨天的价格。

when your training window ends on december 31 and your test window starts january 1, information leaks across that boundary and your Sharpe looks better than it should

当你的训练窗口在12月31日结束,而测试窗口从1月1日开始时,信息会跨越该边界发生泄漏,导致你的夏普比率看起来比实际情况更好

purged k-fold fixes this, Marcos Lopez de Prado covers it in Advances in Financial Machine Learning

purged k-fold(清洗k折交叉验证)可以解决此问题,Marcos Lopez de Prado 在《金融机器学习进展》一书中对此进行了详细阐述

the idea is simple:

其理念很简单:

> split data into folds like standard cross-validation > when a fold is used for testing, remove the adjacent observations that overlap in time > this eliminates the leakage

> 像标准交叉验证一样将数据分割为多个折 > 当某个折用于测试时,移除在时间上重叠的相邻观测值 > 这消除了泄漏

a QUANT friend of mine who runs an ML-based factor model showed me his numbers before and after adding purging, Sharpe dropped from 1.9 to 1.4 on the same strategy with the same data and the extra 0.5 was pure leakage he didn't know he had

我的一位运行基于机器学习的因子模型的量化朋友向我展示了他在添加清洗前后的数据表现,同一策略、同一数据下,夏普比率从1.9降至1.4,多出的0.5纯粹是他未曾察觉的泄漏

use this when you're training ML models on financial data, the leakage in tree-based models is brutal without it

在对金融数据进行机器学习模型训练时使用此方法,否则基于树模型的泄漏会非常严重

technique 4: monte carlo trade shuffling

技术4:蒙特卡洛交易重排

verdict: the reality check that saves capital EVERY SINGLE TIME

结论:每次都能保护资本的现实检验

your backtest shows one sequence of trades, Monte Carlo randomizes the order and runs it thousands of times

你的回测显示了一组特定的交易序列,蒙特卡洛模拟则随机打乱顺序并运行数千次

why this matters:

为什么这很重要:

> your backtest might have gotten lucky with sequencing, what if the drawdown happened in month 2 instead of month 10 > the max drawdown you observed is one path, Monte Carlo shows the full range > the 5th percentile drawdown is often 2 to 3 times worse than what you saw

> 你的回测可能在序列安排上运气好,但如果回撤发生在第2个月而不是第10个月呢? > 你观察到的最大回撤只是其中一条路径,蒙特卡洛展示了完整的范围 > 第5百分位数的回撤通常比你看到的要糟糕2到3倍

few months ago (during the hype of 15-min BTC markets) i built a systematic prediction market strategy that showed 12% max drawdown across 18 months of backtest, but before deploying i ran Monte Carlo with 10,000 shuffled sequences and the 5th percentile scenario showed a 34% drawdown

几个月前(在15分钟比特币市场热潮期间),我构建了一个系统性预测市场策略,在18个月的回测中最大回撤仅为12%,但在部署前我运行了包含10,000个重排序列的蒙特卡洛模拟,结果显示第5百分位情景下的回撤高达34%

ofc i didn't deploy at full size, i sized it at 25% of what i originally planned and three months in the strategy hit a 22% drawdown, but the smaller size meant i could hold through it and the strategy recovered to finish the year up 31%

当然我没有按原计划的全规模部署,而是将其规模调整为原计划的25%。三个月后,该策略遭遇了22%的回撤,但较小的规模使我能够坚持持有并最终恢复,年底收益达到31%

Monte Carlo is why i stayed in that trade instead of blowing up

正是蒙特卡洛模拟让我留在那笔交易中,而没有爆仓

what i actually use in production NOW:

我现在在生产环境中实际使用的方法:

> walk-forward validation as the primary test

> 以滚动向前验证(walk-forward validation)作为主要测试方法

> purged k-fold when the strategy uses ML models

> 当策略使用机器学习模型时,采用清洗k折交叉验证

> Monte Carlo shuffling on strategies that survive both, before any real capital

> 对通过上述两种测试的策略,在任何真实资金介入前进行蒙特卡洛重排

> standard in-sample/out-of-sample only for the very first pass (rarely tho)

> 仅在最初的第一轮筛选中使用标准的样本内/样本外划分(尽管很少这样做)

if your backtest is designed to make you feel good, it's designed to LOSE you money for real.

如果你的回测设计是为了让你感觉良好,那它的设计初衷就是让你真正亏钱。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件