OPEN-SOURCE SCRIPT

Backtest and Beyond? CT CPCV Research Lab v1.2 [Pine v6]

40
CT CPCV Research Lab v1.2

One profitable backtest does not prove you've found a profitable trading strategy.


It proves only one thing: Your strategy worked once...on one version of history.
The real question is: Would it still work if history had unfolded differently?

Professional quantitative researchers have been asking that question for decades.
This framework brings that same question—and one possible way of answering it—to individual traders.

What this description covers
By the time you finish reading this description, you'll understand:
• Why a profitable backtest can be dangerously misleading
• Why professional quantitative researchers demand stronger evidence before trusting a strategy.
• Why Combinatorial Purged Cross-Validation (CPCV) has become one of the most respected validation techniques in quantitative finance.
• What this framework does.
• What it doesn't do.
• Why those differences matter.
So, if that sounds interesting...keep reading.

******
What Is CT CPCV Research Lab?
CT CPCV Research Lab is an open-source quantitative research framework built for TradingView.
It allows complete trading strategies, not just individual indicators, to be evaluated using Combinatorial Purged Cross-Validation (CPCV) under consistent research conditions.

Rather than asking: "Which strategy produced the highest historical return?"
it asks: "Which strategy demonstrated the greatest robustness across many independent historical tests?"
The goal isn't to predict the future. The goal is to help you place more appropriate confidence in what the past may, or may not, be telling you.

If you've ever developed a trading strategy, you've probably followed a familiar process.
• Build some trading rules.
• Run a historical backtest.
• See a profitable equity curve.
• Assume you've found a trading edge.

Unfortunately, that's exactly how thousands of strategies fool their creators every day.
A traditional backtest answers only one question: "What would have happened if I had traded this one sequence of historical events?"
That's a useful question. It just isn't the only question that matters. The uncomfortable truth is that one backtest proves far less than most traders believe.

Built to Teach, Not Just Calculate
Most TradingView scripts give you signals. Some give you statistics. Very few explain why those signals or statistics matter.
CT CPCV Research Lab was designed differently.
Throughout the framework you'll find:
• Plain-English explanations of the underlying research methodology.
• Educational comments describing the purpose of each major section.
• Input tooltips explaining not only what each setting does, but why you might want to change it.
• Built-in guidance that helps interpret CPCV results instead of simply displaying numbers.
Whether you're completely new to quantitative research or already familiar with CPCV, the goal is the same:
Every major part of this framework should either perform the research...or teach the research.
If understanding the research process is just as important to you as obtaining the results, this framework was built with that philosophy in mind.

Why great backtests often fail
Markets only give us one history. One sequence of bull markets, one sequence of bear markets, one sequence of crashes, recoveries, trends and sideways markets. When a strategy performs well on that single history, we don't automatically know why.
Did it discover a genuine market behaviour?
Or...
Did it simply get lucky?
Was it accidentally tailored to one particular period of history?

This is one of the biggest problems in quantitative finance. It is known as overfitting, and it is one of the main reasons strategies that look exceptional in historical testing often disappoint in live trading. The better a strategy becomes at explaining the past, the greater the risk that it has simply memorized the past rather than discovered something that persists into the future.

"I'll just use Out-of-Sample testing."
Good. That's already a major improvement. Out-of-Sample (OOS) testing separates historical data into two sections. One section is used to develop the strategy and the other section is hidden until the strategy is complete. Only then is the strategy evaluated on data it has never seen before.

This helps answer an important question: "Does my strategy still work on genuinely unseen data?"
That's far more honest than testing on everything, but it still has one important weakness.
You only get one Out-of-Sample test, one unseen historical period...and one result.

