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

Technical Analysis (ta.*) APIs

The ta.* namespace provides a massive library of built-in mathematical and technical indicator functions—accessible in both JavaScript (ctx.ta.* / ta.*) and Direct Pine Mode—plus expanded v1.0.6 adapters, gap-aware ta.tr, non-repainting pivot overloads, and math.* / str.* stdlib helpers.

How to Use TA APIs

The ta.* library functions (accessed via ctx.ta.* or globally as ta.*) provide high-performance vectorized calculations. In JS mode, they take either a numeric series array (e.g. ctx.bars.map(b => b.close)) or the raw OHLCV bars array (ctx.bars) and return computed arrays aligned with ctx.bars.

⚠️ Critical Rule in JS Mode (Numeric Array vs. Bar Objects):
  • Numeric Series Functions (e.g., ta.ema, ta.sma, ta.rsi, ta.macd, ta.bollingerBands) require an array of numbers like (ctx.bars || []).map(b => b.close) in JS mode.
  • OHLCV Bar Functions (e.g., ta.tr, ta.supertrend, ta.atr, ta.kc, ta.stochastic, ta.ichimoku) require the full ctx.bars array in JS mode because they read high, low, open, and close across bars (including close[1] gap checks).

1. Single-Value Return (e.g. EMA, RSI)

BacktestX Script
// Extract numeric array of closing prices
const closes = (ctx.bars || []).map(b => b.close);

// Single array output
const ema20 = ctx.ta.ema(closes, 20);
const rsi14 = ctx.ta.rsi(closes, 14);

ctx.plot(ema20, { title: 'EMA (20)', color: '#2962ff', width: 2 });

2. Destructuring Multi-Value Returns (Supertrend, Bollinger Bands, MACD)

Multi-output indicators return an object containing multiple aligned series arrays:

BacktestX Script
// Supertrend (requires ctx.bars, factor, period)
const { supertrend, direction } = ctx.ta.supertrend(ctx.bars, 3, 10);

// Bollinger Bands (requires numeric array, period, stdDev)
const { mid, upper, lower } = ctx.ta.bollingerBands(closes, 20, 2);

// MACD (requires numeric array, fast, slow, signal)
const { line, signal, hist } = ctx.ta.macd(closes, 12, 26, 9);

// Ichimoku Cloud (requires ctx.bars)
const { tenkan, kijun, senkouA, senkouB, chikou } = ctx.ta.ichimoku(ctx.bars);

Moving Averages

Function Description
ta.sma(source, length) Simple Moving Average
ta.ema(source, length) Exponential Moving Average
ta.rma(source, length) Running Moving Average (used in RSI)
ta.wma(source, length) Weighted Moving Average
ta.vwma(source, volume, length) Volume-Weighted Moving Average (Pine overload ta.vwma(source, length) supported)
ta.hma(source, length) Hull Moving Average
ta.kama(source, length) Kaufman Adaptive Moving Average (v1.0.6)
ta.alma(source, length, offset, sigma) Arnaud Legoux Moving Average
ta.linreg(source, length, offset) Linear Regression Curve
ta.dema(source, length) Double Exponential Moving Average
ta.tema(source, length) Triple Exponential Moving Average

Oscillators & Volatility

Function Description
ta.tr / ctx.ta.tr(bars, handle_na) True Range factoring previous bar close[1] gaps (max(high-low, |high-prevClose|, |low-prevClose|)) (v1.0.6)
ta.rsi(source, length) Relative Strength Index
ta.macd(source, fast, slow, sig) MACD { line, signal, hist }
ta.stochastic(bars, k, d) Stochastic { kVals, dVals }
ta.dmi(diLen, adxSmooth) Directional Movement Index [+DI, -DI, ADX] (v1.0.6)
ta.sar(start, inc, max) Parabolic Stop and Reverse (SAR) (v1.0.6)
ta.tsi(source, short, long) True Strength Index
ta.cci(bars, length) Commodity Channel Index
ta.mfi(bars, length) Money Flow Index
ta.wpr(bars, length) Williams %R
ta.atr(bars, length) Average True Range
ta.donchian(bars, len) / ta.dc Donchian Channels (v1.0.6)
ta.bollingerBands(source, len, mult) Bollinger Bands { mid, upper, lower }
ta.kc(bars, len, mult) Keltner Channels { mid, upper, lower }
ta.supertrend(bars, factor, period) Supertrend { supertrend, direction }

Logic & Lookbacks

Function Description
ta.crossover(src1, src2) True if src1 crosses above src2
ta.crossunder(src1, src2) True if src1 crosses below src2
ta.cross(src1, src2) True if src1 crosses src2 in either direction
ta.rising(src, length) True if value is strictly rising for length bars
ta.falling(src, length) True if value is strictly falling for length bars
ta.highest(src, length) Highest value over length bars
ta.lowest(src, length) Lowest value over length bars
ta.highestbars(src, length) Bars since highest value over length bars
ta.lowestbars(src, length) Bars since lowest value over length bars
ta.barssince(conditionArray) Number of bars since condition was true
ta.valuewhen(condArray, src, occ) Value of src when condition was true
ta.pivothigh([src,] left, right) Non-repainting pivot high confirmed at +right bars; defaults to high in 2-arg form (v1.0.6)
ta.pivotlow([src,] left, right) Non-repainting pivot low confirmed at +right bars; defaults to low in 2-arg form (v1.0.6)

Statistical Math

Function Description
ta.stdev(source, length) Standard Deviation
ta.variance(source, length) Statistical Variance
ta.correlation(src1, src2, len) Correlation coefficient between two series
ta.percentrank(source, length) Percentile rank of current value
ta.cum(source) Cumulative sum
ta.offset(source, length) Shifts series forward by length

Extended v1.0.6 TA Adapters

BacktestX v1.0.6 also wires specialized TA helpers across ctx.ta.* and Direct Pine Mode:

ta.lsma · Least Squares Moving Avg
ta.lrc · Linear Regression Curve
ta.mcg · McGinley Dynamic
ta.cks · Chande Kroll Stop
ta.ce · Chandelier Exit
ta.fisher · Fisher Transform
ta.crsi · Connors RSI
ta.cvd · Cumulative Volume Delta
ta.bop · Balance of Power
ta.cho · Chaikin Oscillator
ta.ci · Choppiness Index
ta.bbtrend / ta.bbpb / ta.bbw
ta.env · Envelopes & ta.hv · Historical Vol
ta.bbp · Bull Bear Power & ta.cc · Coppock
ta.efi · Elder Force Index & ta.eom
ta.adr · Average Daily Range

math.* & str.* Standard Library (v1.0.6)

Direct Pine Mode completes missing math and string utilities for complex formulas and custom formatting:

  • math.*: math.pow, math.sqrt, math.log, math.exp, math.sign, trigonometric functions, and math.pi.
  • str.*: str.trim, str.startswith, str.endswith, str.replace, and str.format_time. See Direct Pine Mode: Stdlib.