October 5, 2022

Pine Script™ now does vertical gradients!

The following new overloads for the fill() function can create vertical gradients:

fill(plot1, plot2, top_value, bottom_value, top_color, bottom_color)

fill(hline1, hline2,  top_value, bottom_value, top_color, bottom_color)

All parameters in the new overloads accept series arguments. They create a vertical gradient between the `top_color` and the `bottom_color` in the space between the `top_value` and the `bottom_value`. The plots or hlines which IDs are used in the first two arguments act as a mask over the gradient, determining which portion of the gradient is visible.

See here how we give the old MACD a new look with two vertical gradients, one to fill the space between the two moving averages, and another for what is usually represented as a histogram:

//@version=5
indicator("MACD")
[macd, signal, hist] = ta.macd(close, 12, 26, 9)

// Histogram
float  maxHist  = ta.highest(hist, 100)
float  minHist  = ta.lowest(hist,  100)
bool   histBull = hist > 0
color  topHistColor = histBull ? color.new(color.green, 90) : color.red
color  botHistColor = histBull ? color.green : color.new(color.red, 90)
float  topHistValue = histBull ? maxHist : 0
float  botHistValue = histBull ? 0 : minHist
histPlot   = plot(hist, "Histogram", color(na))
centerPlot = plot(0,    "Middle",    color(na))
fill(histPlot, centerPlot, topHistValue, botHistValue, topHistColor, botHistColor)

// Averages
float  maxLine  = ta.highest(math.max(macd, signal), 100)
float  minLine  = ta.lowest(math.min(macd, signal), 100)
bool   lineBull = macd > signal
color  topLineColor = lineBull ? color.new(color.lime, 90) : color.fuchsia
color  botLineColor = lineBull ? color.lime : color.new(color.fuchsia, 90)
float  topLineValue = lineBull ? maxLine : maxLine
float  botLineValue = lineBull ? minLine : minLine
macdPlot   = plot(macd,   "MACD",   color.gray,   1)
signalPlot = plot(signal, "Signal", color.silver, 1)
fill(macdPlot, signalPlot, topLineValue, botLineValue, topLineColor, botLineColor)

In our next example, we create a Pine forest using one gradient for the background and another for the RSI-based tree line:

//@version=5
indicator("Gradient Fill: Night in the Pine forest")

// Sky
skyTopColor = input.color(color.rgb(144, 191, 249, 1))
skyBotColor = input.color(color.rgb(251, 192, 45, 1))
skyPlot     = plot(100, color = skyTopColor)
groundPlot  = plot(0, color = color.black)
fill(skyPlot, groundPlot, 100, 0, skyTopColor, skyBotColor)

// Trees
crownsVal = ta.rsi(close, 20)
treesPlot = plot(crownsVal, color = color.rgb(6, 126, 116))
fill(treesPlot, groundPlot, crownsVal, 0, color.rgb(6, 126, 116), color.black)

The following are a few more examples published by some of our PineCoders in Community Scripts:

Delta Volume Channels indicator by LucF

RSI – colour fill indicator by Fikira

Webby’s RSI indicator by JohnMuchow

To stay informed of new Pine Script™ features, keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box on Telegram, its Twitter account, and from the Pine Script™ Q&A public chat on TradingView.

We hope you find this highly-requested feature useful. Please keep sending us your feedback and suggestions for improvement. We build TradingView for you, and we’re always keen to hear from you.

Look first / Then leap

Launch Chart