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], orbar.close[1]. Under the hood,make_seriesproxies clamp bounds and returnNaNfor 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).
// 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
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
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: Writevar color biasColor = if close > open ... else ...orregime = 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_seriesat runtime and caught statically when//@strict-no-repaintis 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.
enum TrendState
bull
bear
type SwingPivot
int barIdx
float price
method isHigherThan(SwingPivot this, float level) =>
this.price > level