Appearance
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the sum.
Notes
NaNvalues are ignored- Returns
NaNuntillengthbars 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series, double | Source series or value |
Returns
A series containing the cumulative sum.
Notes
NaNvalues are treated as0
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the SMA.
Notes
NaNvalues are ignored; the average is calculated over non-NaN values only- Returns
NaNuntillengthbars 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the EMA.
Notes
- Returns
NaNif the current source value isNaN - Returns
NaNuntillengthbars are available - Uses SMA for initial
lengthbars, 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the WMA.
Notes
NaNvalues are ignored; weights are assigned by position, not by value count- Returns
NaNuntillengthbars 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the WIMA.
Notes
- Returns
NaNif the current source value isNaN - Returns
NaNuntillengthbars are available - Uses SMA for initial
lengthbars, 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the highest value.
Notes
NaNvalues are ignored- Returns
NaNuntillengthbars are available - Returns
NaNif all values in the window areNaN
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the bar offset to the highest value (0 = current bar).
Notes
NaNvalues are ignored- Returns
NaNuntillengthbars are available - Returns
NaNif all values in the window areNaN
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the lowest value.
Notes
NaNvalues are ignored- Returns
NaNuntillengthbars are available - Returns
NaNif all values in the window areNaN
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the bar offset to the lowest value (0 = current bar).
Notes
NaNvalues are ignored- Returns
NaNuntillengthbars are available - Returns
NaNif all values in the window areNaN
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| left | int | Number of bars to the left |
| right | int | Number of bars to the right |
Returns
The pivot high value, or NaN if not found.
Notes
- Returns
NaNuntilleft + right + 1bars are available - Returns
NaNif no pivot found at current position - For plotting, pass
offset: -righttospline()(or another drawing call that supportsoffset) 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| left | int | Number of bars to the left |
| right | int | Number of bars to the right |
Returns
The pivot low value, or NaN if not found.
Notes
- Returns
NaNuntilleft + right + 1bars are available - Returns
NaNif no pivot found at current position - For plotting, pass
offset: -righttospline()(or another drawing call that supportsoffset) 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() → seriesArguments
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
NaNif 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| length | int | Number 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
NaNuntillengthbars 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| period | int | Number 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
periodbars are available - Returns
NaNif 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number 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
NaNuntillength + 1bars are available - Returns
NaNwhen 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| start | double | Initial acceleration factor (default: 0.02) |
| increment | double | Acceleration factor increment (default: 0.02) |
| maximum | double | Maximum 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, increment0.02, maximum0.2 - Returns
NaNon 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
| Name | Type | Description |
|---|---|---|
| factor | double | ATR multiplier |
| atrPeriod | int | ATR period (Wilder's smoothing/WIMA) |
Returns
An array of two series: [line, direction].
line: SuperTrend line valuedirection: Trend direction —-1for uptrend,1for downtrend
Notes
- ATR is calculated as WIMA of True Range (
ta.atrpath) - Upper and lower bands are ratcheted using the previous bar's values
- Returns
NaNuntil 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number 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 ), wheren = length
- Returns
NaNuntillengthbars are available - If any value in the lookback window is
NaN, the result isNaN
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
| Name | Type | Description |
|---|---|---|
| length | int | Number of bars |
Returns
An array of three series: [plusDI, minusDI, adx].
plusDI: Plus Directional Indicator seriesminusDI: Minus Directional Indicator seriesadx: Average Directional Index series
Notes
- DMI is a trend strength indicator composed of +DI, -DI, and ADX
- All series propagate
NaNif any required value is not available - Returns
NaNuntil 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the value of the linear regression line at the most recent bar for each window.
Notes
- Returns
NaNuntillengthbars are available - If any source value in the window is
NaN, the result isNaNfor 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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number of bars |
Returns
A series containing the mean absolute deviation.
Notes
- Uses the population MAD formula:
dev = Σ|x - mean| / length, wheremean = sma(source, length) - Returns
NaNuntillengthbars are available - If any value in the loopback window is
NaN, the result isNaN
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) → seriesArguments
| Name | Type | Description |
|---|---|---|
| source | series | Source series |
| length | int | Number 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 isNaN
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) → boolArguments
| Name | Type | Description |
|---|---|---|
| source1 | series | First data series |
| source2 | series, number | Second 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
falseif either series has fewer than two bars or any required value isNaN
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) → boolArguments
| Name | Type | Description |
|---|---|---|
| source1 | series | First data series |
| source2 | series, number | Second data series or constant line |
Returns
true if source1 crossed over source2, otherwise false.
Notes
- Returns
falseif either series has fewer than two bars or any required value isNaN
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) → boolArguments
| Name | Type | Description |
|---|---|---|
| source1 | series | First data series |
| source2 | series, number | Second data series or constant line |
Returns
true if source1 crossed under source2, otherwise false.
Notes
- Returns
falseif either series has fewer than two bars or any required value isNaN
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,
});
}