Exciting Update: Version 1.0.6 is now available, introducing Direct Pine Script Mode, Pine Logs Runtime Bridge, Zero-Repaint Linting, Source Maps, Intrabar MFE/MAE Stats, and expanded Pine v5/v6 Transpiler Parity. Read more
Version: 1.0.6

History & State

History Operator ([n] & ta.offset())

BacktestX v1.0.6 supports both execution models for referencing prior bars:

  • Pine Script & Implicit Loop Mode: Use native historical indexing like close[1], high[2], or bar.close[1]. Under the hood, make_series proxies clamp bounds and return NaN for invalid or future (negative) lookbacks.
  • Vectorized JS Array Mode: When working with whole precomputed indicator arrays outside a per-bar loop, shift an entire series backward using ta.offset(sourceArray, length).
BacktestX Script (Pine / Vectorized JS)
// Pine Mode: bounds-checked per-bar history operator
prevClose = close[1]
gap = open - close[1]

// Vectorized JS Mode: array shift helper
const prevCloses = ctx.ta.offset(closes, 1);

Stateful Condition Tracking

Instead of manually writing state loops to remember when conditions triggered in the past, use the built-in stateful helpers:

ta.valuewhen

BacktestX Script
ta.valuewhen(condArray, sourceArray, occurrence)

Returns the value of sourceArray on the exact bar where condArray was true for the occurrenceth time (0 = most recent, 1 = previous).

ta.barssince

BacktestX Script
ta.barssince(condArray)

Returns the number of bars that have elapsed since the last time condArray was true.

Persistent var / varip & Value Expressions

Variables prefixed with var or varip initialize once on bar 0 and retain their mutated values from bar to bar. In v1.0.6, the transpiler also supports Pine expression assignments and multi-value tuple destructuring:

  • Value-returning if & switch: Write var color biasColor = if close > open ... else ... or regime = switch mode ... cleanly without manual IIFEs.
  • User-function tuple destructuring: Destructure arrays returned by your own Pine helper functions with [upper, mid, lower] = calcBands(close, 20).
  • Zero-repaint guards: Out-of-bounds or negative offsets are intercepted by make_series at runtime and caught statically when //@strict-no-repaint is enabled.

User-Defined Types, Enums & Methods

Store structured per-bar objects using Pine v5/v6 type records, enum members, and dot-called method functions with automatic this binding. See Direct Pine Mode: UDTs, Enums & Methods for full syntax and examples.

Pine Script (UDT & Enum)
enum TrendState
    bull
    bear

type SwingPivot
    int barIdx
    float price

method isHigherThan(SwingPivot this, float level) =>
    this.price > level