Guide

Pine Script Plot Offset and Shifted Plots Explained

How plot offset works in Pine Script v6, how it differs from the [] operator, and how to find the line that shifts a plot, with compilable examples.

•12 min read

You add a moving average to your chart, glance at the screen, and something is off. The line sits a few bars to the right of where it should be, or a pivot marker lands five bars after the swing it is supposed to mark, or a cloud stretches into the empty space beyond the last candle and you cannot tell whether that is a bug. Somewhere in the script there is an offset argument, or a [1], or a negative number you copied from an old indicator, and you are no longer sure which one is doing what.

This guide explains exactly what the offset parameter of plot() does in Pine Script v6, how it differs from the history-referencing operator [], why a negative offset can make an indicator look better than it really was, and how to find the one line responsible when a plot looks shifted. Every example is complete and compiles in v6, so you can paste it into the Pine Editor and move the numbers around yourself.

What the offset parameter actually does

Pine Script draws a plot by assigning one value to each bar. The offset argument of plot() changes which bar that value is drawn on. A positive offset moves the whole line to the right, toward the present and past the last bar into empty space. A negative offset moves it to the left, onto older bars. The unit is bars, not time, so an offset of 5 means five bars on whatever timeframe the chart is showing, whether that is five minutes or five days.

The important part is what offset does not do. It does not change the numbers the script calculates. The series stays exactly as computed, bar by bar. Only the place where the chart paints each value moves. That is why a shifted plot can look wrong while every calculation behind it is right, and why shifting a line never changes what a condition built from the original series sees.

Here is the smallest useful demonstration. It plots a 20-bar simple moving average twice, once in place and once shifted by an amount you choose in the settings.

pine
//@version=6
indicator("Plot offset demo", overlay = true)

shift = input.int(5, "Shift in bars", minval = -50, maxval = 50)

basis = ta.sma(close, 20)

plot(basis, "SMA 20", color = color.new(color.gray, 60))
plot(basis, "SMA 20 shifted", color = color.orange, offset = shift)

Set the shift to 5 and the orange line trails the gray one by five bars. Set it to -5 and the orange line runs ahead of the gray one, which looks as if the average knew what price was going to do. It did not know anything. It is the same value, painted five bars earlier than the bar it was calculated on. Hold on to that observation, because it is the root of the most important pitfall in this article.

One more detail: the offset argument is a simple integer. You can drive it from an input.int() as above, or from a constant, but you cannot compute a different offset on every bar. If you need a value that varies per bar, you are looking for the history operator or a drawing object, which both come up below.

Offset is not the same as the history operator

Pine Script has a second way to reach for an older value: the square brackets. close[1] is the close of the previous bar, and slow[3] is the value of slow three bars ago. People often treat plot(x, offset = 3) and plot(x[3]) as interchangeable, and on the screen they look almost identical, but they are different operations.

The history operator changes the data. When you write slow[3], you create a new series whose value on each bar is what slow was three bars earlier. Any condition, crossover, or alert built from that series sees the delayed values. The offset parameter only changes the drawing, so a condition built from slow ignores it completely.

This is the single most common cause of a script that "looks right and behaves wrong". The next example plots a slow EMA shifted three bars to the right, then marks a crossover. Look closely at which series the crossover uses.

pine
//@version=6
indicator("Offset does not move the condition", overlay = true)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)

plot(fast, "Fast EMA", color = color.teal)
plot(slow, "Slow EMA shifted 3", color = color.purple, offset = 3)

// This crossover uses the unshifted slow EMA.
crossed = ta.crossover(fast, slow)

plotshape(crossed, "Cross", style = shape.circle, location = location.bottom, color = color.teal, size = size.tiny)
alertcondition(crossed, "Fast crossed slow", "Fast EMA crossed above slow EMA")

On the chart, the circles appear at the crossing points of the unshifted lines, which do not line up with where the teal line meets the purple line you can see. Anyone reading that chart will think the markers are misplaced, when the markers are in fact the honest ones: the purple line is the one that was moved.

If you want the condition to agree with what the reader sees, build the shifted series first and use it everywhere.

pine
//@version=6
indicator("Shift the data, not just the drawing", overlay = true)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
lagged = slow[3]

plot(fast, "Fast EMA", color = color.teal)
plot(lagged, "Slow EMA lagged 3", color = color.purple)

crossed = ta.crossover(fast, lagged)
plotshape(crossed, "Cross", style = shape.circle, location = location.bottom, color = color.teal, size = size.tiny)

