Technical Analysis
Moving averages, oscillators, volatility, and crossover detection.
Moving Averages
ta.sma
ta.sma(value, period) -> numberSimple moving average — arithmetic mean of the last period values.
| Param | Type | Description |
|---|---|---|
value | number | Input value (e.g. candles.close()) |
period | number | Lookback window |
ta.ema
ta.ema(value, period) -> numberExponential moving average — gives more weight to recent values. Returns NaN when insufficient history.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.rma
ta.rma(value, period) -> numberWilder's smoothed moving average (used internally by RSI, ATR). Returns NaN when insufficient history.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.alma
ta.alma(value, length, offset, sigma, floor?) -> numberArnaud Legoux moving average. Returns the latest cached value when value is NaN.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Input value |
length | number | — | Lookback window |
offset | number | — | Center offset in the weighting curve |
sigma | number | — | Gaussian width parameter |
floor | boolean | false | Floors the computed offset before weighting |
ta.swma
ta.swma(value) -> numberSymmetrically weighted moving average, using weights: 1/6, 2/6, 2/6, 1/6.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
ta.wma
ta.wma(value, period) -> numberWeighted moving average — linearly weighted, most recent bar has highest weight.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.vwma
ta.vwma(price, volume, period) -> numberVolume-weighted moving average.
| Param | Type | Description |
|---|---|---|
price | number | Price value |
volume | number | Volume value |
period | number | Lookback window |
ta.hma
ta.hma(value, length) -> numberHull moving average.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Lookback window |
ta.linreg
ta.linreg(value, period, offset?) -> numberLinear regression projected value.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Input value |
period | number | — | Lookback window |
offset | number | 0 | Bars to project forward |
Oscillators
ta.rsi
ta.rsi(value, period) -> numberRelative Strength Index (0–100).
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.stoch
ta.stoch(source, high, low, length) -> numberStochastic oscillator %K: 100 * (source - lowest(low, length)) / (highest(high, length) - lowest(low, length)).
| Param | Type | Description |
|---|---|---|
source | number | Input source, typically candles.close() |
high | number | High series value |
low | number | Low series value |
length | number | Lookback window |
ta.macd
ta.macd(value, fast?, slow?, signal?) -> { macd: number, signal: number, hist: number }Moving Average Convergence Divergence. Returns an object with three values. Fields are NaN when insufficient history.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Input value |
fast | number | 12 | Fast EMA period |
slow | number | 26 | Slow EMA period |
signal | number | 9 | Signal EMA period |
//@version=2
indicator("MACD Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const m = ta.macd(candles.close())
plot("MACD", m.macd, { color: color.blue })
plot("Signal", m.signal, { color: color.red })
plotHistogram("Hist", m.hist, { color: m.hist >= 0 ? color.green : color.red })
}ta.dmi
ta.dmi(source, diLength, adxSmoothing) -> { plusDI: number, minusDI: number, adx: number }Directional Movement Index. Requires an OHLCV source accessor returned by subscribe(data.OHLCV). The object shape is stable and its numeric fields are NaN until the indicator is ready.
| Param | Type | Description |
|---|---|---|
source | OHLCV accessor | Accessor returned by subscribe(data.OHLCV) |
diLength | number | DI smoothing length |
adxSmoothing | number | ADX smoothing length |
//@version=2
indicator("DMI Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const d = ta.dmi(candles, 14, 14)
plot("+DI", d.plusDI, { color: color.green })
plot("-DI", d.minusDI, { color: color.red })
plot("ADX", d.adx, { color: color.blue })
}Volume-based
ta.accdist
ta.accdist(source) -> numberAccumulation/Distribution line (ADL). This is a cumulative series:
adl = previous_adl + (((close - low) - (high - close)) / (high - low)) * volume
When high === low, the multiplier is treated as 0 for that bar. If any input is NaN, the previous ADL value is returned.
Requires an OHLCV subscription accessor returned by subscribe(data.OHLCV).
| Param | Type | Description |
|---|---|---|
source | OHLCV accessor | Accessor returned by subscribe(data.OHLCV) |
//@version=2
indicator("Chaikin Oscillator", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const ad = ta.accdist(candles)
const fast = ta.ema(ad, 3)
const slow = ta.ema(ad, 10)
plot("Chaikin", fast - slow)
}ta.mfi
ta.mfi(source, volume, length) -> numberMoney Flow Index. This runtime takes source and volume explicitly.
| Param | Type | Description |
|---|---|---|
source | number | Price source, typically hlc3 or candles.close() |
volume | number | Volume value |
length | number | Lookback window |
ta.vwap
ta.vwap(source, volume, anchor?, stdevMult?) -> number
ta.vwap(source, volume, anchor, stdevMult) -> { vwap: number, upper: number, lower: number }Anchored volume-weighted average price. Set anchor to true on the bar where accumulation should reset. When stdevMult is provided, the function returns VWAP plus upper/lower deviation bands as an object.
Numeric missing states use NaN. When stdevMult is provided, the returned object keeps a stable shape and uses NaN fields when unavailable.
| Param | Type | Default | Description |
|---|---|---|---|
source | number | — | Price source |
volume | number | — | Volume value |
anchor | boolean | false | Reset accumulation on this bar |
stdevMult | number | — | Band multiplier |
//@version=2
indicator("VWAP Example", true)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const isNewSession = context.isNew && unix() % 86400 === 0
const pv = ta.vwap(candles.close(), candles.volume())
const bands = ta.vwap(candles.close(), candles.volume(), isNewSession, 2)
plot("VWAP", pv, { color: color.blue })
plot("Upper", bands.upper, { color: color.red })
plot("Lower", bands.lower, { color: color.green })
}Volatility
ta.atr
ta.atr(high, low, close, period) -> numberAverage True Range — measures volatility. Returns NaN when insufficient history.
| Param | Type | Description |
|---|---|---|
high | number | High price |
low | number | Low price |
close | number | Close price |
period | number | Lookback window |
ta.stdev
ta.stdev(value, period) -> numberStandard deviation over a rolling window.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.bb
ta.bb(value, length, mult) -> { basis: number, upper: number, lower: number }Bollinger Bands. Returns a stable object with three values. Fields are NaN until enough history exists.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | SMA period |
mult | number | Standard deviation multiplier |
//@version=2
indicator("Bollinger Bands Example", false)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const bb = ta.bb(candles.close(), 20, 2)
plot("Upper", bb.upper, { color: color.red })
plot("Basis", bb.basis, { color: color.gray })
plot("Lower", bb.lower, { color: color.green })
}Aggregation
ta.highest
ta.highest(value, period) -> numberHighest value over the last period bars.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.max
ta.max(value) -> numberAll-time maximum for the series so far.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
ta.lowest
ta.lowest(value, period) -> numberLowest value over the last period bars.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Lookback window |
ta.pivothigh
ta.pivothigh(source, leftbars, rightbars) -> numberConfirmed pivot high. Returns the pivot price from rightbars bars ago only after the bars to the right have closed and confirmed it as a strict local maximum over the full window.
Use a negative plot offset to draw the confirmed value on the pivot bar:
//@version=2
indicator("Pivot High Example", true)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const ph = ta.pivothigh(candles.high(), 2, 2)
plot("Pivot High", na(ph) ? NaN : ph, { color: color.red, offset: -2 })
}| Param | Type | Description |
|---|---|---|
source | number | Input series, typically candles.high() |
leftbars | number | Bars to inspect on the left side |
rightbars | number | Bars required on the right side before confirmation |
ta.pivotlow
ta.pivotlow(source, leftbars, rightbars) -> numberConfirmed pivot low. Returns the pivot price from rightbars bars ago only after the bars to the right have closed and confirmed it as a strict local minimum over the full window.
//@version=2
indicator("Pivot Low Example", true)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const pl = ta.pivotlow(candles.low(), 2, 2)
plot("Pivot Low", na(pl) ? NaN : pl, { color: color.green, offset: -2 })
}| Param | Type | Description |
|---|---|---|
source | number | Input series, typically candles.low() |
leftbars | number | Bars to inspect on the left side |
rightbars | number | Bars required on the right side before confirmation |
ta.median
ta.median(value, length) -> numberRolling median over the last length values.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Lookback window |
ta.mode
ta.mode(value, length) -> numberMost frequent value in the rolling window.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Lookback window |
ta.dev
ta.dev(value, length) -> numberMean absolute deviation from the rolling average.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Lookback window |
Change Detection
ta.change
ta.change(value, period?) -> numberDifference between current value and value period bars ago.
| Param | Type | Default | Description |
|---|---|---|---|
value | number | — | Input value |
period | number | 1 | Bars to look back |
ta.barssince
ta.barssince(condition) -> numberNumber of bars since condition was last true. Returns 0 on the triggering bar.
| Param | Type | Description |
|---|---|---|
condition | boolean | Condition to track |
ta.cum
ta.cum(value) -> numberCumulative sum of the input series.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
ta.roc
ta.roc(value, period) -> numberRate of change as a percentage: (current - previous) / previous * 100.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
period | number | Bars to look back |
ta.rising
ta.rising(value, length) -> booleanReturns true when value has risen for length consecutive bars.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Consecutive-bar count |
ta.falling
ta.falling(value, length) -> booleanReturns true when value has fallen for length consecutive bars.
| Param | Type | Description |
|---|---|---|
value | number | Input value |
length | number | Consecutive-bar count |
Crossovers
ta.cross
ta.cross(a, b) -> booleanReturns true when a crosses b in either direction.
| Param | Type | Description |
|---|---|---|
a | number | First series value |
b | number | Second series value |
ta.crossover
ta.crossover(a, b) -> booleanReturns true when a crosses above b (previous bar: a <= b, current bar: a > b).
| Param | Type | Description |
|---|---|---|
a | number | First series value |
b | number | Second series value |
ta.crossunder
ta.crossunder(a, b) -> booleanReturns true when a crosses below b (previous bar: a >= b, current bar: a < b).
| Param | Type | Description |
|---|---|---|
a | number | First series value |
b | number | Second series value |
//@version=2
indicator("Crossover Example", true)
const candles = subscribe(data.OHLCV)
function onBar(index) {
const fast = ta.ema(candles.close(), 9)
const slow = ta.ema(candles.close(), 21)
if (ta.crossover(fast, slow)) {
plotMarker("Buy", candles.low(), { marker: shape.up, color: color.green })
}
}