"I'll use Walk-Forward Analysis."
Even better. Walk-Forward Analysis repeats the process. The strategy is trained on earlier data, tested on the next unseen period, then the window moves forward and the process repeats.
Professional quantitative researchers have used Walk-Forward Analysis for many years because it is a significant improvement over a single backtest. But even Walk-Forward follows one continuous timeline.
History always unfolds in the same order, “Beginning...Middle...End.”
It creates many tests…but they're all built from the same historical journey. Walk-Forward Analysis remains one of the most respected validation techniques used by professional researchers today. CPCV should not be viewed as replacing Walk-Forward, but as answering a different and often more demanding research question.

Enter Combinatorial Purged Cross-Validation (CPCV)
Professional quantitative researchers wanted something even more demanding. Instead of relying on one backtest...or one Out-of-Sample period...or one Walk-Forward schedule...,they wanted to repeatedly evaluate a strategy across many different combinations of historical data. One of the best-known methods for doing this is called:
Combinatorial Purged Cross-Validation (CPCV)

Combinatorial Purged Cross-Validation was introduced by Marcos López de Prado in Advances in Financial Machine Learning (2018), where it was developed for validating machine-learning models in finance. This framework adapts its core principles, combinatorial train/test splits, purging, embargoing, and reconstructed out-of-sample paths, to the practical constraints of Pine Script.
Although the name sounds intimidating, the idea is surprisingly simple. Imagine interviewing someone for an important job. Would you hire them because they answered one interview question correctly?
Of course not. You'd ask many questions, test different skills, look for consistency.

Trading strategies deserve exactly the same treatment. A traditional backtest gives a strategy one interview. CPCV gives it dozens of different interviews.
If the strategy performs consistently across many independent historical tests instead of one lucky backtest, it earns greater confidence. Not certainty. Nothing in financial markets offers certainty.
Just stronger evidence.

What does "Combinatorial Purged Cross-Validation" actually mean?
The name sounds complicated, but each word simply describes part of the process.
Combinatorial: Instead of creating one train/test split, CPCV creates many different combinations of training and testing periods. This allows the strategy to be evaluated across many historical scenarios rather than relying on one convenient sequence.
Purged: Financial observations that occur close together often contain overlapping information.
Imagine two students sitting the same exam. If one student quietly glances at the other's answers before writing their own, the exam is no longer fair.
Purging removes nearby training observations that could accidentally leak information into the test period.
Cross-Validation: Rather than asking "Did this strategy work once?", Cross-Validation repeatedly asks
"Does this strategy continue working when tested on different unseen historical data?"
The goal isn't to find one impressive result. The goal is to see whether good performance remains consistent.

Why this framework exists
For many years, advanced quantitative validation techniques were used primarily by institutional researchers, hedge funds and quantitative investment firms. Retail traders rarely had access to these ideas, not because they were secret, but because they were often buried in academic research or implemented in specialist software.
This framework was built to present those same research principles in plain English so individual traders can understand them, question them, and apply them for themselves. Good research shouldn't depend on where you work. It should depend on how carefully you test your ideas.

A note about the Pine implementation
This implementation is designed to bring the core principles of institutional Combinatorial Purged Cross-Validation (CPCV) research into Pine Script while respecting the practical execution limits of the TradingView environment.
Professional quantitative research platforms often have access to dedicated computing resources with relatively few practical limits on memory, processing time, data sources or dataset size.

TradingView's Pine Script is designed for interactive chart analysis and therefore operates within execution-time, memory and resource limits so scripts remain responsive for all users. To make CPCV practical within those limits, this implementation makes several engineering choices, including limiting the number of chronological groups, reference strategies and stored observations. These are implementation constraints, not changes to the underlying CPCV methodology.

A note on purging and embargo in this implementation. These techniques exist to prevent information from a training observation's forward-looking label leaking into a nearby test period. The built-in candidate strategies use one-bar returns, so their natural leakage window is very short and the purge/embargo settings have limited effect on them. Their value grows substantially if you replace a candidate with a strategy that uses multi-bar holding periods or forward-horizon labels, where boundary leakage is a real risk. They are included so the framework remains methodologically complete and correct for the strategies you may add, not only the ones shipped by default.

