Pine script 当所有3条超趋势线均为绿色/红色且仅显示一次信号时,如何添加买入/卖出信号?

Pine script 当所有3条超趋势线均为绿色/红色且仅显示一次信号时,如何添加买入/卖出信号?,pine-script,Pine Script,代码如下: //@version=4 study("Supertrend", overlay = true, format=format.price, precision=2, resolution="") showsignals_3 = input(title="Show Triple Buy/Sell Signal", type=input.bool, defval=true) Periods = input(title=&quo

代码如下:


//@version=4
study("Supertrend", overlay = true, format=format.price, precision=2, resolution="")
showsignals_3 = input(title="Show Triple Buy/Sell Signal", type=input.bool, defval=true)
Periods = input(title="ATR Period", type=input.integer, defval=12, group="Supertrend 1")
src = input(hl2, title="Source", group="Supertrend 1")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0, group="Supertrend 1")
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true, group="Supertrend 1")
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false, group="Supertrend 1")
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true, group="Supertrend 1")
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
///alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
///alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
///alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")


/////


Periods_1 = input(title="ATR Period", type=input.integer, defval=10, group="Supertrend 2")
src_1 = input(hl2, title="Source", group="Supertrend 2")
Multiplier_1 = input(title="ATR Multiplier_1", type=input.float, step=0.1, defval=1.0, group="Supertrend 2")
changeATR_1= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true, group="Supertrend 2")
showsignals_1 = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false, group="Supertrend 2")
highlighting_1 = input(title="Highlighter On/Off ?", type=input.bool, defval=true, group="Supertrend 2")
atr2_1 = sma(tr, Periods_1)
atr_1= changeATR_1 ? atr(Periods_1) : atr2_1
up_1=src_1-(Multiplier_1*atr_1)
up_11 = nz(up_1[1],up_1)
up_1 := close[1] > up_11 ? max(up_1,up_11) : up_1
dn_1=src_1+(Multiplier_1*atr_1)
dn_11 = nz(dn_1[1], dn_1)
dn_1 := close[1] < dn_11 ? min(dn_1, dn_11) : dn_1
trend_1 = 1
trend_1 := nz(trend_1[1], trend_1)
trend_1 := trend_1 == -1 and close > dn_11 ? 1 : trend_1 == 1 and close < up_11 ? -1 : trend_1
up_1Plot = plot(trend_1 == 1 ? up_1 : na, title="Up_1 Trend_1", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal_1 = trend_1 == 1 and trend_1[1] == -1
plotshape(buySignal_1 ? up_1 : na, title="Up_1Trend_1 Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal_1 and showsignals_1 ? up_1 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dn_1Plot = plot(trend_1 == 1 ? na : dn_1, title="Down Trend_1", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal_1 = trend_1 == -1 and trend_1[1] == 1
plotshape(sellSignal_1 ? dn_1 : na, title="DownTrend_1 Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal_1 and showsignals_1 ? dn_1 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot_1 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
fill(mPlot_1, up_1Plot, title="Up_1Trend_1 Highligter", color=longFillColor)
fill(mPlot_1, dn_1Plot, title="DownTrend_1 Highligter", color=shortFillColor)
///alertcondition(buySignal_1, title="Supertrend Buy", message="Supertrend Buy!")
///alertcondition(sellSignal_1, title="Supertrend Sell", message="Supertrend Sell!")
changeCond_1 = trend_1 != trend_1[1]
///alertcondition(changeCond_1, title="Supertrend Direction Change", message="Supertrend has changed direction!")


/////

Periods_2 = input(title="ATR Period", type=input.integer, defval=11, group="Supertrend 3")
src_2 = input(hl2, title="Source", group="Supertrend 3")
Multiplier_2 = input(title="ATR Multiplier_2", type=input.float, step=0.1, defval=2.0, group="Supertrend 3")
changeATR_2= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true, group="Supertrend 3")
showsignals_2 = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=false, group="Supertrend 3")
highlighting_2 = input(title="Highlighter On/Off ?", type=input.bool, defval=true, group="Supertrend 3")
atr2_2 = sma(tr, Periods_2)
atr_2= changeATR_2 ? atr(Periods_2) : atr2_2
up_2=src_2-(Multiplier_2*atr_2)
up_212 = nz(up_2[1],up_2)
up_2 := close[1] > up_212 ? max(up_2,up_212) : up_2
dn_2=src_2+(Multiplier_2*atr_2)
dn_212 = nz(dn_2[1], dn_2)
dn_2 := close[1] < dn_212 ? min(dn_2, dn_212) : dn_2
trend_2 = 1
trend_2 := nz(trend_2[1], trend_2)
trend_2 := trend_2 == -1 and close > dn_212 ? 1 : trend_2 == 1 and close < up_212 ? -1 : trend_2
up_2Plot = plot(trend_2 == 1 ? up_2 : na, title="Up_2 Trend_2", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal_2 = trend_2 == 1 and trend_2[1] == -1
plotshape(buySignal_2 ? up_2 : na, title="Up_2Trend_2 Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal_2 and showsignals_2 ? up_2 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dn_2Plot = plot(trend_2 == 1 ? na : dn_2, title="Down Trend_2", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal_2 = trend_2 == -1 and trend_2[1] == 1
plotshape(sellSignal_2 ? dn_2 : na, title="DownTrend_2 Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal_2 and showsignals_2 ? dn_2 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot_2 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
fill(mPlot_2, up_2Plot, title="Up_2Trend_2 Highligter", color=longFillColor)
fill(mPlot_2, dn_2Plot, title="DownTrend_2 Highligter", color=shortFillColor)
///alertcondition(buySignal_2, title="Supertrend Buy", message="Supertrend Buy!")
///alertcondition(sellSignal_2, title="Supertrend Sell", message="Supertrend Sell!")
changeCond_2 = trend_2 != trend_2[1]
///alertcondition(changeCond_2, title="Supertrend Direction Change", message="Supertrend has changed direction!")

////
///plotshape(buySignal and showsignals_3 ? up : na and buySignal_1 and showsignals_3 ? up_1 : na and buySignal_2 and showsignals_3 ? up_2 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
///plotshape(sellSignal and showsignals_3 ? dn : na and sellSignal_1 and showsignals_3 ? dn_1 : na and sellSignal_2 and showsignals_3 ? dn_2 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

isLongOpen = false
isLongOpen := nz(isLongOpen[1]) 

longOpenSignal = trend == 1 and trend_1 == 1 and trend_2 == 1 and not isLongOpen


if (longOpenSignal)
    isLongOpen := true

plotshape(series=longOpenSignal, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

alertcondition(buySignal and buySignal_1 and buySignal_2, title="Supertrend Buy", message="Supertrend Buy!")
alertcondition(sellSignal and sellSignal_1 and sellSignal_2, title="Supertrend Sell", message="Supertrend Sell!")
////


//////


len = input(200, minval=1, title="Length", group="EMA")
src_ema = input(close, title="Source", group="EMA")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")
out = ema(src_ema, len)
plot(out, title="EMA", color=color.blue, offset=offset)


/////
我还想只显示一次信号,而不是在每支蜡烛上


现有代码将信号添加到所有蜡烛上,并且不符合设置的条件。

每次线条对齐时,它将仅在开始处绘制箭头

cond_long = trend == 1 and trend_1 == 1 and trend_2 == 1
cond_short = trend == -1 and trend_1 == -1 and trend_2 == -1

plotshape(cond_long and not cond_long[1], location = location.bottom, style=shape.triangleup, color = color.green, size = size.small)
plotshape(cond_short and not cond_short[1], location = location.top, style=shape.triangledown, color = color.red, size = size.small)

从这里,您可以随心所欲地玩PlotShape。

效果非常好!非常感谢。@Sagar1996太好了,别忘了对答案进行投票并将其标记为已接受
cond_long = trend == 1 and trend_1 == 1 and trend_2 == 1
cond_short = trend == -1 and trend_1 == -1 and trend_2 == -1

plotshape(cond_long and not cond_long[1], location = location.bottom, style=shape.triangleup, color = color.green, size = size.small)
plotshape(cond_short and not cond_short[1], location = location.top, style=shape.triangledown, color = color.red, size = size.small)