如何在 TradingView 的 PineScript 中突出显示最新信号?

问题描述 投票:0回答:1

我在 Pine Script v5 中遇到一个问题,希望您能帮忙解决。我的脚本运行得很好,但我想让最新的“买入”和“卖出”条件标签和背景阴影更加突出,同时减少旧信号的不透明度。目标是通过仅突出显示最新信号并调暗旧信号来最大程度地减少图表混乱。

目前,代码对所有信号进行颜色和阴影处理,而不仅仅是最近的信号。以下是我的代码的相关部分。你能告诉我我可能做错了什么,或者 Pine 脚本中是否有一个特定的函数可以实现这一点?

我将不胜感激任何有关如何实现这一目标的指导,或者是否可以在 Pine Script 中实现这一目标。

截图结果:

Screenshot results

Pinescript 代码:

// Initialize variables to track the bar index of the most recent buy or sell signal
var int lastBuyIndex = na
var int lastSellIndex = na

// Track the most recent buy or sell condition
if (buyCondition)
    lastBuyIndex := bar_index
if (sellCondition)
    lastSellIndex := bar_index

// Plot Buy and Sell signals for EMA, MACD, RSI, and Volume
plotshape(series=buyCondition, location=location.belowbar, color=lastBuyIndex == bar_index ? color.green : color.new(color.green, 70), style=shape.labelup, text="BUY", textcolor=color.white, size=size.small, offset=0)
plotshape(series=sellCondition, location=location.abovebar, color=lastSellIndex == bar_index ? color.red : color.new(color.red, 70), style=shape.labeldown, text="SELL", textcolor=color.white, size=size.small, offset=0)

// Background shading for buy and sell signals
bgcolor(color = (buyCondition and lastBuyIndex == bar_index) ? color.new(color.green, 15) : color.new(color.green, 90), title="Buy Shading")
bgcolor(color = (sellCondition and lastSellIndex == bar_index) ? color.new(color.red, 15) : color.new(color.red, 90), title="Sell Shading")
pine-script pine-script-v5 tradingview-api trading technical-indicator
1个回答
0
投票

您无法使用

plotshape()
bgcolor()
执行此操作。一旦打印出来,您就无法更改它们的属性。

您将需要使用绘图对象,例如

label
box

以下是使用标签的方法:

var label last_long_label = na

if (buyCondition)
    label.set_color(last_long_label, color.new(color.green, 90))                                // Reduce opacity of the old label
    last_long_label := label.new(bar_index, high, "BUY", color=color.new(color.green, 40))      // Create a new label with more bright color
© www.soinside.com 2019 - 2024. All rights reserved.