The objective has always been to preserve the essential research principles of CPCV while delivering an educational framework that runs efficiently inside TradingView. If you're learning quantitative strategy validation, understanding why CPCV works is far more valuable than simply increasing the number of groups from 8 to 20. Sound methodology will improve your research far more than simply making the computation larger.

How to Read the Tables
This framework displays its results in four tables rather than on the price chart, because CPCV evaluates research quality, not price action. Here's what each table shows and how to act on it.
Split Diagnostics
Each row is one training/test experiment. The engine holds out two chronological groups as unseen test data, selects the best candidate using the training data only, then measures how that choice performed on the held-out test data.
Read the columns left to right: which groups were held out, which candidate was selected, its score on training data, its score on test data, its test return, and its worst drawdown during the test period.
The single most important thing to look for is a candidate that scored well in training but poorly in testing. That gap is the fingerprint of overfitting. A strategy that looks brilliant in training and then collapses out-of-sample has told you something valuable, it just wasn't what you were hoping to hear.
Do not judge the framework by the best split. One good row is not evidence. Consistency across many rows is.

Path Diagnostics
Each row is one fully reconstructed out-of-sample history, assembled from test segments that the strategy never trained on. Instead of one backtest, you are looking at several independent reconstructions of history.
Four things deserve your attention, and the Results Guide lists them in order:
The median path tells you the typical outcome, not the luckiest one. The worst path tells you how bad an unlucky reconstruction looked, this is your pessimistic case, and a strategy that stays tolerable even here is showing real resilience. The positive-path rate tells you how many reconstructions finished profitable; a strategy profitable on one path and negative on the rest has not earned confidence. And return versus drawdown reminds you that a good return purchased with a brutal drawdown is not the same as a good return earned smoothly.
If the paths all look nearly identical, that usually means one candidate dominated selection across every split. That is normal and informative, it simply means CPCV is validating that one strategy.

Research Integrity
This panel does not measure whether your strategy is profitable. It measures how much confidence you should place in the research configuration itself, whether the sample was large enough, the coverage valid, the paths consistent, the worst case survivable, and the typical drawdown manageable.
Treat it as a credibility check on the test, not a prediction of the result. A high score means the validation has few obvious weaknesses. A low score means any attractive-looking performance should be treated with extra caution, because the setup itself may not support strong conclusions. The panel's own final row says it plainly: research confidence, not expected profit.

Results Guide
A permanent on-chart cheat sheet summarizing the reading order above and explaining the color coding, green when the median and worst case are both positive, yellow when the median holds up but the worst case is fragile, red when the median or most paths failed. If you remember nothing else, the guide is there to remind you: focus on the median and the worst case, not the single best split.

Configuring the Candidates
The most common first experience with this framework is to enable all ten candidates, run it on a favorite chart, and find every split selecting the same strategy and every path failing. That is not a malfunction and understanding why explains how to configure the framework properly.
CPCV can only reveal something interesting when the selection is contested, i.e., when different candidates genuinely compete to be chosen, and different ones win in different periods. If every candidate you enable behaves the same way, the framework isn't choosing between ideas. It's choosing the least-bad member of one idea.
Diversify the families, not just the parameters
Five EMA crossovers at different lengths are not five different strategies. They are one strategy at five speeds, and they are highly correlated…they mostly agree, so selection barely changes and the reconstructed paths become near-identical copies.
The candidates ship in two behavioral families:
Trend-following (the EMA crossovers, MACD, Donchian, Supertrend) profits when moves persist. Mean-reversion (RSI, Bollinger) profits when price snaps back toward an average.
These families tend to be opposites: when one struggles, the other often works, because trending and ranging are opposite market conditions. A balanced set, a few trend models alongside both reversion models, lets different candidates win in different periods, which is exactly what makes the paths diverge.

