Skip to content

Technical Analysis

Kind: module

Namespace: ta

Provides functions for technical analysis, including moving averages, statistical calculations, and series manipulations.

ta.sum()

Kind: func

Calculates the sum of the last length values.

Syntax

ta.sum(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the sum.

Notes

  • NaN values are ignored
  • Returns NaN until length bars are available

Example

javascript
function onTick() {
    const totalVolume = ta.sum(volume, 20);
    spline(totalVolume, {title: "20-bar Volume Sum"});
}

ta.cum()

Kind: func

Calculates the cumulative sum of a series.

Syntax

ta.cum(source) → series
ta.cum(number) → series

Arguments

NameTypeDescription
sourceseries, doubleSource series or value

Returns

A series containing the cumulative sum.

Notes

  • NaN values are treated as 0

Example

javascript
function onTick() {
    const cumVolume = ta.cum(volume);
    spline(cumVolume, {title: "Cumulative Volume"});
}

ta.sma()

Kind: func

Calculates the Simple Moving Average (SMA).

Syntax

ta.sma(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the SMA.

Notes

  • NaN values are ignored; the average is calculated over non-NaN values only
  • Returns NaN until length bars are available

Example

javascript
function onTick() {
    const sma20 = ta.sma(close, 20);
    spline(sma20, {title: "SMA 20"});
}

ta.ema()

Kind: func

Calculates the Exponential Moving Average (EMA).

Syntax

ta.ema(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the EMA.

Notes

  • Returns NaN if the current source value is NaN
  • Returns NaN until length bars are available
  • Uses SMA for initial length bars, then switches to EMA formula

Example

javascript
function onTick() {
    const ema20 = ta.ema(close, 20);
    spline(ema20, {title: "EMA 20"});
}

ta.wma()

Kind: func

Calculates the Weighted Moving Average (WMA).

Syntax

ta.wma(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the WMA.

Notes

  • NaN values are ignored; weights are assigned by position, not by value count
  • Returns NaN until length bars are available

Example

javascript
function onTick() {
    const wma20 = ta.wma(close, 20);
    spline(wma20, {title: "WMA 20"});
}

ta.wima()

Kind: func

Calculates the Wilder's Smoothing Moving Average (WIMA).

Syntax

ta.wima(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the WIMA.

Notes

  • Returns NaN if the current source value is NaN
  • Returns NaN until length bars are available
  • Uses SMA for initial length bars, then switches to WIMA formula

Example

javascript
function onTick() {
    const wima20 = ta.wima(close, 20);
    spline(wima20, {title: "WIMA 20"});
}

ta.highest()

Kind: func

Returns the highest value in the last length bars.

Syntax

ta.highest(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the highest value.

Notes

  • NaN values are ignored
  • Returns NaN until length bars are available
  • Returns NaN if all values in the window are NaN

Example

javascript
function onTick() {
    const high20 = ta.highest(high, 20);
    spline(high20, {title: "20-bar High"});
}

ta.highestBar()

Kind: func

Returns the bar offset to the highest value in the last length bars.

Syntax

ta.highestBar(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the bar offset to the highest value (0 = current bar).

Notes

  • NaN values are ignored
  • Returns NaN until length bars are available
  • Returns NaN if all values in the window are NaN

Example

javascript
function onTick() {
    const offset = ta.highestBar(high, 20);
    const highestValue = isNaN(offset) ? NaN : high[offset];
    spline(highestValue, {title: "20-bar High"});
}

ta.lowest()

Kind: func

Returns the lowest value in the last length bars.

Syntax

ta.lowest(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the lowest value.

Notes

  • NaN values are ignored
  • Returns NaN until length bars are available
  • Returns NaN if all values in the window are NaN

Example

javascript
function onTick() {
    const low20 = ta.lowest(low, 20);
    spline(low20, {title: "20-bar Low"});
}

ta.lowestBar()

Kind: func

Returns the bar offset to the lowest value in the last length bars.

Syntax

ta.lowestBar(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the bar offset to the lowest value (0 = current bar).

Notes

  • NaN values are ignored
  • Returns NaN until length bars are available
  • Returns NaN if all values in the window are NaN

Example

javascript
function onTick() {
    const offset = ta.lowestBar(low, 20);
    const lowestValue = isNaN(offset) ? NaN : low[offset];
    spline(lowestValue, {title: "20-bar Low"});
}

ta.pivotHigh()

Kind: func

Returns the pivot high value if found.

Syntax

ta.pivotHigh(source, left, right) → series

Arguments

NameTypeDescription
sourceseriesSource series
leftintNumber of bars to the left
rightintNumber of bars to the right

Returns

The pivot high value, or NaN if not found.

Notes

  • Returns NaN until left + right + 1 bars are available
  • Returns NaN if no pivot found at current position
  • For plotting, pass offset: -right to spline() (or another drawing call that supports offset) so the marker appears on the candle where the pivot actually occurred, not on the latest bar.

Example

javascript
function onTick() {
    const right = 5;
    const ph = ta.pivotHigh(high, 5, right);
    spline(ph, {title: "Pivot High", style: spline.STYLE_CIRCLES, offset: -right});
}

ta.pivotLow()

Kind: func

Returns the pivot low value if found.

Syntax

ta.pivotLow(source, left, right) → series

Arguments

NameTypeDescription
sourceseriesSource series
leftintNumber of bars to the left
rightintNumber of bars to the right

Returns

The pivot low value, or NaN if not found.

Notes

  • Returns NaN until left + right + 1 bars are available
  • Returns NaN if no pivot found at current position
  • For plotting, pass offset: -right to spline() (or another drawing call that supports offset) so the marker appears on the candle where the pivot actually occurred, not on the latest bar.

Example

javascript
function onTick() {
    const right = 5;
    const pl = ta.pivotLow(low, 5, right);
    spline(pl, {title: "Pivot Low", style: spline.STYLE_CIRCLES, offset: -right});
}

ta.tr()

Kind: func

Calculates the True Range (TR) for the current bar.

Syntax

ta.tr() → series

Arguments

None

Returns

A series containing the True Range value for the current bar.

Notes

  • True Range is the maximum of (high-low, abs(high - close[1]), abs(low-close[1]))
  • Might return NaN if some of the above sources are not available

Example

javascript
function onTick() {
    const tr = ta.tr();
    spline(tr, {title: "True Range"});
}

ta.atr()

Kind: func

Calculates the Average True Range (ATR) over a specified period.

Syntax

ta.atr(length) → series

Arguments

NameTypeDescription
lengthintNumber of bars

Returns

A series containing the Average True Range(ATR) value for the current bar.

Notes

  • ATR is calculated as the Wilder's Moving Average (WIMA) of the True Range (TR) over the specified period
  • You can use ta.tr() to get the True Range series for custom ATR calculations
  • Returns NaN until length bars are available

Example

Default ATR calculation:

javascript
function onTick() {
    const atr14 = ta.atr(14);
    spline(atr14, {title: "ATR (14)"});
}

ATR using Simple Moving Average instead of WIMA:

javascript
function onTick() {
    const tr = ta.tr();
    const atr14 = ta.sma(tr, 14);
    spline(atr14, {title: "ATR (14) with SMA"});
}

ta.wpr()

Kind: func

Calculates the Williams Percent Range (%R) for the current bar.

Syntax

ta.wpr(period) → series

Arguments

NameTypeDescription
periodintNumber of bars

Returns

A series containing the %R values.

Notes

  • Formula: %R = (Close - Highest High) / (Highest High - Lowest Low) × 100
  • Uses an expanding window: produces values from the first bar, widening the lookback until period bars are available
  • Returns NaN if the highest high equals the lowest low (zero-range bar)

Example

javascript
function onTick() {
    const wr = ta.wpr(14);
    spline(wr, {title: "Williams %R (14)"});
}

ta.rsi()

Kind: func

Calculates the Relative Strength Index (RSI) for the current bar.

Syntax

ta.rsi(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the RSI values.

Notes

  • Formula: change = source[0] - source[1]; RSI = 50 × (1 + wima(change) / wima(|change|))
  • Uses Wilder's smoothing (WIMA) for both average gain and average loss components
  • Returns NaN until length + 1 bars are available
  • Returns NaN when the smoothed absolute change is zero

Example

javascript
function onTick() {
    const rsi = ta.rsi(close, 14);
    spline(rsi, {title: "RSI (14)", color: color.BLUE});
}

ta.sar()

Kind: func

Calculates the Parabolic Stop and Reverse (SAR) for the current bar.

Syntax

ta.sar() → series
ta.sar(start, increment, maximum) → series

Arguments

NameTypeDescription
startdoubleInitial acceleration factor (default: 0.02)
incrementdoubleAcceleration factor increment (default: 0.02)
maximumdoubleMaximum acceleration factor (default: 0.2)

Returns

A series containing the SAR values.

Notes

  • Values are clamped to the previous two bars' extremes and seeded on the second bar from the close direction.
  • The no-argument form uses defaults: start 0.02, increment 0.02, maximum 0.2
  • Returns NaN on the first bar (no prior bar to seed the trend)

Example

javascript
function onTick() {
    const sar = ta.sar();
    spline(sar, {title: "SAR", style: spline.STYLE_CIRCLES, color: color.YELLOW});
}

ta.supertrend()

Kind: func

Calculates the SuperTrend indicator line and trend direction for the current bar.

Syntax

ta.supertrend(factor, atrPeriod) → [line, direction]

Arguments

NameTypeDescription
factordoubleATR multiplier
atrPeriodintATR period (Wilder's smoothing/WIMA)

Returns

An array of two series: [line, direction].

  • line: SuperTrend line value
  • direction: Trend direction — -1 for uptrend, 1 for downtrend

Notes

  • ATR is calculated as WIMA of True Range (ta.atr path)
  • Upper and lower bands are ratcheted using the previous bar's values
  • Returns NaN until enough bars are available for ATR calculation

Example

javascript
function onTick() {
    const [st, dir] = ta.supertrend(3.0, 10);
    spline(dir < 0 ? st : NaN, {title: "Up direction", color: color.GREEN, style: spline.STYLE_LINE_BREAK});
    spline(dir > 0 ? st : NaN, {title: "Down direction", color: color.RED, style: spline.STYLE_LINE_BREAK});
}

ta.stdev()

Kind: func

Calculates the Standard Deviation of the last length values.

Syntax

ta.stdev(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the standard deviation for the current bar.

Notes

  • Uses the population standard deviation formula:
    • stdev = sqrt( ( Σ(x^2) - (Σx)^2 / n ) / n ), where n = length
  • Returns NaN until length bars are available
  • If any value in the lookback window is NaN, the result is NaN

Example

javascript
function onTick() {
    const sd20 = ta.stdev(close, 20);
    spline(sd20, {title: "StdDev (20)", color: color.RED});
}

ta.dmi()

Kind: func

Calculates the Directional Movement Index (DMI) indicators: Plus Directional Indicator (+DI), Minus Directional Indicator (-DI), and Average Directional Index (ADX).

Syntax

ta.dmi(length) → [plusDI, minusDI, adx]

Arguments

NameTypeDescription
lengthintNumber of bars

Returns

An array of three series: [plusDI, minusDI, adx].

  • plusDI: Plus Directional Indicator series
  • minusDI: Minus Directional Indicator series
  • adx: Average Directional Index series

Notes

  • DMI is a trend strength indicator composed of +DI, -DI, and ADX
  • All series propagate NaN if any required value is not available
  • Returns NaN until enough bars are available for the calculation
  • +DI and -DI are calculated using Wilder's smoothing (WIMA)
  • ADX is the smoothed average of the DX (difference index)

Example

javascript
function onTick() {
    const [plusDI, minusDI, adx] = ta.dmi(14);
    spline(plusDI, {title: "+DI", color: color.GREEN});
    spline(minusDI, {title: "-DI", color: color.RED});
    spline(adx, {title: "ADX", color: color.BLUE});
}

ta.lreg()

Kind: func

Calculates the value of the linear regression line at the most recent bar over a rolling window.

Syntax

ta.lreg(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the value of the linear regression line at the most recent bar for each window.

Notes

  • Returns NaN until length bars are available
  • If any source value in the window is NaN, the result is NaN for that bar
  • The result is the value of the regression line at the most recent (rightmost) bar in the window

Example

javascript
function onTick() {
    const lreg = ta.lreg(close, 20);
    spline(lreg, {title: "Linear Regression (20)"});
}

ta.dev()

Kind: func

Calculates the Mean Absolute Deviation (MAD) of the last length values.

Syntax

ta.dev(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the mean absolute deviation.

Notes

  • Uses the population MAD formula: dev = Σ|x - mean| / length, where mean = sma(source, length)
  • Returns NaN until length bars are available
  • If any value in the loopback window is NaN, the result is NaN

Example

javascript
function onTick() {
    const deviation = ta.dev(close, 20);
    spline(deviation, {title: "Mean Abs Deviation (20)"});
}

ta.cci()

Kind: func

Calculates the Commodity Channel Index (CCI) for the current bar.

Syntax

ta.cci(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns A series containing the CCI value for the current bar.

Notes

  • CCI is calculated using the formula: CCI = (source - sma(source, length)) / (0.015 * dev(source, length))
  • If any value in the loopback window is NaN, the result is NaN

Example

javascript
function onTick() {
    const cci = ta.cci(hlc3, 20);
    spline(cci, {title: "CCI (20)"});
}

ta.cross()

Kind: func

Detects whether two series crossed each other on the current bar (in either direction).

Syntax

ta.cross(source1, source2) → bool
ta.cross(source1, constant) → bool

Arguments

NameTypeDescription
source1seriesFirst data series
source2series, numberSecond data series or constant line

Returns

true if the two series crossed each other on the current bar, otherwise false.

Notes

  • Equivalent to ta.crossover(source1, source2) || ta.crossunder(source1, source2)
  • Returns false if either series has fewer than two bars or any required value is NaN

Example

RSI midline cross — highlight when RSI crosses the 50 level in either direction:

javascript
indicator({overlay: false});

const length = input("length", 14);

function onTick() {
    const rsi = ta.rsi(close, length);

    spline(rsi, {title: "RSI", color: color.BLUE});
    spline(70, {title: "Overbought", color: color.RED});
    spline(30, {title: "Oversold", color: color.GREEN});
    spline(ta.cross(rsi, 50) ? rsi : NaN, {title: "Midline cross",
        style: spline.STYLE_CIRCLES,
        color: color.YELLOW,
    });
}

ta.crossover()

Kind: func

Detects whether source1 crossed over source2 on the current bar: the value of source1 is greater than source2, and on the previous bar source1 was less than or equal to source2.

Syntax

ta.crossover(source1, source2) → bool
ta.crossover(source1, constant) → bool

Arguments

NameTypeDescription
source1seriesFirst data series
source2series, numberSecond data series or constant line

Returns

true if source1 crossed over source2, otherwise false.

Notes

  • Returns false if either series has fewer than two bars or any required value is NaN

Example

Golden cross — mark when the short MA crosses above the long MA:

javascript
const shortLength = input("shortLength", 50);
const longLength = input("longLength", 200);

function onTick() {
    const maShort = ta.sma(close, shortLength);
    const maLong = ta.sma(close, longLength);

    spline(maShort, {title: "MA Short", color: color.GREEN});
    spline(maLong, {title: "MA Long", color: color.RED});
    spline(ta.crossover(maShort, maLong) ? maShort : NaN, {
        title: "Golden cross",
        style: spline.STYLE_CIRCLES,
        color: color.YELLOW,
    });
}

ta.crossunder()

Kind: func

Detects whether source1 crossed under source2 on the current bar: the value of source1 is less than source2, and on the previous bar source1 was greater than or equal to source2.

Syntax

ta.crossunder(source1, source2) → bool
ta.crossunder(source1, constant) → bool

Arguments

NameTypeDescription
source1seriesFirst data series
source2series, numberSecond data series or constant line

Returns

true if source1 crossed under source2, otherwise false.

Notes

  • Returns false if either series has fewer than two bars or any required value is NaN

Example

Death cross — mark when the short MA crosses below the long MA:

javascript
const shortLength = input("shortLength", 50);
const longLength = input("longLength", 200);

function onTick() {
    const maShort = ta.sma(close, shortLength);
    const maLong = ta.sma(close, longLength);

    spline(maShort, {title: "MA Short", color: color.GREEN});
    spline(maLong, {title: "MA Long", color: color.RED});
    spline(ta.crossunder(maShort, maLong) ? maShort : NaN, {
        title: "Death cross",
        style: spline.STYLE_CIRCLES,
        color: color.YELLOW,
    });
}