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.vwma() ​

Kind: func

Calculates the Volume Weighted Moving Average (VWMA).

Syntax

ta.vwma(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the VWMA.

Notes

  • Uses chart volume for weighting
  • NaN price or volume values in the window are skipped
  • Returns NaN until length bars are available
  • Returns NaN when total volume in the window is zero

Example

javascript
function onTick() {
    const vwma20 = ta.vwma(close, 20);
    spline(vwma20, {title: "VWMA 20"});
}

ta.swma() ​

Kind: func

Calculates the Symmetrically Weighted Moving Average (SWMA) over the last four bars.

Syntax

ta.swma(source) → series

Arguments

NameTypeDescription
sourceseriesSource series

Returns

A series containing the SWMA.

Notes

  • Returns NaN until four bars are available

Example

javascript
function onTick() {
    const swma = ta.swma(close);
    spline(swma, {title: "SWMA"});
}

ta.alma() ​

Kind: func

Calculates the Arnaud Legoux Moving Average (ALMA).

Syntax

ta.alma(source, length) → series
ta.alma(source, length, offset, sigma) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars
offsetdoubleGaussian offset (default: 0.85)
sigmadoubleGaussian width (default: 6.0)

Returns

A series containing the ALMA.

Notes

  • Uses Gaussian weights centered at offset × (length - 1)
  • Returns NaN until length bars are available
  • Returns NaN if any value in the window is NaN

Example

javascript
function onTick() {
    const alma = ta.alma(close, 9);
    spline(alma, {title: "ALMA (9)"});
}

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: RSI = 50 × (1 + wima(ta.change(source)) / wima(|ta.change(source)|))
  • 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) → [series, series]

Arguments

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

Returns

A tuple of two series: [line, direction].

  • line (series): SuperTrend line value on each bar
  • direction (series): Trend direction on each bar — -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 [line, direction] = ta.supertrend(3.0, 10);
    spline(direction < 0 ? line : NaN, {title: "Up direction", color: color.GREEN, style: spline.STYLE_LINE_BREAK});
    spline(direction > 0 ? line : 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.variance() ​

Kind: func

Calculates the variance of the last length values.

Syntax

ta.variance(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the variance for the current bar.

Notes

  • Equivalent to ta.stdev(source, length)²
  • Uses the population variance (same basis as ta.stdev)
  • Returns 0 when length is 1
  • 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 var20 = ta.variance(close, 20);
    spline(var20, {title: "Variance (20)"});
}

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) → [series, series, series]

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
  • Usually CCI indicator is calculated based on Typical Price (hlc3)

Example

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

ta.change() ​

Kind: func

Returns the difference between the current value and the previous bar.

Syntax

ta.change(source) → series

Arguments

NameTypeDescription
sourceseriesSource series

Returns

A series containing the bar-to-bar change.

Notes

  • Only the 1-bar form is supported (no length argument)
  • Computed as source - source[1]
  • Returns NaN on the first bar or when either value is NaN

Example

javascript
function onTick() {
    const delta = ta.change(close);
    spline(delta, {title: "1-bar Change"});
}

ta.roc() ​

Kind: func

Calculates the Rate of Change (ROC) as a percentage.

Syntax