Test where regimes actually change
The framework can only display path dispersion if the data contains different conditions for different candidates to win in. A market in one long, clean trend will let a single slow trend-follower dominate every split, producing results that are consistent but undifferentiated. A market stuck in pure chop will punish every trend model at once, which is how an all-red board appears. The most revealing samples contain both trending and ranging periods, so widen the date range until it spans at least one full cycle of each, and consider instruments that move through both rather than sitting in a single prolonged trend.

Change the metric to change the winner
The selector ranks candidates using one training metric, and different metrics crown different winners. Sharpe rewards smoothness, Calmar rewards drawdown avoidance, Sortino favors directional trend-following, Omega favors skewed reversion payoffs. Running the same enabled set under different Optimization Metrics is a fast way to see selection shift, and watching which candidate wins under which metric is itself a lesson in how sensitive strategy selection really is.

What a good configuration looks like
Success here is not a green board. It is a contested one. In Split Diagnostics you want the MODEL column to show several different names rather than one name repeated down every row. In Path Diagnostics you want P1, P2, P3 and the rest to be visibly different numbers rather than clones, a genuine median sitting between a positive best case and a negative worst case. That spread is the CPCV signal. A board where one model wins everything is almost as uninformative as one where everything fails; both mean the selection was never truly contested.
One honest warning. Do not hunt through instruments and metrics until you find a green result and then treat that as proof. That is simply overfitting one level higher, cherry-picking the demonstration instead of the strategy. The purpose of a contested run is to watch the method work, red parts included. A result that mixes success and failure across its paths is not a disappointing outcome. It is the framework telling the truth.


Using Your Own Strategies
The ten built-in candidates are examples, not the point. The framework is designed so you can delete any of them and drop in your own logic, that is its intended use, not a hack.

To replace a candidate, keep to three rules:
First, produce a persistent desired-position series with exactly three possible values: +1 for long, 0 for flat, −1 for short. The framework evaluates this series, not your entry/exit orders, so your logic must hold a position until it decides to change it, rather than firing a one-bar signal.
Second, stay causal. Use only information available up to the current bar. Do not use future data, negative historical offsets, or lookahead on. The framework already lags exposure by one bar when simulating returns, so your job is simply to avoid look-ahead in the signal itself.
Third, wrap your output through the direction filter (f_direction) exactly as the existing candidates do, so the Long Only / Short Only / Long & Short control keeps working.

That's it. Anything that respects it, a moving-average system, an oscillator, a breakout rule, or something entirely your own, will be validated under the same CPCV conditions as the built-in examples. If your strategy uses multi-bar holding periods or forward-looking labels, this is also where purging and embargo begin to do real work, so set those values to match your strategy's actual horizon.

An important limitation
If you've read this far and concluded that CPCV is the ultimate solution... then this description has failed.
CPCV is one of the most rigorous historical validation techniques available, but it is not a crystal ball.
No historical validation method can predict the future.

Markets evolve
One of the biggest misconceptions in technical analysis is that indicators suddenly "stop working."
Most of the time, they don't. The mathematics haven't changed. A 50/200 EMA crossover calculates exactly the same way today as it did ten years ago.
RSI hasn't changed.
MACD hasn't changed.
Bollinger Bands haven't changed.
…the market changed.
Financial markets constantly move through different environments. Sometimes they trend strongly.
sometimes they move sideways for months, other times volatility explodes…and then it disappears. Periodically, liquidity is abundant…and inexplicably, it evaporates.

These and other changing environments are commonly called market regimes. Within every regime there are often shorter-lived shifts or states in market behavior that further influence strategy performance. The same trading strategy can perform exceptionally well in one regime and struggle in another, even if the strategy itself never changes. That's one of the most important lessons in quantitative research.

No technical indicator can know which market regime it is operating within. It simply continues performing the same calculation while the market itself changes around it.

