Built-in Technical Indicators#
Pine Script’s ta namespace includes dozens of built-in technical indicator functions covering moving averages, oscillators, volatility indicators, and more. This chapter introduces the most commonly used indicators and their parameters.
Moving Averages#
ta.sma — Simple Moving Average#
ta.sma(source, length)sma20 = ta.sma(close, 20)
plot(sma20, "SMA 20", color=color.orange)ta.ema — Exponential Moving Average#
ta.ema(source, length)EMA assigns greater weight to recent prices, making it more responsive than SMA:
ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
plot(ema12, "EMA 12", color=color.blue)
plot(ema26, "EMA 26", color=color.orange)Other Moving Averages#
| Function | Description |
|---|---|
ta.wma(src, len) | Weighted Moving Average (WMA) |
ta.vwma(src, len) | Volume-Weighted Moving Average (VWMA) |
ta.hma(src, len) | Hull Moving Average (HMA) — smooth with low lag |
ta.alma(src, len, offset, sigma) | Arnaud Legoux Moving Average |
ta.swma(src) | Symmetrically Weighted Moving Average (fixed length of 4) |
Oscillators#
ta.rsi — Relative Strength Index#
ta.rsi(source, length)//@version=6
indicator("RSI")
rsiValue = ta.rsi(close, 14)
plot(rsiValue, "RSI", color=color.purple)
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)ta.macd — Moving Average Convergence/Divergence#
ta.macd(source, fastLength, slowLength, signalLength)
// Returns [macdLine, signalLine, histLine]//@version=6
indicator("MACD")
[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)
plot(hist, "Histogram", style=plot.style_histogram,
color=hist >= 0 ? color.new(color.green, 20) : color.new(color.red, 20))
plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)
hline(0, color=color.gray)ta.stoch — Stochastic Oscillator (KD)#
ta.stoch(close, high, low, length)
// Returns the %K value; %D = ta.sma(%K, 3)//@version=6
indicator("Stochastic KD")
kValue = ta.stoch(close, high, low, 14)
dValue = ta.sma(kValue, 3)
plot(kValue, "K", color=color.blue)
plot(dValue, "D", color=color.orange)
hline(80, "Overbought", color=color.red)
hline(20, "Oversold", color=color.green)ta.cci — Commodity Channel Index#
ta.cci(source, length)ta.williams — Williams %R#
ta.williams(high, low, close, length)Trend Indicators#
ta.adx — Average Directional Index#
[plusDI, minusDI, adx] = ta.dmi(diLength, adxSmoothing)//@version=6
indicator("ADX / DMI")
[plusDI, minusDI, adxValue] = ta.dmi(14, 14)
plot(plusDI, "+DI", color=color.green)
plot(minusDI, "-DI", color=color.red)
plot(adxValue, "ADX", color=color.blue, linewidth=2)
hline(25, "Trend Threshold", color=color.gray, linestyle=hline.style_dashed)Volatility Indicators#
ta.bb — Bollinger Bands#
ta.bb(source, length, mult)
// Returns [middle, upper, lower]//@version=6
indicator("Bollinger Bands", overlay=true)
[middle, upper, lower] = ta.bb(close, 20, 2)
plot(upper, "Upper", color=color.blue)
plot(middle, "Middle", color=color.orange)
plot(lower, "Lower", color=color.blue)
fill(plot(upper, display=display.none),
plot(lower, display=display.none),
color=color.new(color.blue, 92))ta.atr — Average True Range#
ta.atr(length)ATR is commonly used to set dynamic stop-loss distances:
//@version=6
indicator("ATR Stop-Loss Demo", overlay=true)
atrValue = ta.atr(14)
multiplier = input.float(2.0, "ATR Multiplier")
stopLong = close - atrValue * multiplier
stopShort = close + atrValue * multiplier
plot(stopLong, "Long Stop", color=color.red, style=plot.style_stepline)
plot(stopShort, "Short Stop", color=color.green, style=plot.style_stepline)Volume Indicators#
ta.obv — On Balance Volume#
ta.obvta.vwap — Volume Weighted Average Price#
ta.vwap(source)
// Or use the built-in VWAP directly: ta.vwap//@version=6
indicator("VWAP", overlay=true)
vwapValue = ta.vwap(hlc3)
plot(vwapValue, "VWAP", color=color.purple, linewidth=2)Pivot Point Detection#
ta.pivothigh / ta.pivotlow#
Detects pivot highs and lows, commonly used for automatic trend line drawing:
ta.pivothigh(source, leftBars, rightBars)
ta.pivotlow(source, leftBars, rightBars)
// Returns the price of the pivot point; returns na if the current bar is not a pivot//@version=6
indicator("Pivot Points", overlay=true)
pivotH = ta.pivothigh(high, 5, 5)
pivotL = ta.pivotlow(low, 5, 5)
plotshape(pivotH, "Pivot High", shape.triangledown,
location.abovebar, color.red, size=size.small)
plotshape(pivotL, "Pivot Low", shape.triangleup,
location.belowbar, color.green, size=size.small)Crossover Detection#
| Function | Description |
|---|---|
ta.crossover(a, b) | a crosses above b (golden cross) |
ta.crossunder(a, b) | a crosses below b (death cross) |
ta.cross(a, b) | a and b cross in either direction |
//@version=6
indicator("MA Crossover", overlay=true)
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
golden = ta.crossover(ma5, ma20)
death = ta.crossunder(ma5, ma20)
plotshape(golden, "Golden Cross", shape.labelup,
location.belowbar, color.green, text="G", textcolor=color.white)
plotshape(death, "Death Cross", shape.labeldown,
location.abovebar, color.red, text="D", textcolor=color.white)