Guide

Pine Script Pivot Points: ta.pivot_point_levels Guide

Use ta.pivot_point_levels in Pine Script v6: array order, anchors, six pivot types, extended lines, and a cross-check against hand-built pivots.

•12 min read

You open a chart, add TradingView's Pivot Points Standard, and the lines look right. Then you want the same levels inside your own script, to color bars near them, to fire an alert when price touches R1, or to feed them into a strategy filter. So you start typing the formulas: the prior high, low and close, a division by three, a handful of subtractions. Half an hour later the numbers are close to the built-in indicator but not identical, and you are not sure which one is wrong.

Pine Script v6 has a function for exactly this. ta.pivot_point_levels returns the same set of levels that TradingView's own pivot indicators draw, for six pivot types, anchored to whatever period you choose. This guide covers what it returns, how the anchor works, how to plot and extend the levels, how to cross-check them against a hand-built version, and the pitfalls that catch people the first time.

One naming warning before the code. "Pivot points" in the trading sense, the P, R1 and S1 levels computed from a previous period, have nothing to do with ta.pivothigh and ta.pivotlow, which detect swing highs and lows. If you want swings, read ta.pivothigh and ta.pivotlow: Why Your Pivot Signals Arrive Late. This post is about the period-based levels.

What ta.pivot_point_levels returns

The v6 signature is ta.pivot_point_levels(type, anchor, developing) and it returns an array<float>. Three inputs control it:

  • type is a string: "Traditional", "Fibonacci", "Woodie", "Classic", "DM" or "Camarilla".
  • anchor is a bool that is true on the bar where a new pivot period starts.
  • developing is an optional bool, false by default. False means the levels are calculated once at each anchor and then held. True means they are recalculated continuously from the data since the last anchor.

The array always has 11 elements, in this order: P, R1, S1, R2, S2, R3, S3, R4, S4, R5, S5. Index 0 is the central pivot, odd indexes are resistances and even indexes (from 2) are supports. Not every type defines every level. Per the v6 reference, levels a type does not support come back as na. The DM type, for example, supplies only P, R1 and S1. Rather than memorizing which type fills which slots, write your code to tolerate na in any slot above index 0.

The levels are fixed between anchors. They do not move bar to bar; they change when anchor fires again. That is what you want from a level: a horizontal line that stays put for the whole day, week or month.

The anchor decides everything

Most confusion about pivots comes from the anchor, not the formula. The function does not know what a "day" is. It only knows that on some bars the anchor is true and on the others it is false. Each time it becomes true, the function computes a new set of levels from the period that just ended.

The natural way to build an anchor is timeframe.change, which returns true on the first bar of each new period of the timeframe you pass. timeframe.change("D") gives daily pivots, timeframe.change("W") weekly and timeframe.change("M") monthly. The same idea powers the day-change detection in Pine Script ta.change: Detect a New Day, Week or Session.

Two consequences follow. First, the anchor period should be larger than the chart timeframe. Daily pivots on an hourly chart make sense. Daily pivots on a daily chart produce a new set of levels on every bar, which is rarely what you wanted. Second, you can anchor to anything that is a boolean. A session open, the first bar after a specific hour, or a custom condition all work, because the function only watches for the transition to true. Be aware that on markets that trade around the clock, such as the crypto spot markets, the day boundary depends on the exchange's timezone, so check that your anchor lines up with the one you expect.

A minimal daily pivot indicator

Start with the smallest thing that works: daily Traditional pivots, plotted as three lines.

pine
//@version=6
indicator("Daily Pivots Minimal", overlay=true)

// New day anchor
newDay = timeframe.change("D")

// 11 levels: P, R1, S1, R2, S2, R3, S3, R4, S4, R5, S5
levels = ta.pivot_point_levels("Traditional", newDay)

p  = levels.get(0)
r1 = levels.get(1)
s1 = levels.get(2)

plot(p,  "P",  color.gray,  style=plot.style_linebr)
plot(r1, "R1", color.red,   style=plot.style_linebr)
plot(s1, "S1", color.green, style=plot.style_linebr)

Several details matter here. The call sits at the global scope and runs on every bar, which is how Pine's ta.* functions expect to be used. Using levels.get(0) is the v6 method form of array.get(levels, 0); both work. And plot.style_linebr breaks the line when the value is na, so you do not get a stray diagonal connecting the end of one period to the start of the next. Before the first anchor, every level is na, so the lines start at the first full day rather than at the left edge of the chart.

If you add this to a 1 hour chart you should see three flat lines that step to a new value at each daily boundary. Compare them with the built-in Pivot Points Standard set to Traditional and a daily period. They should sit on top of one another. If they do not, the first thing to check is the anchor, as covered above.

Choose the type and the period with inputs

A single hard-coded type is fine for a test, but the real value of the function is that switching type is just a string. Here is a version that lets you pick the type and the period from the settings dialog and plots every level the type supports.

