Appearance
ta
How do you compute an indicator on each bar? Call a ta.* function at the top level of your script, once per bar. Each call keeps its own state and sees every bar. The answers are TradingView's, except where an entry's warning says how they differ.
pine
//@version=6
indicator("Two averages", overlay = true)
fast = ta.ema(close, 9)
slow = ta.sma(close, 21)
plot(fast, "EMA 9", color.orange)
plot(slow, "SMA 21", color.blue)
// called on every bar, so the cross compares this bar with the one before
plotshape(ta.crossover(fast, slow), "Cross up", shape.triangleup, location.belowbar, color.green)Four rules hold for every function on this page.
- One state per call. Each
ta.*call in your text keeps its own window. Two calls ofta.sma(close, 20)on two lines are two averages, and a call inside your own function keeps one state for each place that function is called from. - A call in a branch sees only the bars it runs on. Most functions count only their calls, so a bar the call skipped isn't in the window:
ta.sma,ta.ema,ta.rma,ta.rsi,ta.highest,ta.stdevand every entry that says nothing about branches. The rest say how they read a skipped bar. Those that read their source bar by bar (ta.wma,ta.swma,ta.alma,ta.change,ta.mom,ta.roc,ta.percentrank,ta.hma, and the inner loops ofta.dev,ta.cciandta.cog) treat it as holding the last value the call read. See functions and branches. nain the middle of a series. Some functions skip anaand read the lastlengthnumbers; others readnawhile the hole is in their window. Each window function's entry says which. An infinity counts asnain every window.- Lengths are whole bars. A fractional length or offset is cut toward zero, so
ta.wma(close, 15 / 2)is a 7-bar average. A length of 0 or less readsna.
ta.accdist
pine
series float ta.accdistThe accumulation/distribution line: the running sum of each bar's close location within its range, times its volume.
A bar with no range adds 0. A bar with a na price or volume reads na and adds nothing. It is a value, not a call: every read on one bar gives that bar's first answer, so read it on every bar. In a branch it sums only the bars where it runs.
ta.alma
pine
ta.alma(series, length, offset, sigma, floor) → series floatThe Arnaud Legoux moving average: a Gaussian-weighted average whose peak sits at offset along the window.
The window always holds the last length bars, so a na in it makes the result na until the na leaves. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to average. | |
lengthseries int | 9AmenoScript only | How many bars. |
offsetsimple int/float | 0.85AmenoScript only | Where the weights peak, from 0 (the oldest bar) to 1 (the newest). |
sigmasimple int/float | 6AmenoScript only | How sharp the peak is. A larger value narrows it. |
floorsimple bool | false | true rounds the peak's position down to a whole bar. |
ta.atr
pine
ta.atr(length) → series floatThe average true range: Wilder's moving average (ta.rma) of the true range.
On the first bar the true range is the bar's high minus its low. After bars the call skipped, the true range still reads the close of the bar just before.
| Parameter | Description |
|---|---|
lengthsimple int | How many bars to smooth over. It must be fixed for the run. |
ta.barssince
pine
ta.barssince(condition) → series intThe number of bars since condition was last true: 0 on a bar where it is true, and na before it has ever been true.
The count goes up by one for each call, so a call inside a branch counts the bars it runs on.
| Parameter | Description |
|---|---|
conditionseries bool | The test to look back for. |
pine
//@version=6
indicator("Bars since the last up close")
since = ta.barssince(close > open) // 0 on an up bar, na before the first one
plot(since)ta.bb
pine
ta.bb(series, length, mult) → [series float, series float, series float]Bollinger Bands: [middle, upper, lower], the length-bar simple moving average and that average plus and minus mult standard deviations.
A na leaves the window as it was, as ta.sma and ta.stdev do.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to measure. | |
lengthseries int | 20AmenoScript only | How many bars. |
multsimple int/float | 2AmenoScript only | How many standard deviations each band sits from the middle. |
pine
//@version=6
indicator("Bollinger Bands", overlay = true)
[middle, upper, lower] = ta.bb(close, 20, 2) // a tuple of three series
plot(middle, "Basis", color.orange)
p1 = plot(upper, "Upper", color.blue)
p2 = plot(lower, "Lower", color.blue)
fill(p1, p2, color.new(color.blue, 90))ta.bbw
pine
ta.bbw(series, length, mult) → series floatThe Bollinger Band width: the distance between the bands as a percentage of the middle band.
It is (upper - lower) / middle * 100, and na where the middle is 0.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to measure. | |
lengthseries int | 20AmenoScript only | How many bars. |
multsimple int/float | 2AmenoScript only | How many standard deviations each band sits from the middle. |
ta.beta
AmenoScript only
ameno
ta.beta(y, x, length) → series floatThe slope of y on x over the last length bars: their covariance divided by the variance of x. See the cross-section.
A bar where either value is na is left out, so the window is the last length bars where both were numbers. It reads na while the window fills, and where x didn't move.
| Parameter | Description |
|---|---|
yseries int/float | The series to explain, such as a coin's returns. |
xseries int/float | The series it moves with, such as the market's returns. |
lengthseries int | How many bars. |
ameno
indicator("Beta to its own past")
r = close / close[1] - 1
plot(ta.beta(r, r[1], 30)) // AmenoScript onlyta.cci
pine
ta.cci(source, length) → series floatThe commodity channel index: how far the source sits from its length-bar average, in units of 0.015 times the mean absolute deviation.
It reads na while a na is in the window. Where the deviation is 0, as on a flat window, it repeats its last value. In a branch, the average counts only the calls, and the deviation treats a skipped bar as the value the call last read.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to measure. | |
lengthseries int | 20AmenoScript only | How many bars. |
ta.change
pine
ta.change(source, length) → series floatThe source minus its value length bars ago.
The window always holds the last length + 1 bars, so a na at either end reads na. In a branch, a skipped bar counts as the value the call last read.
A bool source returns a number here
On a bool source this engine returns 1, 0 or -1, reading true as 1, where TradingView returns whether the value changed. Write cond != cond[1] for TradingView's answer.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float/bool | The values to compare. | |
lengthseries int | 1 | How many bars back to compare with. |
ta.cmo
pine
ta.cmo(series, length) → series floatThe Chande momentum oscillator: the sum of up moves minus the sum of down moves, over their total, times 100.
Each move is from one bar to the next, so a na takes the moves on both sides of it out of the sums. A window with no move reads na.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to measure. | |
lengthseries int | 9AmenoScript only | How many bars. |
ta.cog
pine
ta.cog(source, length) → series floatThe centre of gravity: a weighted sum of the last length values, newest weighted least, over their plain sum, negated.
It reads na while a na is in the window. In a branch, its weighted sum treats a skipped bar as the value the call last read.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to measure. | |
lengthseries int | 10AmenoScript only | How many bars. |
ta.correlation
pine
ta.correlation(source1, source2, length) → series floatThe Pearson correlation of two series over the last length bars, from -1 to 1.
Each mean skips its own na, so a hole in one series moves only that series' means. A window where either series is flat reads 0.
| Parameter | Description |
|---|---|
source1series int/float | The first series. |
source2series int/float | The second series. |
lengthseries int | How many bars. |
ta.cov
AmenoScript only
ameno
ta.cov(source1, source2, length) → series floatThe covariance of two series over the last length bars, dividing by length. See the cross-section.
A bar where either value is na is left out, so the window is the last length bars where both were numbers.
| Parameter | Description |
|---|---|
source1series int/float | The first series. |
source2series int/float | The second series. |
lengthseries int | How many bars. |
ta.cross
pine
ta.cross(source1, source2) → series boolWhether the two series crossed in either direction since the last call.
A bar where either value is na returns false and doesn't count, so the next bar compares with the last bar where both were numbers.
| Parameter | Description |
|---|---|
source1series int/float | The first series. |
source2series int/float | The second series. |
ta.crossover
pine
ta.crossover(source1, source2) → series boolWhether source1 crossed above source2: it is above now, and was at or below on the last call.
A bar where either value is na returns false and doesn't count, so the next bar compares with the last bar where both were numbers.
| Parameter | Description |
|---|---|
source1series int/float | The series that crosses. |
source2series int/float | The series it crosses. |
ta.crossunder
pine
ta.crossunder(source1, source2) → series boolWhether source1 crossed below source2: it is below now, and was at or above on the last call.
A bar where either value is na returns false and doesn't count.
| Parameter | Description |
|---|---|
source1series int/float | The series that crosses. |
source2series int/float | The series it crosses. |
ta.cum
pine
ta.cum(source) → series floatThe running total of the source since the first bar.
A na bar reads na and adds nothing, and the total carries on after it.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to add up. |
ta.dev
pine
ta.dev(source, length) → series floatThe mean absolute deviation: the average distance of the last length values from their mean.
It reads na while a na is in the window. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to measure. |
lengthseries int | How many bars. |
ta.dmi
pine
ta.dmi(diLength, adxSmoothing) → [series float, series float, series float]The directional movement index: [plusDI, minusDI, adx].
The first bar adds no movement, since each move is a change from the bar before. Where the smoothed true range is 0, each DI repeats its last value.
| Parameter | Default | Description |
|---|---|---|
diLengthsimple int | 14AmenoScript only | The bars that smooth the directional movement and the true range. |
adxSmoothingsimple int | 14AmenoScript only | The bars that smooth the ADX. |
pine
//@version=6
indicator("DMI")
[plusDI, minusDI, adx] = ta.dmi(14, 14)
plot(plusDI, "+DI", color.green)
plot(minusDI, "-DI", color.red)
plot(adx, "ADX", color.blue)ta.ema
pine
ta.ema(source, length) → series floatThe exponential moving average, which weights each bar 2 / (length + 1) against the average before it.
It reads na for the first length - 1 bars and starts from the simple average of the first length values. A na bar reads na and leaves the average as it was.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthsimple int | How many bars. It must be fixed for the run: a length that changes keeps the first bar's. |
ta.falling
pine
ta.falling(source, length) → series boolWhether the source fell from each bar to the next over the last length bars.
A step with a na on either side leaves the count as it was.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to test. |
lengthseries int | How many falls in a row it takes. |
ta.highest
pine
ta.highest(source, length) → series float
ta.highest(length) → series floatThe highest value of the last length bars.
With one argument it reads high. The window is the last length numbers, read from the lengthth call on, so a na leaves it as it was. A length that changes from bar to bar reads the window of the new length.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With one argument, high. |
lengthseries int | How many bars. |
ta.highestbars
pine
ta.highestbars(source, length) → series int
ta.highestbars(length) → series intHow many bars back the highest of the last length bars was, as 0 or a negative number.
With one argument it reads high. Of equal highs, the newest wins. The offset counts bars, so a na hole or bars the call skipped push it further back.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With one argument, high. |
lengthseries int | How many bars. |
ta.hma
pine
ta.hma(source, length) → series floatThe Hull moving average: ta.wma(2 * ta.wma(source, length / 2) - ta.wma(source, length), floor(sqrt(length))).
Both inner lengths are rounded down. Each weighted average treats a na, and a bar a branch skipped, as ta.wma does.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthsimple int | How many bars. It must be fixed for the run. |
ta.ichimoku
AmenoScript only
ameno
ta.ichimoku(conversionPeriods, basePeriods, laggingSpan2Periods, displacement) → [series float, series float, series float, series float, series float]The Ichimoku lines: [conversion, base, leadA, leadB, lagging]. See AmenoScript's built-ins.
conversion and base are the midpoints of the highest high and lowest low over their periods, leadA is their mean, and leadB is the midpoint over laggingSpan2Periods. The leads come unshifted, so plot them with offset = displacement - 1 to draw the cloud ahead, as the chart's Ichimoku Cloud does. lagging is the close of displacement - 1 bars ago.
| Parameter | Default | Description |
|---|---|---|
conversionPeriodssimple int | 9 | Bars for the conversion line. |
basePeriodssimple int | 26 | Bars for the base line. |
laggingSpan2Periodssimple int | 52 | Bars for the second lead. |
displacementsimple int | 26 | How far the leads are meant to sit ahead, and the lagging line behind. |
ameno
indicator("Ichimoku", overlay = true)
[conv, base, leadA, leadB, lag] = ta.ichimoku(9, 26, 52, 26) // AmenoScript only
plot(conv, "Conversion", color.blue)
plot(lag, "Lagging close", color.gray)
plot(base, "Base", color.red)
plot(leadA, "Lead A", color.green, offset = 25) // displacement - 1
plot(leadB, "Lead B", color.orange, offset = 25)ta.iii
pine
series float ta.iiiThe intraday intensity index: (2 * close - high - low) / (high - low) * volume.
A bar with no range reads na.
ta.kc
pine
ta.kc(series, length, mult, useTrueRange) → [series float, series float, series float]Keltner Channels: [middle, upper, lower], the exponential average of the source and that average plus and minus mult times the exponential average of the range.
The range is the true range, or the plain high minus low with useTrueRange = false. On the first bar the true range is na, so the bands start a bar after the middle. After bars the call skipped, the true range still reads the close of the bar just before.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to average. | |
lengthseries int | 20AmenoScript only | How many bars, for both averages. |
multsimple int/float | 2AmenoScript only | How many ranges each band sits from the middle. |
useTrueRangesimple bool | true | false measures each bar's high minus low. |
ta.kcw
pine
ta.kcw(series, length, mult, useTrueRange) → series floatThe Keltner Channel width: (upper - lower) / middle, as a ratio, not a percentage.
It reads na where the middle is 0. After bars the call skipped, the true range still reads the close of the bar just before.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The values to average. | |
lengthseries int | 20AmenoScript only | How many bars. |
multsimple int/float | 2AmenoScript only | How many ranges each band sits from the middle. |
useTrueRangesimple bool | true | false measures each bar's high minus low. |
ta.linreg
pine
ta.linreg(source, length, offset) → series floatThe value of the least-squares line through the last length values, at offset bars back from the newest.
The window always holds the last length bars, so a na in it reads na. In a branch, the call keeps a ring of length + 1 slots by bar index: a slot no call wrote reads 0, and after skipped bars a slot keeps what a call length + 1 bars earlier wrote.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to fit. | |
lengthseries int | How many bars. | |
offsetsimple int | 0AmenoScript only | How many bars back from the newest to read the line at. |
ta.lowest
pine
ta.lowest(source, length) → series float
ta.lowest(length) → series floatThe lowest value of the last length bars.
With one argument it reads low. A na leaves the window as it was, as in ta.highest.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With one argument, low. |
lengthseries int | How many bars. |
ta.lowestbars
pine
ta.lowestbars(source, length) → series int
ta.lowestbars(length) → series intHow many bars back the lowest of the last length bars was, as 0 or a negative number.
With one argument it reads low. Of equal lows, the newest wins. The offset counts bars, so a na hole or bars the call skipped push it further back.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With one argument, low. |
lengthseries int | How many bars. |
ta.macd
pine
ta.macd(source, fastlen, slowlen, siglen) → [series float, series float, series float]The moving average convergence divergence: [macd, signal, histogram].
macd is the fast exponential average minus the slow one, signal is the exponential average of macd, and histogram is their difference. A na source reads na in all three and moves nothing.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to average. | |
fastlensimple int | 12AmenoScript only | Bars for the fast average. |
slowlensimple int | 26AmenoScript only | Bars for the slow average. |
siglensimple int | 9AmenoScript only | Bars for the signal line. |
pine
//@version=6
indicator("MACD")
[macdLine, signal, hist] = ta.macd(close, 12, 26, 9)
plot(hist, "Histogram", color.gray, style = plot.style_columns)
plot(macdLine, "MACD", color.blue)
plot(signal, "Signal", color.orange)ta.max
pine
ta.max(source) → series floatThe highest value the source has had since the first bar.
A na or an infinity is skipped. It reads na until the first number.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to track. |
ta.md
AmenoScript only
ameno
ta.md(source, length) → series floatta.dev under a second name, MD for mean deviation. See AmenoScript's built-ins.
It gives what ta.dev gives with the same arguments, and each call keeps its own state like any other. It exists only so a script can use the short name; in Pine, write ta.dev.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to measure. |
lengthseries int | How many bars. |
ta.median
pine
ta.median(source, length) → series int/floatThe median of the last length values.
A na leaves the window as it was, so it reads the median of the last length numbers.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to measure. |
lengthseries int | How many bars. |
ta.mfi
pine
ta.mfi(series, length) → series floatThe money flow index: the share of the last length bars' money flow that came on rising bars, from 0 to 100.
Each bar's flow is its source times its volume, counted as rising or falling by the change from the bar before. A window with no falling flow reads 100.
| Parameter | Default | Description |
|---|---|---|
seriesseries int/float | The typical price. | |
lengthseries int | 14AmenoScript only | How many bars. |
ta.min
pine
ta.min(source) → series floatThe lowest value the source has had since the first bar.
A na or an infinity is skipped. It reads na until the first number.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to track. |
ta.mode
pine
ta.mode(source, length) → series int/floatThe most frequent of the last length values, the smallest of them on a tie.
A na leaves the window as it was.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to count. |
lengthseries int | How many bars. |
ta.mom
pine
ta.mom(source, length) → series floatThe momentum: the source minus its value length bars ago.
The window always holds the last length + 1 bars, so a na at either end reads na. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to compare. | |
lengthseries int | 10AmenoScript only | How many bars back. |
ta.nvi
pine
series float ta.nviThe negative volume index: from 1, it moves by the close's percentage change on each bar whose volume fell.
ta.obv
pine
series float ta.obvThe on-balance volume: the running sum of volume, added on an up close and taken away on a down close.
It reads na on the first bar.
ta.percentile_linear_interpolation
pine
ta.percentile_linear_interpolation(source, length, percentage) → series floatThe value below which percentage percent of the last length values fall, interpolating between the two nearest.
The window keeps na values, and a na sorts above every number, so a na in the window can be the answer near the top.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to rank. |
lengthseries int | How many bars. |
percentagesimple int/float | The percentile, from 0 to 100. |
ta.percentile_nearest_rank
pine
ta.percentile_nearest_rank(source, length, percentage) → series floatThe value at percentage percent of the last length values, sorted, by the nearest-rank method.
It always returns one of the window's values. The window keeps na values sorted above every number, as in ta.percentile_linear_interpolation.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to rank. |
lengthseries int | How many bars. |
percentagesimple int/float | The percentile, from 0 to 100. |
ta.percentrank
pine
ta.percentrank(source, length) → series floatThe percentage of the previous length values that are at or below the current one.
A na in the window counts as not at or below, and a na source reads 0. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to rank. |
lengthseries int | How many earlier bars to compare with. |
ta.pivot_point_levels
pine
ta.pivot_point_levels(type, anchor, developing) → array<float>The classic pivot levels of the current bar, as an array of seven: [pivot, s1, r1, s2, r2, s3, r3].
This engine's levels differ from TradingView's
This engine reads type, anchor and developing but uses none of them: every call returns the classic levels from the current bar's high, low and close. TradingView returns eleven levels of the chosen type, from the last anchor period. Compute the levels you need from request.security until this changes.
| Parameter | Description |
|---|---|
typeseries string | The kind of pivots TradingView would compute, such as "Traditional". Ignored here. |
anchorseries bool | When TradingView would start a new period. Ignored here. |
developingseries bool | Whether TradingView would update the levels as the period forms. Ignored here. |
ta.pivothigh
pine
ta.pivothigh(source, leftbars, rightbars) → series float
ta.pivothigh(leftbars, rightbars) → series floatThe value of a pivot high, returned rightbars bars after the pivot bar, and na on every other bar.
A pivot high is higher than each of the rightbars bars after it and no lower than each of the leftbars bars before it, so an earlier bar can tie it and a later one can't. With two arguments it reads high. The value arrives late by design: on the bar it returns, the pivot is rightbars bars back. It counts those bars by bar index, so in a branch the bars the call skipped count too.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With two arguments, high. |
leftbarsseries int/float | How many bars before the pivot it is compared with. |
rightbarsseries int/float | How many bars after the pivot it must beat, and so how late the value comes. |
pine
//@version=6
indicator("Pivot highs", overlay = true)
ph = ta.pivothigh(high, 3, 3)
// the pivot was 3 bars ago, so draw it there; live, the mark appears 3 bars after its bar
plotshape(not na(ph), "Pivot", shape.triangledown, location.abovebar, color.red, offset = -3)ta.pivotlow
pine
ta.pivotlow(source, leftbars, rightbars) → series float
ta.pivotlow(leftbars, rightbars) → series floatThe value of a pivot low, returned rightbars bars after the pivot bar, and na on every other bar.
A pivot low is lower than each of the rightbars bars after it and no higher than each of the leftbars bars before it, so an earlier bar can tie it and a later one can't. With two arguments it reads low. It counts rightbars by bar index, so in a branch the bars the call skipped count too.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to search. With two arguments, low. |
leftbarsseries int/float | How many bars before the pivot it is compared with. |
rightbarsseries int/float | How many bars after the pivot it must beat, and so how late the value comes. |
ta.pvi
pine
series float ta.pviThe positive volume index: from 1, it moves by the close's percentage change on each bar whose volume rose.
ta.pvt
pine
series float ta.pvtThe price-volume trend: the running sum of each bar's percentage change in close times its volume.
It reads na on the first bar.
ta.range
pine
ta.range(source, length) → series floatThe highest minus the lowest of the last length values.
A na leaves the window as it was, and it reads na until the window holds length numbers.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to measure. |
lengthseries int | How many bars. |
ta.rci
pine
ta.rci(source, length) → series floatThe rank correlation index: 100 times the correlation between the bars' order in time and the order of their values, from -100 to 100.
A steady rise reads about 100. Equal values share their average rank. A na bar repeats the last answer. In a branch, the call reads a ring of length + 1 slots by bar index, where a slot no call wrote reads 0.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to rank. | |
lengthsimple int | 9AmenoScript only | How many bars. It must be fixed for the run. |
ta.rising
pine
ta.rising(source, length) → series boolWhether the source rose from each bar to the next over the last length bars.
A step with a na on either side leaves the count as it was.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to test. |
lengthseries int | How many rises in a row it takes. |
ta.rma
pine
ta.rma(source, length) → series floatWilder's moving average, which weights each bar 1 / length against the average before it.
It starts from the simple average of the first length values. A na bar reads na and leaves the average as it was.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthsimple int | How many bars. It must be fixed for the run. |
ta.roc
pine
ta.roc(source, length) → series floatThe rate of change: the percentage change of the source from length bars ago.
It reads na where the value length bars ago is 0. The window always holds the last length + 1 bars, so a na at either end reads na. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to compare. | |
lengthseries int | 1AmenoScript only | How many bars back. |
ta.rsi
pine
ta.rsi(source, length) → series floatThe relative strength index, from 0 to 100: Wilder's average of the gains over that of the losses.
It reads 100 where the losses' average is 0, a flat window included, and 0 where only the gains' average is. Each gain or loss is the change from one bar to the next, so a na takes the changes on both sides of it out of the averages.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to measure. |
lengthsimple int | How many bars. It must be fixed for the run. |
ta.sar
pine
ta.sar(start, increment, maximum) → series floatThe parabolic stop and reverse: a trailing level that speeds up as the trend runs and flips side when price crosses it.
It starts on the run's second bar, so a call inside a branch that misses that bar reads na for good.
TradingView's keyword names don't reach it
This engine names the last two parameters increment and maximum. TradingView names them inc and max, and a call written ta.sar(0.02, inc = 0.1, max = 0.5) runs here on the defaults. Pass them by position.
| Parameter | Default | Description |
|---|---|---|
startsimple int/float | 0.02AmenoScript only | The step the speed starts from on each new trend. |
incrementsimple int/float | 0.02AmenoScript only | How much the speed grows with each new extreme. |
maximumsimple int/float | 0.2AmenoScript only | The highest the speed can go. |
ta.sma
pine
ta.sma(source, length) → series floatThe simple moving average: the mean of the last length values.
A na leaves the window as it was, so the mean is of the last length numbers. A length that changes from bar to bar reads the window of the new length.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthseries int | How many bars. |
ta.stdev
pine
ta.stdev(source, length, biased) → series floatThe standard deviation of the last length values.
A na leaves the window as it was. Rounding can't take it below 0.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to measure. | |
lengthseries int | How many bars. | |
biasedseries bool | true | true divides by length, the population figure. false divides by length - 1, the sample figure. |
ta.stoch
pine
ta.stoch(source, high, low, length) → series floatThe stochastic: where the source sits between the lowest low and the highest high of the last length bars, from 0 to 100.
A flat window or a na source repeats the last value.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to place, usually close. |
highseries int/float | The highs to search. |
lowseries int/float | The lows to search. |
lengthseries int | How many bars. |
pine
//@version=6
indicator("Stochastic")
k = ta.sma(ta.stoch(close, high, low, 14), 3)
d = ta.sma(k, 3)
plot(k, "%K", color.blue)
plot(d, "%D", color.orange)ta.supertrend
pine
ta.supertrend(factor, atrPeriod) → [series float, series float]The supertrend: [supertrend, direction], a trailing band under price in an uptrend and over it in a downtrend.
direction is -1 in an uptrend and 1 in a downtrend. The band is hl2 plus or minus factor times the average true range, and it only moves in the trend's favour until price closes through it. After bars the call skipped, the true range still reads the close of the bar just before.
| Parameter | Description |
|---|---|
factorseries int/float | How many average true ranges the band sits from hl2. |
atrPeriodsimple int | How many bars the average true range covers. |
pine
//@version=6
indicator("Supertrend", overlay = true)
[st, dir] = ta.supertrend(3, 10)
plot(dir < 0 ? st : na, "Up", color.green, style = plot.style_linebr) // -1 is an uptrend
plot(dir > 0 ? st : na, "Down", color.red, style = plot.style_linebr)ta.swma
pine
ta.swma(source) → series floatThe symmetrically weighted moving average over 4 bars, weights 1, 2, 2, 1.
The window always holds the last 4 bars, so a na in it reads na. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
ta.tr
pine
series float ta.tr
ta.tr(handle_na) → series floatThe true range: the greatest of the bar's high minus low and the distances from the close before to the high and to the low.
On the first bar there is no close before, so it reads na, or the high minus the low with handle_na = true. Read bare as ta.tr, it is ta.tr(false). After bars the call skipped, it reads the close of the bar just before.
| Parameter | Description |
|---|---|
handle_nasimple bool | true reads the high minus the low where there is no close before, and false reads na there. |
ta.ts_rank
AmenoScript only
ameno
ta.ts_rank(source, from, to) → series floatHow the current value ranks against the values from to to bars ago: the number it is above minus the number it isn't. See the cross-section.
Values within 1e-10 of each other count as equal, as Pine's comparisons read them, and a na in the window is skipped. A window with nothing in it reads na.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to rank. |
fromseries int | The nearest bar back to compare with. |
toseries int | The furthest bar back to compare with. |
ameno
indicator("Rank against the last 20 bars")
plot(ta.ts_rank(close, 1, 20)) // AmenoScript only: from -20 to 20ta.tsi
pine
ta.tsi(source, short_length, long_length) → series floatThe true strength index: the double-smoothed change of the source over the double-smoothed size of that change, from -1 to 1.
It gives the ratio itself, not a percentage. Each change is from one bar to the next.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to measure. | |
short_lengthsimple int | 13AmenoScript only | Bars for the second smoothing. |
long_lengthsimple int | 25AmenoScript only | Bars for the first smoothing. |
ta.valuewhen
pine
ta.valuewhen(condition, source, occurrence) → series bool/color/float/intThe value source had on the occurrence-th most recent bar where condition was true, counting from 0.
occurrence = 0 is the latest such bar, which can be the current one. It reads na until the condition has been true occurrence + 1 times.
| Parameter | Default | Description |
|---|---|---|
conditionseries bool | The test that marks a bar. | |
sourceseries bool/color/int/float | The value to read on the marked bars. | |
occurrencesimple int | 0AmenoScript only | Which marked bar, 0 for the latest. It must be fixed for the run. |
pine
//@version=6
indicator("Close at the last two crosses", overlay = true)
cross = ta.crossover(close, ta.sma(close, 20))
plot(ta.valuewhen(cross, close, 0), "Last cross", color.blue)
plot(ta.valuewhen(cross, close, 1), "Cross before it", color.gray) // one cross further backta.variance
pine
ta.variance(source, length, biased) → series floatThe variance of the last length values.
A na leaves the window as it was. Rounding can't take it below 0.
| Parameter | Default | Description |
|---|---|---|
sourceseries int/float | The values to measure. | |
lengthseries int | How many bars. | |
biasedseries bool | true | true divides by length, the population figure. false divides by length - 1, the sample figure. |
ta.vwap
pine
series float ta.vwap
ta.vwap(source) → series float
ta.vwap(source, anchor) → series float
ta.vwap(source, anchor, stdev_mult) → [series float, series float, series float]The volume-weighted average price since the last anchor.
With no anchor, it starts again at the first bar of each UTC day. With stdev_mult it returns [vwap, upper, lower], the bands sitting that many volume-weighted standard deviations away. Read bare as ta.vwap, it is the daily average of hlc3.
The default anchor is the UTC day
TradingView restarts the average at each new trading day. For a symbol whose session crosses midnight UTC, such as a future that opens in the evening, the two differ. For the trading day, pass anchor = ta.change(time_tradingday) != 0.
| Parameter | Description |
|---|---|
sourceseries int/float | The price to average. |
anchorseries bool | True on a bar where the average starts again. |
stdev_multseries int/float | How many standard deviations each band sits away. Passing it, even as na, returns the three-value tuple. |
pine
//@version=6
indicator("Weekly VWAP with bands", overlay = true)
[vwap, upper, lower] = ta.vwap(hlc3, timeframe.change("1W"), 1) // restarts each week
plot(vwap, "VWAP", color.orange)
plot(upper, "Upper", color.gray)
plot(lower, "Lower", color.gray)ta.vwma
pine
ta.vwma(source, length) → series floatThe volume-weighted moving average: the average of source times volume over the average of volume, over length bars.
Each average skips its own na, so the two windows part where only the source has a hole. It reads na where the volume's average is 0.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthseries int | How many bars. |
ta.wad
pine
series float ta.wadThe Williams accumulation/distribution: the running sum of each bar's close against the true range's far end.
ta.wma
pine
ta.wma(source, length) → series floatThe weighted moving average over length bars, weighting the newest bar length and the oldest 1.
A na bar reads na and holds the last number in its slot, and the average waits for length numbers. In a branch, a skipped bar counts as the value the call last read.
| Parameter | Description |
|---|---|
sourceseries int/float | The values to average. |
lengthseries int | How many bars. |
ta.wpr
pine
ta.wpr(length) → series floatWilliams %R: where the close sits between the highest high and lowest low of the last length bars, from -100 to 0.
A flat window reads na.
| Parameter | Default | Description |
|---|---|---|
lengthseries int | 14AmenoScript only | How many bars. |
ta.wvad
pine
series float ta.wvadThe Williams variable accumulation/distribution of one bar: (close - open) / (high - low) * volume.
A bar with no range reads na.
ta.zigzag
AmenoScript only
ameno
ta.zigzag(deviation, depth) → [series float, series float, series bool]A zigzag of price swings: [pivot, direction, isHigh] on the bar a swing of at least deviation percent completes. See AmenoScript's built-ins.
pivot is the swing's extreme and reads na on other bars. direction is 1 after a swing up, -1 after a swing down, and na before the first. isHigh is true when the new pivot is a high. A new pivot waits at least depth bars after the last.
| Parameter | Default | Description |
|---|---|---|
deviationseries int/float | 5 | The smallest swing that counts, in percent. |
depthseries int | 10 | The fewest bars between two pivots. |
ameno
indicator("Zigzag pivots", overlay = true)
[pivot, dir, isHigh] = ta.zigzag(5, 10) // AmenoScript only
plotshape(not na(pivot) and isHigh, "High", shape.circle, location.abovebar, color.red)
plotshape(not na(pivot) and not isHigh, "Low", shape.circle, location.belowbar, color.green)
plot(dir, "Direction", display = display.data_window)