我怎样才能让 Pine Script 只显示一个红色或一个绿色的交易指示?

How can I tell Pine Script to show me only one red or one green indication for entering a trade?

我在 Pine Script 中制作了一个指示器,只要满足所有条件,它就会显示红色或绿色菱形。 但是,只要条件有效,pine 脚本就会在每根蜡烛上打印出一颗钻石。

该脚本使用 50 和 200 EMA、DMI+/- 和 ADX、RSI 运行

查看参考图片: Multiple red/green diamonds

我希望它只显示一颗红色钻石,直到出现绿色,而不是连续显示多个红色钻石。

代码如下:

//@version=5
indicator("Master Chef", shorttitle="Master Chef", overlay=true, timeframe="", timeframe_gaps=true)

//===== Moving Averages =====//
ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na

show_ma1   = input(true   , "MA №1", inline="MA #1")
ma1_type   = input.string("SMA"  , ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma1_source = input(close  , ""     , inline="MA #1")
ma1_length = input.int(20     , ""     , inline="MA #1", minval=1)
ma1_color  = input(#f6c309, ""     , inline="MA #1")
ma1 = ma(ma1_source, ma1_length, ma1_type)
plot(show_ma1 ? ma1 : na, color = ma1_color, title="MA №1")

show_ma2   = input(true   , "MA №2", inline="MA #2")
ma2_type   = input.string("SMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma2_source = input(close  , ""     , inline="MA #2")
ma2_length = input.int(50     , ""     , inline="MA #2", minval=1)
ma2_color  = input(#fb9800, ""     , inline="MA #2")
ma2 = ma(ma2_source, ma2_length, ma2_type)
plot(show_ma2 ? ma2 : na, color = ma2_color, title="MA №2")

show_ma3   = input(true   , "MA №3", inline="MA #3")
ma3_type   = input.string("SMA"  , ""     , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma3_source = input(close  , ""     , inline="MA #3")
ma3_length = input.int(100    , ""     , inline="MA #3", minval=1)
ma3_color  = input(#fb6500, ""     , inline="MA #3")
ma3 = ma(ma3_source, ma3_length, ma3_type)
plot(show_ma3 ? ma3 : na, color = ma3_color, title="MA №3")

show_ma4   = input(true   , "MA №4", inline="MA #4")
ma4_type   = input.string("SMA"  , ""     , inline="MA #4", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma4_source = input(close  , ""     , inline="MA #4")
ma4_length = input.int(200    , ""     , inline="MA #4", minval=1)
ma4_color  = input(#f60c0c, ""     , inline="MA #4")
ma4 = ma(ma4_source, ma4_length, ma4_type)
plot(show_ma4 ? ma4 : na, color = ma4_color, title="MA №4")

//===== RSI =====//
lenRSI = input.int(14, minval=1, title="Length")
src = input(close, "Source")
upRSI = ta.rma(math.max(ta.change(src), 0), lenRSI)
downRSI = ta.rma(-math.min(ta.change(src), 0), lenRSI)
rsi = downRSI == 0 ? 100 : upRSI == 0 ? 0 : 100 - (100 / (1 + upRSI / downRSI))

//===== DMI+/-=====//
lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
lenDMI = input.int(14, minval=1, title="DI Length")
upDMI = ta.change(high)
downDMI = -ta.change(low)
plusDMI = na(upDMI) ? na : (upDMI > downDMI and upDMI > 0 ? upDMI : 0)
minusDMI = na(downDMI) ? na : (downDMI > upDMI and downDMI > 0 ? downDMI : 0)
trur = ta.rma(ta.tr, lenDMI)
plus = fixnan(100 * ta.rma(plusDMI, lenDMI) / trur)
minus = fixnan(100 * ta.rma(minusDMI, lenDMI) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

//===== Defining parameters =====//
//Long
long = rsi >= 50 and rsi <= 90 and plusDMI > minusDMI and adx >= 25
//Short
short = rsi <= 50 and rsi >= 10 and plusDMI < minusDMI and adx >= 25

//===== Plot signals to chart =====//
//Long
plotshape(long, title = "Long", location = location.belowbar, color = color.rgb(0, 250, 0), size = size.tiny, style = shape.diamond)
//Short
plotshape(short, title = "Short", location = location.abovebar, color = color.rgb(250, 0, 0), size = size.tiny, style = shape.diamond)

//
//===== Create Alerts =====//
alertcondition(long or short, title = "Master Chef", message= " Signal: {{ticker}}, {{interval}}")

在此先感谢您的帮助!

您可以使用两个额外的变量来查看您是否已经做多 (isLong) 或做空 (isShort)。然后你的初始 long 信号只会是 true 当你还没有做多时反之亦然。

//===== Defining parameters =====//
var isLong = false
var isShort = false

//Long
long = not isLong and rsi >= 50 and rsi <= 90 and plusDMI > minusDMI and adx >= 25
//Short
short = not isShort and rsi <= 50 and rsi >= 10 and plusDMI < minusDMI and adx >= 25

if (long)
    isLong := true
    isShort := false
    
if (short)
    isLong := false
    isShort := true

//===== Plot signals to chart =====//
//Long
plotshape(long, title = "Long", location = location.belowbar, color = color.rgb(0, 250, 0), size = size.tiny, style = shape.diamond)
//Short
plotshape(short, title = "Short", location = location.abovebar, color = color.rgb(250, 0, 0), size = size.tiny, style = shape.diamond)