ta.roc(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the ROC percentage.

Notes

  • Formula: 100 × (source - source[length]) / source[length]
  • Uses an N-bar lookback directly (not ta.change, which is 1-bar only)
  • Returns NaN until length + 1 bars are available
  • Returns NaN when the lookback value is zero or NaN

Example

javascript
function onTick() {
    const roc = ta.roc(close, 12);
    spline(roc, {title: "ROC (12)"});
}

ta.stoch() ​

Kind: func

Calculates the raw Stochastic %K value.

Syntax

ta.stoch(source, length) → series
ta.stoch(source, high, low, length) → series

Arguments

NameTypeDescription
sourceseriesSource series (typically close)
highseriesHigh series (optional; defaults to chart high)
lowseriesLow series (optional; defaults to chart low)
lengthintNumber of bars

Returns

A series containing the %K value (0–100).

Notes

  • Formula: 100 × (source - lowest(low, length)) / (highest(high, length) - lowest(low, length))
  • Result is clamped to the range 0–100
  • Returns NaN until length bars are available
  • Returns NaN when the high-low range is zero

Example

javascript
function onTick() {
    const k = ta.stoch(close, 14);
    spline(k, {title: "Stochastic %K (14)"});
}

ta.tsi() ​

Kind: func

Calculates the True Strength Index (TSI).

Syntax

ta.tsi(source, shortLength, longLength) → series

Arguments

NameTypeDescription
sourceseriesSource series
shortLengthintShort EMA period
longLengthintLong EMA period

Returns

A series containing the TSI values ranging from [-100, 100].

Notes

  • Momentum is ta.change(source)
  • Formula: 100 × ema(ema(momentum, longLength), shortLength) / ema(ema(|momentum|, longLength), shortLength)
  • Returns NaN until enough bars are available for the nested EMAs

Example

javascript
function onTick() {
    const tsi = ta.tsi(close, 13, 25);
    spline(tsi, {title: "TSI (13, 25)"});
}

If you want a signal line, use ta.ema(tsi, signalLength)

javascript
function onTick() {
    const tsi = ta.tsi(close, 13, 25);
    const signal = ta.ema(tsi, 7);
    spline(tsi, {title: "TSI (13, 25)", color: color.BLUE});
    spline(signal, {title: "Signal (7)", color: color.RED});
}

ta.rising() ​

Kind: func

Tests whether the source has risen for length consecutive bars.

Syntax

ta.rising(source, length) → bool

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of consecutive rising bars

Returns

true if the source has risen for length consecutive bars, otherwise false.

Notes

  • Compares each bar to the previous bar's value via lookback (source[0] > source[1] > … > source[length])
  • Returns false when any compared value is NaN
  • Returns false until length + 1 bars are available

Example

javascript
function onTick() {
    const up3 = ta.rising(close, 3);
    spline(up3 ? close : NaN, {title: "3-bar rise", style: spline.STYLE_CIRCLES});
}

ta.falling() ​

Kind: func

Tests whether the source has fallen for length consecutive bars.

Syntax

ta.falling(source, length) → bool

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of consecutive falling bars

Returns

true if the source has fallen for length consecutive bars, otherwise false.

Notes

  • Compares each bar to the previous bar's value via lookback (source[0] < source[1] < … < source[length])
  • Returns false when any compared value is NaN
  • Returns false until length + 1 bars are available

Example

javascript
function onTick() {
    const down3 = ta.falling(close, 3);
    spline(down3 ? close : NaN, {title: "3-bar fall", style: spline.STYLE_CIRCLES});
}

ta.correlation() ​

Kind: func

Calculates the Pearson correlation coefficient between two series over a rolling window.

Syntax

ta.correlation(source1, source2, length) → series

Arguments

NameTypeDescription
source1seriesFirst series
source2seriesSecond series
lengthintNumber of bars

Returns

A series containing the correlation coefficient (−1 to 1).

Notes

  • Uses the standard Pearson formula over the last length bars
  • Returns NaN until length bars are available
  • Returns NaN if any value in either series window is NaN
  • Returns NaN when the denominator is zero

Example

javascript
function onTick() {
    const corr = ta.correlation(close, volume, 20);
    spline(corr, {title: "Close/Volume Correlation (20)"});
}

ta.percentrank() ​

Kind: func

Calculates the percent rank of the current value within a rolling window.

Syntax

ta.percentrank(source, length) → series

Arguments

NameTypeDescription
sourceseriesSource series
lengthintNumber of bars

Returns

A series containing the percent rank (0–100).

Notes

  • Percentage of values in the window source[0..length] that are less than or equal to the current value
  • Formula: (count - 1) × 100 / length, where count is the number of values ≤ current
  • Returns NaN until length + 1 bars are available
  • Returns NaN if any value in the window is NaN

Example

javascript
function onTick() {
    const rank = ta.percentrank(close, 20);
    spline(rank, {title: "Percent Rank (20)"});
}

ta.accdist() ​

Kind: func

Calculates the Accumulation/Distribution index.

Syntax

ta.accdist() → series

Arguments

None

Returns

A series containing the cumulative accumulation/distribution value.

Notes

  • Uses chart high, low, close, and volume
  • Money Flow Multiplier: ((close - low) - (high - close)) / (high - low) (0 when range is zero)
  • Money Flow Volume: multiplier × volume
  • Result is the cumulative sum of money flow volume

Example

javascript
function onTick() {
    const ad = ta.accdist();
    spline(ad, {title: "Accumulation/Distribution"});
}

ta.zigzag() ​

Kind: func

Calculates the ZigZag indicator line by filtering price movements below a deviation threshold.

Syntax

ta.zigzag(deviation, depth) → series

Arguments

NameTypeDescription
deviationdoubleMinimum price change in percent (e.g. 5 = 5%)
depthintMinimum number of bars between pivots

Returns

A series containing the interpolated ZigZag line value.

Notes

  • Uses chart close as the price source
  • Deviation threshold is deviation / 100
  • The line is linearly interpolated between confirmed pivot points
  • Returns NaN until enough bars are available to establish a pivot

Example

javascript
function onTick() {
    const zz = ta.zigzag(5, 12);
    spline(zz, {title: "ZigZag (5%, 12)"});
}

ta.zigzagbars() ​

Kind: func

Returns the bar offset to the most recent confirmed ZigZag pivot.

Syntax

ta.zigzagbars(deviation, depth) → series

Arguments

NameTypeDescription
deviationdoubleMinimum price change in percent (e.g. 5 = 5%)
depthintMinimum number of bars between pivots

Returns

A series containing the negative bar offset to the last pivot (e.g. -5 means 5 bars ago).

Notes

  • Uses the same pivot detection as ta.zigzag()
  • Returns NaN until a confirmed pivot exists

Example

javascript
function onTick() {
    const bars = ta.zigzagbars(5, 12);
    spline(bars, {title: "Bars since ZigZag pivot"});
}

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,
    });
}