pine
//@version=6
indicator("Pivot Levels Selectable", overlay=true)

typeInput = input.string("Traditional", "Pivot type",
     options=["Traditional", "Fibonacci", "Woodie", "Classic", "DM", "Camarilla"])
tfInput   = input.timeframe("D", "Pivot period")
developingInput = input.bool(false, "Developing levels")

anchor = timeframe.change(tfInput)
levels = ta.pivot_point_levels(typeInput, anchor, developingInput)

// Reading by index. Unsupported levels are na and simply draw nothing.
p  = levels.get(0)
r1 = levels.get(1)
s1 = levels.get(2)
r2 = levels.get(3)
s2 = levels.get(4)
r3 = levels.get(5)
s3 = levels.get(6)

plot(p,  "P",  color.gray,   style=plot.style_linebr)
plot(r1, "R1", color.red,    style=plot.style_linebr)
plot(s1, "S1", color.green,  style=plot.style_linebr)
plot(r2, "R2", color.maroon, style=plot.style_linebr)
plot(s2, "S2", color.teal,   style=plot.style_linebr)
plot(r3, "R3", color.orange, style=plot.style_linebr)
plot(s3, "S3", color.lime,   style=plot.style_linebr)

Switch the type to Fibonacci and the same indexes now carry Fibonacci levels. Switch it to Camarilla and they carry Camarilla levels, which cluster tightly around the prior close. The code does not change at all. That is the practical argument for the built-in function over hand-written formulas: six formula families behind one call.

The developing checkbox is worth trying once to see what it does. With it on, the levels recompute as the current period develops, so they move during the day. With it off, they stay fixed at their anchor values. One restriction to remember: the reference states that "Woodie" cannot be combined with developing = true, and doing so raises a runtime error. If you expose both as inputs, as above, either leave developing out when Woodie is selected or accept that the script will stop on that combination.

Extend the levels with lines and labels

Plain plot() lines are fine, but traders usually want levels drawn as horizontal rays that begin at the period start, carry a label, and stop when the next period begins. Drawing objects do that, and the anchor tells you exactly when to create and when to close them.

pine
//@version=6
indicator("Pivot Levels Extended", overlay=true, max_lines_count=100, max_labels_count=100)

typeInput = input.string("Traditional", "Pivot type",
     options=["Traditional", "Fibonacci", "Woodie", "Classic", "DM", "Camarilla"])
tfInput   = input.timeframe("D", "Pivot period")

anchor = timeframe.change(tfInput)
levels = ta.pivot_point_levels(typeInput, anchor)

names = array.from("P", "R1", "S1", "R2", "S2", "R3", "S3", "R4", "S4", "R5", "S5")
var array<line> active = array.new<line>()

if anchor
    // Close the previous period's rays at the bar where the new period starts
    for ln in active
        ln.set_extend(extend.none)
        ln.set_x2(bar_index)
    active.clear()

    for i = 0 to 10
        lvl = levels.get(i)
        if not na(lvl)
            col = i == 0 ? color.gray : (i % 2 == 1 ? color.red : color.green)
            active.push(line.new(bar_index, lvl, bar_index + 1, lvl, extend=extend.right, color=col))
            label.new(bar_index, lvl, names.get(i), style=label.style_none, textcolor=col)

The structure is the standard one for period-based drawings. A var array holds the lines that are currently live. When the anchor fires, the old lines are cut off at the current bar and the array is emptied. Then one new ray is created per level that is not na. Because the array has a fixed order, the loop can name each level from the names array and color resistances red and supports green by index parity. Each period leaves behind a set of finished segments, so you can scroll back and see where the levels were on earlier days.

The line and label limits matter here. A day of 11 levels over 30 days is 330 lines, which is why the script raises max_lines_count and max_labels_count. TradingView's drawing limits are covered in Pine Script Drawing Objects: Lines, Boxes, and Labels That Do Not Hit the Limit. If you only care about the recent past, lower the limits and let old drawings fall off the end.

Cross-check against a hand-built version

You should not trust any level function blindly, including a built-in one, until you have seen it agree with something you built yourself. The Traditional pivot has a short, well known formula: the pivot is the average of the previous period's high, low and close, resistance 1 is twice the pivot minus the low, and support 1 is twice the pivot minus the high. Build that with request.security, using the non-repainting pattern of reading the prior bar's values with the lookahead flag on, and plot the difference.

pine
//@version=6
indicator("Pivot Cross-Check")

[h, l, c] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]],
     lookahead=barmerge.lookahead_on)

manualP  = (h + l + c) / 3
manualR1 = 2 * manualP - l
manualS1 = 2 * manualP - h

levels = ta.pivot_point_levels("Traditional", timeframe.change("D"))

plot(manualP  - levels.get(0), "P diff",  color.gray)
plot(manualR1 - levels.get(1), "R1 diff", color.red)
plot(manualS1 - levels.get(2), "S1 diff", color.green)