Now the circles sit exactly where the visible lines cross. The purple line looks the same as the offset version, with one difference worth knowing: slow[3] stops at the last bar, while plot(slow, offset = 3) continues three bars into the empty area to the right of the chart. That extension is the only visual difference, and it is the reason offset exists as a plot feature in the first place.

Positive offsets and the area to the right of the chart

A positive offset is how an indicator draws into the future part of the chart, the blank strip past the latest bar. Ichimoku Cloud is the classic example. Its two leading spans are calculated from today's data and then plotted 25 bars forward, so the cloud on the chart is a projection of values that were computed in the past, not a forecast of anything. The script is not predicting. It is drawing an old calculation further to the right.

The same idea works for any level you want to project forward so it stays readable next to the live candle. Here is a midline of the recent range, drawn in place and projected one full lookback to the right.

pine
//@version=6
indicator("Projected midline", overlay = true)

len = input.int(26, "Length", minval = 2)

mid = math.avg(ta.highest(high, len), ta.lowest(low, len))

plot(mid, "Midline", color = color.blue)
plot(mid, "Midline projected", color = color.new(color.blue, 50), offset = len)

There is a ceiling here. Plots can reach a limited number of bars into the future, so a very large positive offset is clipped by the platform rather than drawn in full. For long projections, a line or box drawing object, which takes an exact bar index or time, is the better tool. Our guide to Pine Script drawing objects covers their limits. If you want to see the Ichimoku offsets in a complete script, the Ichimoku Cloud build walks through them line by line.

Negative offsets and why they flatter an indicator

A negative offset moves the plot onto older bars, and that is where indicators start to lie without any error appearing. When you shift a value to the left, you are placing a number on a bar that did not have that number at the time. A reader scanning the chart sees a marker sitting neatly on a past high, or a line that turns exactly before price turns, and concludes the indicator is remarkably precise. It is precise only in hindsight.

Pivot detection shows the legitimate version of this clearly. ta.pivothigh(high, 5, 5) cannot confirm a pivot until five bars have closed after it, so the function returns the pivot's value on the bar where it is confirmed, five bars after the actual high. The honest way to mark the real swing is to shift the marker back by the number of right-hand bars, with the understanding that the marker was only knowable five bars later.

pine
//@version=6
indicator("Pivot marked on the pivot bar", overlay = true)

left = input.int(5, "Left bars", minval = 1)
right = input.int(5, "Right bars", minval = 1)

ph = ta.pivothigh(high, left, right)
pl = ta.pivotlow(low, left, right)

plotshape(not na(ph), "Pivot high", style = shape.triangledown, location = location.abovebar, color = color.red, offset = -right)
plotshape(not na(pl), "Pivot low", style = shape.triangleup, location = location.belowbar, color = color.green, offset = -right)

This is correct drawing, because the markers land on the bars that really were the pivots. It is also a lookback trap: someone who screenshots the chart sees triangles that appear to call every turn, when each one arrived five bars late. The detailed explanation is in why pivot results arrive late, and the broader pattern is covered in the lookback bias problem.

The rule to remember is simple. A negative offset is fine for labeling where something happened. It is wrong as an input to anything that claims to act at that earlier bar. If a condition, an alert, or a strategy order depends on the left-shifted value, you have built a script that uses information from the future, and its historical behavior will not match live behavior. Our article on repainting lists the other ways this happens.

Offsets on other plotting functions

The plot family shares the idea. plotshape(), plotchar(), and plotarrow() accept an offset parameter that works the same way: it moves the drawn marker by whole bars without touching the data. The pivot example above used it on plotshape().

Two other functions that look similar behave differently. bgcolor() and barcolor() take an offset as well, which is how you can tint the background behind a confirmed event instead of the bar where it was confirmed, for example. Drawing objects such as line.new() and label.new() do not use an offset in bars at all. They take explicit bar indexes or timestamps, which gives you per-bar control that the simple-integer offset cannot.

Most built-in moving average functions, ta.sma() and ta.ema() among them, have no offset argument. If you have seen a moving average with a "shift" setting on another platform, in Pine Script that shift is done by passing offset to plot() or by using [] on the result. Our guide to moving average types shows the standard functions.

How to debug a plot that looks shifted

When a line sits in the wrong place, the quickest way to find out why is to ask four questions in order.

First, is there an offset argument on the plot() call, and what is its sign? A positive value shifts right and a negative one shifts left. Many "mysterious" shifts are an offset left over from a copied script.

Second, is there a history operator in the expression being plotted? plot(close[1]) and plot(close, offset = 1) look the same on the chart, but only one of them changes the series.

