1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
103 lines of Pine Script v6 from scripts/backtest-integrity-scanner.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.
backtest-integrity-scanner.pine
//@version=6
// Taught to Trade - Backtest Integrity Scanner
// Measures the properties of THIS chart that quietly break backtests.
// It does not read your strategy. It measures the instrument and timeframe
// your strategy is being tested on, and tells you which assumptions are unsafe here.
// No buy/sell signals. Alerts are yours to configure.
indicator("Taught to Trade - Backtest Integrity Scanner", shorttitle = "TT BIS", overlay = true, max_bars_back = 1000)
grpC = "Cost assumptions"
commPct = input.float(0.04, "Commission per side (%)", minval = 0.0, step = 0.01, group = grpC, tooltip = "What one side of a round trip actually costs you, as a percentage of notional. Use your broker real number, not zero.")
slipPct = input.float(0.02, "Slippage per side (%)", minval = 0.0, step = 0.01, group = grpC, tooltip = "Expected slippage per side. Zero is the single most common dishonest backtest setting.")
grpS = "Sample"
lookback = input.int(500, "Sample window (bars)", minval = 50, maxval = 1000, group = grpS)
holdBars = input.int(20, "Assumed bars held per trade", minval = 1, group = grpS, tooltip = "Used only to estimate how many trades your history could produce. Change it to match your strategy.")
minTrades = input.int(30, "Minimum trades you would accept", minval = 5, group = grpS)
grpD = "Data checks"
atrLen = input.int(14, "ATR length", minval = 2, group = grpD)
gapMult = input.float(2.0, "Flag gaps larger than (x ATR)", minval = 0.5, step = 0.5, group = grpD)
costWarn = input.float(25.0, "Warn when cost exceeds this % of a median bar", minval = 1.0, step = 5.0, group = grpD)
showGaps = input.bool(true, "Mark flagged gap bars on the chart", group = grpD)
grpT = "Table"
posIn = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpT)
szIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grpT)
atr = ta.atr(atrLen)
barsAvail = bar_index + 1
var int tFirst = na
if na(tFirst)
tFirst := time
yearsData = (time - tFirst) / 31557600000.0
approxTrades = math.floor(barsAvail / math.max(holdBars, 1))
barRangePct = close > 0 ? (high - low) / close * 100.0 : na
medRangePct = ta.percentile_linear_interpolation(barRangePct, lookback, 50)
roundTripPct = (commPct + slipPct) * 2.0
costShare = medRangePct > 0 ? roundTripPct / medRangePct * 100.0 : na
gapPct = close[1] > 0 ? math.abs(open - close[1]) / close[1] * 100.0 : na
avgGapPct = ta.sma(gapPct, lookback)
headVsCost = roundTripPct > 0 ? avgGapPct / roundTripPct : na
zeroVolBars = math.sum(nz(volume, 0) == 0 ? 1 : 0, lookback)
bigGapBars = math.sum(math.abs(open - close[1]) > atr * gapMult ? 1 : 0, lookback)
atrPct = close > 0 ? atr / close * 100.0 : na
p33 = ta.percentile_linear_interpolation(atrPct, lookback, 33)
p67 = ta.percentile_linear_interpolation(atrPct, lookback, 67)
regimeNow = na(atrPct) ? "n/a" : atrPct <= p33 ? "Low" : atrPct <= p67 ? "Mid" : "High"
shareHigh = math.sum(atrPct > p67 ? 1 : 0, lookback) / lookback * 100.0
shareLow = math.sum(atrPct <= p33 ? 1 : 0, lookback) / lookback * 100.0
okCol = color.new(#2E7D32, 0)
warnCol = color.new(#B26A00, 0)
badCol = color.new(#B3261E, 0)
bgCol = color.new(#0E1526, 10)
txtCol = color.new(#E6EAF2, 0)
hdrCol = color.new(#2F6FED, 0)
f_flagCol(bool bad, bool warn) => bad ? badCol : warn ? warnCol : okCol
f_flagTxt(bool bad, bool warn) => bad ? "CHECK" : warn ? "WATCH" : "OK"
tradesBad = approxTrades < minTrades
costBad = not na(costShare) and costShare > costWarn
costWarnF = not na(costShare) and costShare > costWarn / 2.0
headBad = not na(headVsCost) and headVsCost > 1.0
volBad = zeroVolBars > 0
gapWarnF = bigGapBars > 0
regimeBad = shareHigh > 60.0 or shareLow > 60.0
tblPos = posIn == "Top left" ? position.top_left : posIn == "Bottom right" ? position.bottom_right : posIn == "Bottom left" ? position.bottom_left : position.top_right
tblSz = szIn == "Tiny" ? size.tiny : szIn == "Normal" ? size.normal : size.small
var table t = table.new(tblPos, 3, 10, border_width = 1, border_color = color.new(#2F6FED, 70))
f_row(int r, string label, string val, color c, string flag) =>
table.cell(t, 0, r, label, text_color = txtCol, text_size = tblSz, text_halign = text.align_left, bgcolor = bgCol)
table.cell(t, 1, r, val, text_color = txtCol, text_size = tblSz, text_halign = text.align_right, bgcolor = bgCol)
table.cell(t, 2, r, flag, text_color = color.white, text_size = tblSz, text_halign = text.align_center, bgcolor = c)
if barstate.islast
table.cell(t, 0, 0, "BACKTEST INTEGRITY", text_color = color.white, text_size = tblSz, text_halign = text.align_left, bgcolor = hdrCol)
table.cell(t, 1, 0, str.tostring(barsAvail) + " bars", text_color = color.white, text_size = tblSz, text_halign = text.align_right, bgcolor = hdrCol)
table.cell(t, 2, 0, str.tostring(yearsData, "#.0") + "y", text_color = color.white, text_size = tblSz, text_halign = text.align_center, bgcolor = hdrCol)
f_row(1, "Approx trades available", str.tostring(approxTrades), f_flagCol(tradesBad, approxTrades < minTrades * 2), f_flagTxt(tradesBad, approxTrades < minTrades * 2))
f_row(2, "Median bar range", str.tostring(medRangePct, "#.###") + "%", bgCol, "")
f_row(3, "Round-trip cost", str.tostring(roundTripPct, "#.###") + "%", bgCol, "")
f_row(4, "Cost / median bar", na(costShare) ? "n/a" : str.tostring(costShare, "#.#") + "%", f_flagCol(costBad, costWarnF), f_flagTxt(costBad, costWarnF))
f_row(5, "One-bar head start", na(avgGapPct) ? "n/a" : str.tostring(avgGapPct, "#.###") + "%", bgCol, "")
f_row(6, "Head start / cost", na(headVsCost) ? "n/a" : str.tostring(headVsCost, "#.##") + "x", f_flagCol(headBad, false), f_flagTxt(headBad, false))
f_row(7, "Zero-volume bars", str.tostring(zeroVolBars), f_flagCol(volBad, false), f_flagTxt(volBad, false))
f_row(8, "Gaps > " + str.tostring(gapMult, "#.#") + "x ATR", str.tostring(bigGapBars), f_flagCol(false, gapWarnF), f_flagTxt(false, gapWarnF))
f_row(9, "Volatility regime now", regimeNow + " (hi " + str.tostring(shareHigh, "#") + "% / lo " + str.tostring(shareLow, "#") + "%)", f_flagCol(regimeBad, false), f_flagTxt(regimeBad, false))
plotshape(showGaps and math.abs(open - close[1]) > atr * gapMult, title = "Gap bar", style = shape.triangleup, location = location.belowbar, size = size.tiny, color = color.new(#B26A00, 20))
alertcondition(costBad, title = "Cost exceeds threshold share of a median bar", message = "Backtest Integrity Scanner: round-trip cost now exceeds the configured share of a median bar range on this chart.")
alertcondition(headBad, title = "One-bar head start worth more than costs", message = "Backtest Integrity Scanner: the average one-bar gap on this chart is larger than a full round-trip cost.")
alertcondition(volBad, title = "Zero-volume bars present in sample", message = "Backtest Integrity Scanner: zero-volume bars are present in the sample window on this chart.")
alertcondition(regimeBad, title = "Sample dominated by one volatility regime", message = "Backtest Integrity Scanner: the sample window is dominated by a single volatility regime on this chart.")
Overview
Taught to Trade - Backtest Integrity Scanner evaluates trading costs like commission and slippage against median bar ranges, detects zero-volume bars, and flags price gaps exceeding specified ATR multiples. Additionally, it analyzes volatility regime distributions over a customizable lookback window. All diagnostic checks are presented in an overlay table alongside visual chart markers for flagged price gaps, helping traders identify data quality issues and cost hurdles without generating trading signals.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Commission per side (%)
float
0.04
commPct
Slippage per side (%)
float
0.02
slipPct
Sample window (bars)
int
500
lookback
Assumed bars held per trade
int
20
holdBars
Minimum trades you would accept
int
30
minTrades
ATR length
int
14
atrLen
Flag gaps larger than (x ATR)
float
2.0
gapMult
Warn when cost exceeds this % of a median bar
float
25.0
costWarn
Mark flagged gap bars on the chart
bool
true
showGaps
Position
string
Top right
posIn
Text size
string
Small
szIn
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver
This multi-symbol indicator evaluates a combined trading condition across up to 40 user-defined symbols. It processes MACD bullish crossovers, RSI thresholds, Bollinger Band boundaries, and DMI/ADX trend filters using security calls. When buy or sell criteria
This indicator calculates theoretical options contract values and in-the-money probabilities using historical realized volatility across multiple standard deviations. It displays these metrics in a table overlay directly on the price chart. Users can adjust th
This indicator maps market structure and supply and demand zones on the price chart. It marks validated and optional unvalidated swing highs and lows using a pivot length and extra confirmation bars. Consolidation and impulse moves are measured against ATR to
Storm Gauge calculates annualized realized volatility and compares it against a historical percentile lookback window to classify market volatility regimes into calm, normal, or storm conditions. Based on a target volatility input, it calculates dynamic positi
The 8zz Contrarian Indicator plots reverse sentiment trading signals directly on the price chart based on tracked retail post activity. It generates bullish signals during retail panic and bearish signals during market euphoria. The indicator features adjustab