In these code i want to check the last 3 candles if not na in bgcolor, like in that 3 candles if there are only 1 or 2 is not na then it will return true, it will only return false if the 3 candles is na. "//@version=5
// Copyright (c) 2019-present, Alex Orekhov (everget)
// Parabolic SAR script may be freely distributed under the terms of the GPL-3.0 license.

indicator("Parabolic SAR", shorttitle="PSAR", overlay=true)

start = input.float(title="Start", step=0.02, defval=0.02)
increment = input.float(title="Increment", step=0.009, defval=0.009)
maximum = input.float(title="Maximum", step=0.9, defval=0.9)
width = input.int(title="Point Width", minval=1, defval=1)
highlightStartPoints = input.bool(title="Highlight Start Points ?", defval=true)
showLabels = input.bool(title="Show Buy/Sell Labels ?", defval=true)
highlightState = input.bool(title="Highlight State ?", defval=true)



length = input.int(610, title='MA Length', minval = 2, tooltip = 'Moving Average Length\n\n- Smaller values will respond more quickly and activate more frequently. (Best for short-term analysis)\n- Larger values will have a slower response, will be less sensitive to small volume variations, and will highlight large volumes. (Best for long-term analysis)\n\nFormula: How many stdev is the volume far from the Moving Average?')
slength = input.int(610, title='Standard Deviation Length', minval=2, tooltip = 'Standard Deviation lookback period.\n\nFormula: How many stdev is the volume far from the Moving Average?')


g_hmz = 'Heatmap Threshold Multipliers'
thresholdExtraHigh = input.float(4, title='Extra High Multiplier', tooltip = 'Volume Threshold Multiplier\neg: 4 = 400% or 4 x Stdev above the mean', group=g_hmz)
thresholdHigh = input(2.5, title='High Multiplier', group=g_hmz)
thresholdMedium = input.float(1, title='Medium Multiplier', group=g_hmz)
thresholdNormal = input(-0.5, title='Normal Multiplier', group=g_hmz)



//------------------------------------------------------------------------------
// Colors


// config
g_color = 'Color settings'
bcolor_enabled = input(true, 'Colored bars', group = g_color)
zmode = input.string('Backgrounds', 'Display Heatmap Zones as', options=['None', 'Lines', 'Backgrounds', 'Both'], group = g_color)
cmode = input.string('Heatmap', 'Color Mode', options=['Heatmap', 'Up/Down'], group = g_color, tooltip = '- Heatmap mode: colors based only on volume.\n- Up/Down mode: colors based on candle volume and direction.\nChange the cores below according to the selected mode.')


// heatmap colors
chm1 = #ff0000  // extra high red
chm2 = #ff7800  // high orange
chm3 = #ffcf03  // medium yellow
chm4 = #a0d6dc  // normal
chm5 = #1f9cac  // low

// heatmap colors
g_mhcolor = 'Heatmap colors'
chmthresholdExtraHigh = input(chm1, 'Extra High', group=g_mhcolor)
chmthresholdHigh = input(chm2, 'High', group=g_mhcolor)
chmthresholdMedium = input(chm3, 'Medium', group=g_mhcolor)
chmthresholdNormal = input(chm4, 'Normal', group=g_mhcolor)
chmthresholdLow = input(chm5, 'Low', group=g_mhcolor)





// up colors
g_up = 'Up colors'
cupthresholdExtraHigh = input(#00FF00, 'Extra High', group = g_up)
cupthresholdHigh = input(#30FF30, 'High', group = g_up)
cupthresholdMedium = input(#60FF60, 'Medium', group = g_up)
cupthresholdNormal = input(#8FFF8F, 'Normal', group = g_up)
cupthresholdLow = input(#BFFFBF, 'Low', group = g_up)


// down colors
g_down = 'Down colors'
cdnthresholdExtraHigh = input(#FF0000, 'Extra High', group = g_down)
cdnthresholdHigh = input(#FF3030, 'High', group = g_down)
cdnthresholdMedium = input(#FF6060, 'Medium', group = g_down)
cdnthresholdNormal = input(#FF8F8F, 'Normal', group = g_down)
cdnthresholdLow = input(#FFBFBF, 'Low', group = g_down)


// threshold colors
cthresholdExtraHighUp = cmode == 'Heatmap' ? chmthresholdExtraHigh : cupthresholdExtraHigh
cthresholdHighUp = cmode == 'Heatmap' ? chmthresholdHigh : cupthresholdHigh
cthresholdMediumUp = cmode == 'Heatmap' ? chmthresholdMedium : cupthresholdMedium

cthresholdExtraHighDn = cmode == 'Heatmap' ? chmthresholdExtraHigh : cdnthresholdExtraHigh
cthresholdHighDn = cmode == 'Heatmap' ? chmthresholdHigh : cdnthresholdHigh
cthresholdMediumDn = cmode == 'Heatmap' ? chmthresholdMedium : cdnthresholdMedium



//------------------------------------------------------------------------------
// Calcs


length := length > bar_index + 1 ? bar_index + 1 : length
slength := slength > bar_index + 1 ? bar_index + 1 : slength

dir = close > open
mean = ta.sma(volume, length)
std = ta.stdev(volume, slength)

// how many stdev is the volume far from the mean?
// eg: stdbar = 2.5, is 2.5 * stdev above the mean (2.5 * stdev + mean)
stdbar = (volume - mean) / std 

bolor = stdbar > thresholdExtraHigh ? dir ? cthresholdExtraHighUp : cthresholdExtraHighDn : stdbar > thresholdHigh ? dir ? cthresholdHighUp : cthresholdHighDn : stdbar > thresholdMedium ? dir ? cthresholdMediumUp : cthresholdMediumDn : na
bgcolor = not na(bolor)
barcolor(bcolor_enabled ? bolor : na, editable=false)

psar = ta.sar(start, increment, maximum)
dirs = psar < close ? 1 : -1

psarColor = dirs == 1 ? color.new(#3388bb, 0) : #e730ffd4
psarPlot = plot(psar, title="PSAR", style=plot.style_circles, linewidth=width, color=psarColor)

var color longColor = color.rgb(83, 200, 200, 93)
var color shortColor = color.rgb(238, 53, 161, 94)

buySignal = dirs == 1 and dirs[1] == -1
plotshape(buySignal and highlightStartPoints ? psar : na, title="Long Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor)
plotshape(buySignal and bgcolor and showLabels ? psar: na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.rgb(0, 169, 169), textcolor=color.white)

sellSignal = dirs == -1 and dirs[1] == 1
plotshape(sellSignal and highlightStartPoints ? psar : na, title="Short Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor)
plotshape(sellSignal and  bgcolor and showLabels ? psar: na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.rgb(195, 28, 109), textcolor=color.white)

midPricePlot = plot(request.security(syminfo.tickerid, timeframe.period, (open + high + low + close) / 4), title="", display=display.none)

fillColor = highlightState ? (dirs == 1 ? longColor : shortColor) : na
fill(midPricePlot, psarPlot, color=fillColor)

"