Third, does the value come from a higher timeframe through request.security()? Higher-timeframe values are held across many lower-timeframe bars and can look displaced by a full higher-timeframe candle, with no offset anywhere in the script. The behavior depends on the lookahead setting, covered in how request.security works and the lookahead guide.

Fourth, is the indicator inherently late? Pivots, zigzags, and anything that needs future bars to confirm will always arrive after the event. That is the definition of the calculation, not a bug in your plot.

To check the answer rather than guess it, make the script show its own offset. A pair of labels is enough: print the raw value on the current bar and the value you plotted, then compare them on a single known bar.

pine
//@version=6
indicator("Offset audit", overlay = true)

shift = input.int(3, "Offset to test")
basis = ta.ema(close, 14)

plot(basis, "EMA", color = color.gray)
plot(basis, "EMA offset", color = color.orange, offset = shift)

if barstate.islast
    label.new(bar_index, high, "EMA now: " + str.tostring(basis, format.mintick) + "\nOffset: " + str.tostring(shift), style = label.style_label_down)

Hover over the orange line at any bar and compare the value in the data window with the gray line shift bars earlier (for a positive shift): they match, because the number is the same one, just drawn elsewhere. Our guide to debugging Pine Script when there is no error message covers more ways to inspect values like this.

Fix a misaligned plot with PineScripter

If a script you did not write has a plot in the wrong place, you do not have to read every line to find the cause. In the PineScripter AI editor you can paste the script and describe the problem the way you would say it to a colleague, for example: "The orange line is drawn three bars to the right of the EMA it is based on. Find out why and make it line up." The AI edits only the lines that change, and the Code Changes view shows each edit line by line, so you can see whether it removed an offset argument, moved a [], or left the plot alone and explained why.

To confirm the result with your own eyes, run the script in the PineScripter chart on real market data. The chart covers 308 crypto spot markets across 14 timeframes, up to 5,000 bars per run. Then use replay to step through it one bar at a time and watch the exact bar where each marker appears. A marker that shows up five bars after the move it points at is a confirmation delay, and replay makes that visible in a way that a finished chart cannot. You review the diff and decide whether to keep it, and you paste the final code into TradingView yourself.

Common mistakes with plot offsets

A few mistakes show up repeatedly in scripts people bring in for repair.

The first is using a negative offset to make a plot "line up with the signal bar" and then feeding the shifted series into a condition. As covered above, the condition cannot see the shift, so the result is a chart and a rule that disagree.

The second is stacking shifts. A script with both ema[2] and offset = 2 on the same line is delayed by four bars, and whoever reads it later has no idea why. Pick one mechanism per plot and comment why.

The third is expecting offsets to be time-aware. They count bars. On a chart with gaps, such as a session chart, five bars is not always the same amount of clock time.

The fourth is trusting a shifted plot in a screenshot. If you share an indicator with a negative offset, say so, because a reader cannot tell a hindsight marker from a live one by looking.

Where to go from here

The offset parameter of plot() moves drawings, the [] operator moves data, and the difference between them explains most shifted-plot problems. Positive offsets project a calculation into the empty area to the right, negative offsets place a late-confirmed value back onto the bar it describes, and neither changes what a condition sees. When something looks misaligned, check the sign, check for a history operator, check for a higher-timeframe request, and then decide whether the delay is a bug or simply how the calculation works.

If you would rather skip the hunt, describe the misalignment to the editor at PineScripter, review the line-by-line diff, and keep a copy of the version that lines up.

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.

Back to Blog

Frequently asked questions

What does offset do in plot() in Pine Script?

It moves where each value is drawn by a whole number of bars. A positive offset draws the line to the right, a negative offset to the left. The calculated values do not change.

Is plot offset the same as using [1] or [3]?

No. The history operator creates a new series with older values, so conditions and alerts see it. Offset only moves the drawing, so conditions built from the original series ignore it.

Can I use a different offset on every bar?

No. The offset parameter of plot() is a simple integer, so it can come from a constant or an input but not a per-bar calculation. For per-bar placement use drawing objects such as label.new or line.new.

Why do my pivot markers appear late or early?

ta.pivothigh and ta.pivotlow confirm a pivot only after the right-hand bars have closed. The marker lands on the confirmation bar unless you shift it back by the right-hand bar count with a negative offset.

Does a negative offset make an indicator repaint?

It makes the chart show values on bars that did not know them at the time, which flatters historical results. Never feed a left-shifted value into an alert or order condition.

Describe your indicator in plain English and get Pine Script that compiles →


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.