GUIDE · TESTING A STRATEGY

Out-of-sample and walk-forward testing in TradingView

Out-of-sample testing means choosing a strategy's settings on one stretch of history and judging them, unchanged, on a later stretch they never saw. Walk-forward testing repeats that step through time. TradingView leaves the split to you. Tuned on TSLA from 2016 to 2023, the best of 16 SuperTrend settings made 1,606.4%; on the next three years it lost 13.4% while the stock rose 51.8%.

In sample and out of sample

In sample is the history you look at while you choose the rules and settings. Out of sample is a later stretch you keep apart and run once, with nothing changed. The split matters because the setting that scores best on a stretch of history fits two things at once: whatever lasting pattern is there, and that stretch's noise. Only the first carries forward, and only data the setting never saw can tell them apart.

TradingView's own manual describes it the same way: optimise on the in-sample data, then test that configuration on the out-of-sample data with no further tuning.

Worked example: SuperTrend on TSLA

Sixteen settings of the copilot's SuperTrend strategy, ATR length 7, 10, 14 or 21 times a multiplier of 1.5, 2, 3 or 4, longs only and with our default costs, ran on TSLA from 4 January 2016 to 18 October 2023. The best by Sharpe was ATR 10 with a multiplier of 2. Then it ran, unchanged, on the three years after:

In sampleOut of sample
Dates4 January 2016 → 18 October 202319 October 2023 → 19 October 2026
Sessions1,962752
Net, best setting+1,606.4%−13.4%
Sharpe, best setting1.080.06
Trades5322
Holding TSLA+1,528.9%+51.8%

In sample the tuned setting beat holding the stock. Out of sample it lost money while the stock rose by half. The tester's word is decayed: a Sharpe still above zero, far under half of what it showed in sample. Of the 16 settings, 12 decayed, 3 survived and 1 did not survive, and the plain default, ATR 10 with a multiplier of 3, decayed too: +559.7% in sample, +16.9% out.

A split can pass as well: the RSI(2) example on SPY had a better Sharpe in its last three years than before them. Run the split before trusting a result either way.

Walk-forward: the split, repeated

One split is one draw. Walk-forward testing makes several: fit the settings on the history up to a point, test them on the slice right after, then move the point forward and fit again. Anchored walk-forward keeps the start of the history, so the fit window grows; rolling walk-forward moves both ends. Our optimiser's walk-forward is anchored, with three test slices, each fit starting in January 2016 and ending where its slice begins. On the same TSLA strategy, starting from ATR 10 with a multiplier of 3:

Test sliceSettings chosenSharpe, fitted → testedNet on the sliceStarting settings
Sep 2018 → May 2021×2.3, ATR 5, 2 ATR target0.93 → 0.83+63.3%+567.1%
May 2021 → Feb 2024×2.3, ATR 8, no target1.34 → 0.60+54.1%+33.9%
Feb 2024 → Oct 2026×2.3, ATR 8, no target1.10 → 0.41+25.2%+19.2%

In all three slices the Sharpe on unseen data came in under the Sharpe on the data the settings were fitted to. That shrink is normal, and the tested column is the honest one. The fitted settings beat the starting ones in the last two slices and fell far behind in the first, where they added a 2 ATR profit target and made +63.3% against +567.1% for the starting settings. The optimiser counts a slice as held up only when the fitted settings made money there and beat the starting ones, so this one reads 2 of 3.

How to do it in TradingView

  1. Choose the split before you look
    Decide the out-of-sample dates first, for example the last third of the history, and write them down. Moving them after seeing a result turns the test back into tuning.
  2. Tune on the in-sample dates only
    Set the testing period at the top of the strategy report to the in-sample dates, or add a date window to the script, and change settings only while looking at that range.
  3. Run the out-of-sample dates once
    Switch the testing period to the later dates without touching an input. Compare Sharpe or the average trade rather than net return, since the two periods differ in length.
  4. Repeat it for a walk-forward
    Move both dates forward and do it again, keeping your own table of each step's settings and results.
A date window: tune with it on the in-sample dates, then move it once
fromDate = input.time(timestamp("2016-01-04"), "Test from")
toDate   = input.time(timestamp("2023-10-18"), "Test to")
inWindow = time >= fromDate and time <= toDate

// open trades only inside the window
if inWindow and longCondition
    strategy.entry("Long", strategy.long)

What counts as passing

Our tester's rule for the held-out stretch: survived if its Sharpe is above zero and at least half the in-sample Sharpe; decayed if above zero but under half; did not survive at zero or below; too few trades to judge under 5 trades. The free tester holds out the last stretch nearest a third and prints that word on every run, and the optimiser runs the walk-forward. Costs change the answer too: commission and slippage, explained.

Questions

In-sample is the history you look at while choosing the rules and settings. Out-of-sample is a later stretch kept apart and looked at once, with the settings frozen. Only the second says anything about how the settings might do on data they were not chosen on.

Sources, read on 20 October 2026: TradingView's Pine Script manual, “Strategies” (in-sample and out-of-sample testing, overfitting, the testing period in the strategy report). The numbers are our engine's, run on 20 October 2026 on our daily data. A backtest describes the past; it is not a forecast or advice. TradingView belongs to its owner, and Alpha Charts is not affiliated with it.