Pine Script linefill.new: fill the space between two lines
How linefill.new works in Pine Script v5 and v6, a working channel example, how it differs from fill(), and how to update or delete a linefill.
linefill.new(line1, line2, color) colours the space between two lines made with line.new(). It is the drawing version of fill(): fill() joins two plots across every bar, while a linefill joins two drawn lines, wherever they are on the chart. SuperCharting supports linefill.new and its setters in Pine Script v5 and v6.
A working example: a channel of the last 30 bars
//@version=6
indicator("30-bar channel", overlay = true)
len = input.int(30, "Bars")
hi = ta.highest(high, len)
lo = ta.lowest(low, len)
var line top = na
var line bot = na
var linefill zone = na
if barstate.islast
line.delete(top)
line.delete(bot)
top := line.new(bar_index - len, hi, bar_index, hi, color = color.teal)
bot := line.new(bar_index - len, lo, bar_index, lo, color = color.teal)
zone := linefill.new(top, bot, color.new(color.teal, 85))
The two lines mark the highest high and the lowest low of the last 30 bars, and the linefill shades the range between them.
How it behaves
- The fill follows its lines. Move a line with
line.set_xy1()orline.set_y2()and the fill moves with it. Extended lines (extend.right,extend.both) extend the fill too. - Delete a line and the fill goes with it.
line.delete()on either line removes the linefill.linefill.delete(zone)removes only the fill. - One linefill per pair. Calling
linefill.new()again on the same two lines replaces the old fill instead of stacking a second one. - Change the colour later with
linefill.set_color(zone, newColor).linefill.get_line1()andlinefill.get_line2()return the two lines. - With
force_overlay, the fill is drawn on the price chart when both of its lines useforce_overlay = true.
linefill or fill?
| Use | When |
|---|---|
fill(plot1, plot2, color) | The two edges are series values on every bar, such as Bollinger Bands. |
linefill.new(line1, line2, color) | The edges are drawn lines: a channel, a range box between two levels, a trend channel from two pivots. |
How to try it
- Open a chart, for example
NSE:NIFTYon the 15m interval. - Open the Pine Editor, paste the example and click Add to chart.
- The shaded channel appears over the last 30 bars and moves with every new bar.
The free Pine Script checker checks a script without an account.
Frequently asked questions
Why does linefill.new draw nothing?
Both arguments must be lines that exist. If either is na or was deleted, there is nothing to fill. Create the lines first, then the linefill.
Can a linefill have a gradient?
No. A linefill has one colour. For a gradient, use fill() between two plots with top_color and bottom_color.