What this framework can, and cannot, tell you
This framework is designed to answer one question: "How robust did this strategy appear across many independent historical tests?"
It is not designed to answer another: "Will this strategy continue working as markets evolve?" That requires continued observation, adaptation and research.
Do not be surprised if the same candidate is selected on every split. When one strategy scores highest across all training folds, CPCV effectively becomes a validation of that single strategy, and if it then fails out-of-sample on every path, that is not a malfunction. It is the framework doing its job: telling you the best-looking in-sample choice did not survive honest testing.
Good quantitative research is not about finding certainty. It is about reducing the chances that we fool ourselves before risking real capital.
One important clarification about what is being validated. This framework does not cross-validate each candidate strategy in isolation. It cross-validates a selection procedure: on every training split it picks the best-scoring candidate, then judges that choice on unseen test data. This matters because strategy selection is one of the most common places overfitting hides. A candidate that consistently looks best in training but fails in testing is exactly what CPCV is designed to expose.

The philosophy behind this project
Good quantitative research doesn't ask: "How can I prove this strategy works?"
It asks: "How hard can I try to prove that it doesn't?"
Every independent test that a strategy survives earns it a little more credibility. Not because it made more money, but because it had more opportunities to fail, and didn't.
That's the philosophy behind CT CPCV Research Lab.

A personal perspective
In my experience, market structure, liquidity, positioning, capital flows, and changing market regimes often explain market behavior more consistently than any individual technical indicator alone. This is where my own work has focused for many years — and, increasingly, on the fragility that builds inside a market before it shows up in price.
That does not mean technical analysis has no value. Millions of traders around the world continue to make decisions using technical analysis every day. Whether those decisions rest on enduring market truths or widely shared behavioral patterns is a separate question. Either way, when millions of participants respond to similar signals, those behaviors become part of the market itself.
For that reason alone, understanding how technical strategies behave — and how rigorously they should be tested — remains a worthwhile pursuit. If you're one of those traders, my hope is that this framework helps you evaluate technical strategies more honestly than a single backtest ever could.
Whether your strategy uses moving averages, RSI, MACD, Bollinger Bands, Donchian channels, or something entirely your own, it deserves to be tested honestly. And if this framework leaves you curious about why the same strategy thrives in one regime and dies in another — about the structural conditions that shift beneath price before price ever moves — then it has done something a backtest never could. That question is where my own work goes next.
If this project encourages even a few traders to question impressive-looking backtests, demand stronger evidence, and approach strategy development with greater intellectual honesty, then it has achieved exactly what it was created to do.
Good research doesn't eliminate uncertainty. It simply reduces the chances that we mistake luck for skill. If this framework helps even a handful of traders make that distinction, then it has served its purpose.

Disclaimer
CT CPCV Research Lab is provided for educational, informational, and analytical purposes only.
Nothing within this script, its outputs, tables, scores, grades, metrics, documentation, or associated materials constitutes financial advice, investment advice, trading advice, legal advice, or a recommendation to buy, sell, hold, or otherwise transact in any financial instrument.
Trading and investing involve substantial risk. Past performance is not indicative of future results, and no historical validation method, including Combinatorial Purged Cross-Validation, can predict future performance. Markets are inherently uncertain, and losses may occur.
Users are solely responsible for all trading and investment decisions made using information derived from this script. The developer makes no representations or warranties regarding accuracy, completeness, profitability, suitability, reliability, or fitness for any particular purpose.
CT CPCV Research Lab is a research and validation tool. It does not generate trade signals, is not an automated trading system, and must not be relied upon as the sole basis for any trading or investment decision. A high Research Integrity score reflects the credibility of a research configuration, not the likelihood of future profit.
By accessing or using this script, the user acknowledges and accepts all risks associated with its use and agrees that the developer shall not be liable for any direct, indirect, incidental, consequential, or special losses or damages arising from the use of the script or any information it provides.

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.