pine-scriptstochastictechnical-indicator

How to remove consecutive candle highlight in the following indicator?


I have created an indicator in pinescript as following which will highlight a candle that is closed above 9EMA and stok %k above 80.

//@version=5
indicator("ASK - Algo Short", overlay=true)

// Define inputs
length = input(9, "EMA Length")
stochLength = input(14, "Stochastic Length")
stochSmoothK = input(3, "Stochastic %K Smoothing")

// Calculate EMA
ema = ta.ema(close, length)

// Calculate Stochastic %K of previous candle
stoK = ta.stoch(close[1], high[1], low[1], stochLength)
stoKSmoothed = ta.sma(stoK, stochSmoothK)

// Check for break of previous candle low above 9 EMA
isBreakOfLow = (low < ta.highest(low[1], 1)) and (low[1] > ema)
isStochAbove80 = stoKSmoothed > 80

// Check for red candle low above 9 EMA
isPreviousCandleRed = close[1] < open[1]

// Check if the previous candle was highlighted
prevCandleHighlighted = ta.valuewhen(isBreakOfLow and isStochAbove80, isBreakOfLow and isStochAbove80, 1)

// Plot a highlight if condition is met
highlightedCandle = isBreakOfLow and isStochAbove80 //and isPreviousCandleRed
plotshape(highlightedCandle, title="Break of Previous Candle Low Above 9 EMA", location=location.abovebar, color=color.rgb(6, 0, 6), style=shape.arrowdown, size=size.tiny, textcolor=color.white, transp=0)


// Plot the EMA
plot(ema, color=color.blue, title="9 EMA")

The output is as following enter image description here

When I draw a down arrow, code should check if previous candle is highlighted with down arrow. if yes then this candle should not draw a down arrow.

Can anyone help me please?

I added following code but entire highlight was gone. no candle got highlighted.

// Check if the previous candle was highlighted prevCandleHighlighted = ta.valuewhen(isBreakOfLow and isStochAbove80, isBreakOfLow and isStochAbove80, 1)


Solution

  • I updated your plotshape to include not highlightedCandle[1]. This prevents the signal from occurring on back to back bars.

    plotshape(highlightedCandle and not highlightedCandle[1], title="Break of Previous Candle Low Above 9 EMA", location=location.abovebar, color=color.rgb(6, 0, 6), style=shape.arrowdown, size=size.tiny, textcolor=color.white)