All three lines should hug zero once the first full day has passed. If one of them drifts, you have learned something real about your setup: usually a difference in where the period boundary falls, or in which bars count as part of the previous period. That is far better to discover in a test script than inside a strategy. For a number to compare against outside TradingView, the free Pivot Points Calculator computes the same levels from a high, low and close you type in. The pattern behind the request.security call is explained in Pine Script Previous Day High and Low: Plot Them Right.

Pitfalls that catch people the first time

The first pitfall is na handling. Because unsupported levels are na, any comparison against them is false and any arithmetic with them is na. A check such as close > r4 will quietly never fire on a type that does not fill R4, and the script will compile and run without an error. Guard with not na(level) when the logic depends on a specific slot.

The second is the first period. Until the first anchor completes there are no levels, so conditions built on them stay false for the opening stretch of chart history. Plan for that when you are testing how many bars a script has to work with.

The third is mixing up the level order. The array is P, R1, S1, R2, S2 and so on, with the resistance first in each pair. It is not P, R1, R2, R3 followed by the supports. Wrapping the indexes in named variables once, at the top of the script, saves you from off-by-one mistakes later.

The fourth is the timeframe on which you read the result. Anchoring to a larger period is safe on lower timeframes. Reading daily levels on a weekly chart does not give you more than one level set per week at best, so choose the period for the chart you actually use.

The fifth is expecting the levels to mean something on their own. They are arithmetic on a prior period's prices. A script can draw them accurately and still tell you nothing about what happens next, and nothing in this post suggests they do. Treat them as reference lines whose behavior you check on past data rather than assume.

Mark it on the chart with the agent

Writing a script is one way to get levels onto a chart. When you want to look at a chart first and code afterwards, PineScripter's chart agent gives you a way to see the idea before you build it. Pivot levels are computed from prior periods, but the lines that matter on the chart are often the ones price has already reacted to, so a useful first look is a comparison between the two.

The request you would type in the chat is plain English: "Mark the swing highs and lows on BTCUSDT 1h and draw the levels they cluster into as horizontal lines."

Using only what the product does, the agent reads the chart, including the swing levels with their touch counts and any drawings already on it. Then it draws with the toolbar's drawing tools while you watch its cursor click the points, and it can switch market or timeframe if you ask. Each batch it draws can be undone with one action, so a first attempt that is not what you wanted costs you nothing.

What you do next is the important part. Treat the markup as a starting point and check it yourself. The touch counts tell you how many times price reached each level, so you can judge for yourself whether a line deserves its place. Switch the timeframe and see whether the same levels survive. Open the chart in replay and step through it one bar at a time to watch how price behaved around a level as it formed, a method described in Check a Pine Script Before and After It Goes Live. Then lay the daily pivot indicator from this post over the same chart and compare where the lines fall. Sometimes they agree, often they do not, and either way you now have evidence from the chart, not a guess.

The agent marks what you ask for and describes the chart. It does not decide which levels will hold and it does not trade. The optional next step is turning the markup into something reusable: ask for an indicator that draws the period levels on every chart, and use the extended-lines script above as the base. You can read how the agent fits into the workspace in What Is PineScripter? An AI Agent for Charts and Pine Script.

Add alerts and checks once the levels work

Once the levels are on the chart, the natural next step is to react to them, for example by detecting when price crosses the central pivot. The crossing logic is the same as for any moving average and is covered in ta.crossover and ta.crossunder in Pine Script Explained. Because the pivot is fixed for the whole period, a cross against it is well defined, but you should still evaluate it on a confirmed bar, as discussed in barstate.isconfirmed in Pine Script: Signals That Stop Repainting.

If you describe the behavior you want in plain English to PineScripter's AI editor, it writes Pine Script v6, checks it for compile errors, fixes them itself, and edits only the lines that change when you ask for a follow-up. You can then run the script on real market data in the chart to see what it draws before pasting it into TradingView. The crypto spot markets are what the chart covers, so test there and then paste into TradingView for the markets you trade.

Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.

Frequently asked questions

What does ta.pivot_point_levels return?

An array of 11 floats in the order P, R1, S1, R2, S2, R3, S3, R4, S4, R5, S5. Levels a pivot type does not support are na.

Is ta.pivot_point_levels the same as ta.pivothigh?

No. ta.pivothigh and ta.pivotlow detect swing highs and lows. ta.pivot_point_levels computes period-based levels such as P, R1 and S1 from the previous period.

What should I use as the anchor?

A bool that is true on the first bar of each new period. timeframe.change("D") gives daily levels, "W" weekly and "M" monthly. Keep the period larger than the chart timeframe.

What does the developing parameter do?

With developing false, the default, levels are fixed at each anchor. With true they are recalculated from data since the anchor. Woodie cannot be used with developing true.

Why are some of my pivot levels na?

Not every type defines every level, and there are no levels before the first anchor completes. Guard comparisons with not na(level).

Ask the agent to mark it on the chart, then turn it into Pine Script →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.