Search code examples
pine-scriptlimittradingcandlestick-chartpine-script-v4

How to ignore my white soldiers signals whenever a previous candle produces a signal?


Trying to bound or limit my 3 white soldiers candle indicator so it doesn't paint these signals back to back ever.

my chart

You'll see on July 5th my code painted 3 days of this signal in a row. I would like to ignore painting the signal after a signal already is painted on the previous candle. In other words, never paint the signal twice in a row.

3 White Soldiers Configuration: Look for three tall white candles (mine are black on the chart), each with a close near the high, higher closes, and bodies that overlap (an opening price within the prior candle's body) (this is hard to see on the chart).

link to above quote

//@version=4

study("My Three White Soldiers Indicator (3 candle figure) by jsmith #20", overlay = true)
// TODO 
// stop painting signals back to back

upper_shadow1 = abs(close[2] - high[2])
lower_shadow1 = abs(open[2] - low[2])

upper_shadow2 = abs(close[1] - high[1])
lower_shadow2 = abs(open[1] - low[1])

upper_shadow3 = abs(close - high)
lower_shadow3 = abs(open - low)


body1 = abs(open[2] - close[2])
body2 = abs(open[1] - close[1])
body3 = abs(open - close)

openBar1 = open[2]
closeBar1 = close[2]

openBar2 = open[1]
closeBar2 = close[1]
 
openBar3 = open
closeBar3 = close

my_color = color.new(color.white, 0)

// Candle logic
soldiersCond1 = openBar3 < closeBar3
soldiersCond2 = (openBar2 >= openBar1) and (openBar2 <= closeBar1)
soldiersCond3 = (openBar3 >= openBar2) and (openBar3 <= closeBar2)

// No long upper shadows
soldiersCond4 = (upper_shadow1 < body1 * 0.33)
soldiersCond5 = (upper_shadow2 < body2 * 0.33)
soldiersCond6 = (upper_shadow3 < body3 * 0.33)

soldiers = soldiersCond1 and soldiersCond2 and soldiersCond3 and soldiersCond4 and soldiersCond5 and soldiersCond6

//////////////////////////////////////////////////////

plotchar(soldiers, char = "♞", location = location.belowbar, color = my_color, size = size.tiny, transp = 0)

// do bg color paint
isThreesoldiers = soldiers ? color.green : na
bgcolor(color=isThreesoldiers, transp=80)

Solution

  • You can add a check on the soldiers signal where you can check if the previous candle value of soldier was false. Example below

    //@version=4
    
    study("My Three White Soldiers Indicator (3 candle figure) by jsmith #20", overlay = true)
    // TODO 
    // stop painting signals back to back
    
    upper_shadow1 = abs(close[2] - high[2])
    lower_shadow1 = abs(open[2] - low[2])
    
    upper_shadow2 = abs(close[1] - high[1])
    lower_shadow2 = abs(open[1] - low[1])
    
    upper_shadow3 = abs(close - high)
    lower_shadow3 = abs(open - low)
    
    
    body1 = abs(open[2] - close[2])
    body2 = abs(open[1] - close[1])
    body3 = abs(open - close)
    
    openBar1 = open[2]
    closeBar1 = close[2]
    
    openBar2 = open[1]
    closeBar2 = close[1]
     
    openBar3 = open
    closeBar3 = close
    
    my_color = color.new(color.white, 0)
    
    // Candle logic
    soldiersCond1 = openBar3 < closeBar3
    soldiersCond2 = (openBar2 >= openBar1) and (openBar2 <= closeBar1)
    soldiersCond3 = (openBar3 >= openBar2) and (openBar3 <= closeBar2)
    
    // No long upper shadows
    soldiersCond4 = (upper_shadow1 < body1 * 0.33)
    soldiersCond5 = (upper_shadow2 < body2 * 0.33)
    soldiersCond6 = (upper_shadow3 < body3 * 0.33)
    
    var soldiers=false
    soldiers := soldiersCond1 and soldiersCond2 and soldiersCond3 and soldiersCond4 and soldiersCond5 and soldiersCond6 and not soldiers[1]