Write
Technical analysis
The ta.* namespace: averages, oscillators, bands, crosses, pivots and multi-value studies, with working recipes.
ta.* holds the building blocks: more than seventy functions, from ta.sma to ta.supertrend. They all follow the bar-by-bar model, so each returns a series and handles its own warm-up (the first bars are na).
Averages#
| Function | Notes |
|---|---|
ta.sma(src, len) | Simple average |
ta.ema(src, len) | Exponential, reacts faster |
ta.wma(src, len) | Linearly weighted |
ta.hma(src, len) | Hull, smooth and fast |
ta.rma(src, len) | Wilder's smoothing, used inside RSI and ATR |
ta.vwma(src, len) | Volume weighted (see the note on volume below) |
ta.alma(src, len, offset, sigma) | Arnaud Legoux |
Oscillators and volatility#
ta.rsi, ta.stoch, ta.cci, ta.mfi, ta.mom, ta.roc, ta.tsi, ta.wpr, ta.atr, ta.tr, ta.stdev, ta.dev, and more.
//@version=6indicator("Stochastic")k = ta.sma(ta.stoch(close, high, low, 14), 3)d = ta.sma(k, 3)plot(k, "%K", color = color.new(#8b7cff, 0), linewidth = 2)plot(d, "%D", color = color.new(#ffb547, 0))hline(80, "Upper", color = color.gray)hline(20, "Lower", color = color.gray)Studies that return several values#
Some functions return a tuple. Destructure it with square brackets:
//@version=6indicator("MACD")[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)plot(hist, "Histogram", style = plot.style_columns, color = hist >= 0 ? color.new(color.teal, 40) : color.new(color.red, 40))plot(macdLine, "MACD", color = color.new(#8b7cff, 0))plot(signalLine, "Signal", color = color.new(#ffb547, 0))Others in this family: ta.bb (basis, upper, lower), ta.kc (Keltner), ta.dmi (+DI, -DI, ADX), ta.supertrend (value, direction).
//@version=6indicator("Supertrend", overlay = true)[st, dir] = ta.supertrend(3, 10)plot(dir < 0 ? st : na, "Up trend", color = color.teal, style = plot.style_linebr, linewidth = 2)plot(dir > 0 ? st : na, "Down trend", color = color.red, style = plot.style_linebr, linewidth = 2)In ta.supertrend, a negative direction means an up trend.
Crosses, highs and lows, pivots#
ta.crossover(a, b),ta.crossunder(a, b),ta.cross(a, b):trueon the bar the cross happens.ta.highest(src, len),ta.lowest(src, len), andta.highestbars/ta.lowestbarsfor how many bars ago.ta.pivothigh(src, left, right)andta.pivotlow(...): the pivot price, reportedrightbars after the pivot (because it needs those bars to confirm).naelsewhere.ta.valuewhen(cond, src, n): the value ofsrcthe n-th last timecondwas true.ta.barssince(cond): bars sincecondwas last true.
//@version=6indicator("Pivot levels", overlay = true)ph = ta.pivothigh(high, 10, 10)pl = ta.pivotlow(low, 10, 10)plot(ta.valuewhen(not na(ph), ph, 0), "Last pivot high", color = color.red, style = plot.style_stepline)plot(ta.valuewhen(not na(pl), pl, 0), "Last pivot low", color = color.teal, style = plot.style_stepline)plotshape(not na(ph), "PH", style = shape.triangledown, location = location.abovebar, color = color.red, offset = -10, size = size.tiny)plotshape(not na(pl), "PL", style = shape.triangleup, location = location.belowbar, color = color.teal, offset = -10, size = size.tiny)offset = -10 draws each marker back on the bar that actually was the pivot.
A note on volume#
Volume on this feed is a tick count per bar, not traded size. Indicators that compare volume between bars (ta.obv, ta.mfi, plotting volume itself) are meaningful on it. Ones that treat it as size, such as ta.vwap and accumulation/distribution, still compute, and the editor warns that the result is approximate. See The data.