Pine Script™现在可以垂直渐变!

Oct 5, 2022

fill()函数的以下新重载可以创建垂直渐变:

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

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

新重载中的所有参数都接受系列参数。它们在`top_value``bottom_value`之间的空间中创建`top_color``bottom_color`之间的垂直渐变。在前两个参数中使用了ID的绘图或线充当了渐变的掩码,确定渐变的哪个部分是可见的。

看这里我们如何用两个垂直渐变为旧MACD赋予新外观,一个填充两个移动平均线之间的空间,另一个用于通常表示为直方图的内容:

//@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)

在我们的下一个示例中,我们使用一个渐变作为背景,使用另一个渐变作为基于RSI的树线创建一个Pine:

//@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)

以下是我们的一些PineCoders在社区脚本中发布的更多示例:

LucF提供的Delta Volume Channels

Fikira提供的RSI – colour fill

JohnMuchow提供的Webby’s RSI

要随时了解新的Pine Script™功能,请留意用户手册的发行说明PineCoders帐户还通过Telegram上的Squawk BoxTwitter帐户和TradingView上的Pine Script™ Q&A公共聊天广播更新。

我们希望大家发现这个高需求度的功能很有用。请继续向我们发送您的反馈和改进建议。我们为您打造TradingView,我们总是渴望收到您的来信。


 

还没有关注我们的中文微信公众号?快来扫二维码吧!

Look first Then leap

TradingView专门为您而打造,请确保您充分利用我们出色的功能
开启图表