Zero-Repaint Auditing, Source Maps & Raw JS Interop
Standard Pine Script silently allows future-bar leakage when strategies use barmerge.lookahead_on or negative series offsets. BacktestX v1.0.6 introduces compile-time zero-repaint auditing (//@strict-no-repaint), proxy-enforced series bounds checks, Pine-to-JS line source maps, and hybrid //@js-raw JavaScript blocks.
1. Compile-Time Zero-Repaint Directive (//@strict-no-repaint)
During transpilation, the linter inspects every unmasked statement for repaint and future-leak patterns. By default, suspicious patterns emit gutter warnings (severity: 'warn'). Adding //@strict-no-repaint anywhere in your script elevates every violation to a blocking compile error (severity: 'error') with the exact Pine source line number.
| Diagnostic Code | Detected Pattern | Default → Strict Severity | Why It Matters |
|---|---|---|---|
| lookahead-on | barmerge.lookahead_on | warn → error | Reads finalized future HTF bar close prices before the higher-timeframe candle actually closes. |
| negative-offset | close[-1], high[-2] | warn → error | Attempts forward lookback across unreleased bars in replay simulations. |
| future-bar-index | series[bar_index + N] | warn → error | Forward-indexes a series past the current bar during evaluation. |
//@version=5
//@strict-no-repaint
indicator("Audit Clean MTF", overlay=true)
// Safe: lookahead_off prevents reading future higher-timeframe closes
htfClose = request.security(syminfo.tickerid, "60", close[1], lookahead=barmerge.lookahead_off)
plot(htfClose, title="1H Confirmed Close", color=color.blue)
2. Runtime Proxy Bounds (make_series) & Confirmed Pivots
Even without //@strict-no-repaint, every built-in OHLCV series and history-tracked variable is wrapped in a runtime make_series ES6 Proxy inside the bar loop:
- Hard Future Read Guard: Reading any index where
idx > barIdxoridx < 0dynamically returnsNaNinstead of peeking at later bars in the array. - Right-Shifted Pivot Confirmation: Both 2-argument (
ta.pivothigh(left, right)) and 3-argument (ta.pivothigh(src, left, right)) pivot calls write the detected pivot onto indexpivotIdx + rightLen—matching TradingView's real-time confirmation bar with zero repainting.
3. Pine Line Source Maps (transpilePineToJSWithMap)
Because generated JavaScript prepends helper runtime shims before the bar loop, raw JS stack line numbers do not match your Pine editor lines. Version 1.0.6 exports transpilePineToJSWithMap(pineCode) returning:
{
code: "...", // Transpiled JS output
sourceMap: { "142": 12 }, // Maps transpiled JS line -> original Pine line
diagnostics: [ ... ], // { line, severity, code, message }
strictNoRepaint: true,
error: null // Or Error with `.line` pointing to Pine source line
}
When runLiveLint or runtime execution hits a syntax error or zero-repaint error, the Script Editor walks backward through sourceMap to underline and tooltip the exact Pine source line in the red error gutter.
4. Inline Raw JavaScript & Canvas 2D Blocks (//@js-raw) & request.timeframe_data
Need HTML5 Canvas 2D gradients, Bezier splines, DOM helpers, or direct ctx APIs inside a Pine Script? Wrap JavaScript statements between //@js-raw and //@end-js-raw. Lines inside that block bypass Pine rewriting completely while still preserving exact line numbers in sourceMap. You can also query BacktestX's multi-timeframe indicator cache via request.timeframe_data(symbol, tf, indName, ...args).
//@version=5
indicator("Hybrid Canvas Glow", overlay=true)
ema20 = ta.ema(close, 20)
plot(ema20, title="EMA 20", color=color.blue, linewidth=2)
htfRsi = request.timeframe_data(syminfo.tickerid, "1H", "rsi", 14)
if barstate.islast
log.info("Latest 1H RSI={0}", htfRsi[bar_index])
//@js-raw
const x = ctx.barToX(barIdx);
const y = ctx.priceToY(close[barIdx]);
ctx.save();
ctx.fillStyle = "rgba(41, 98, 255, 0.25)";
ctx.beginPath();
ctx.arc(x, y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
//@end-js-raw