Search code examples
plotviewsignalspine-scripttrading

PineScript = How to not display the 2nd-nth occurrence of plotshape of buy/sell signal until the sell/buy signal occur?


Only those signals that are encircled with yellow is expected

enter image description here

study(title="ALMA", overlay=true)

alma = alma(close, 9, 0.85, 6)

plot(alma, title="ALMA", color=color.yellow, linewidth=2)

ema50 = ema(close,50)

plot(ema50, title="EMA", color=color.fuchsia, linewidth=2)

A = close > alma

B = close < alma

var S_sell = false

var S_buy = false

if (A)

  S_sell := true

  S_buy := false
  
if (B)

  S_Sell := false

  S_buy := true

plotshape(A,text="Buy",style=shape.flag,size=size.small,offset=0,color=color.green,textcolor=color.green,location=location.belowbar)

plotshape(B,text="Sell",style=shape.flag,size=size.small,offset=0,color=color.red,textcolor=color.red,location=location.abovebar)

Solution

  • You can use barssince() function set to 1.

    //@version=4
    study(title="ALMA", overlay=true)
    
    alma = alma(close, 9, 0.85, 6)
    ema50 = ema(close,50)
    
    plot(alma, title="ALMA", color=color.yellow, linewidth=2)
    plot(ema50, title="EMA", color=color.fuchsia, linewidth=2)
    
    A = close > alma
    B = close < alma
    
    plotshape(barssince(A)==1,text="Buy",style=shape.flag,size=size.small,offset=0,color=color.green,textcolor=color.green,location=location.belowbar)
    plotshape(barssince(B)==1,text="Sell",style=shape.flag,size=size.small,offset=0,color=color.red,textcolor=color.red,location=location.abovebar)