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.
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?')
// 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
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)
Sure! To check the last three candles for a non-na background color (bgcolor) and return true when 1 or 2 of the last 3 candles are non-na or false only if all the last 3 candles are na, you can use a simple approach combining bgcolor with a logic check.
Here's how you can do it:
Define an array of the last 3 bgcolor values.
Count the number of non-na values in this array.
Return true if the count is 1 or 2, otherwise return false.
Below the initial code segment where you set the bgcolor, you can add the logic to achieve this. I'll show you how to modify your script:
pinescript
//@version=5
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)
# ... [rest of your initial setup and code]
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)
// Color settings
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
// Group 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)
// Group 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)
// Group 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)
// Additional logic to check the last 3 bars' bgcolor
bg_colors = array.from(bgcolor, bgcolor[1], bgcolor[2])
non_na_count = array.reduce(bg_colors, 0, (accum, elem) => accum + (elem ? 1 : 0))
last_3_candles_check = non_na_count < 3
alertcondition(last_3_candles_check, title="Match Last 3 Candles", message="Match condition met for last 3 candles")
Explanation:
Array creation: We create an array bg_colors which holds the bgcolor value of the last 3 candles.
Count non-na values: Using array.reduce, we count how many of the last 3 bgcolors are non-na. This count is stored in non_na_count.
Check condition: We check if non_na_count is less than 3. This evaluates to true if there are 1 or 2 non-na colors, and false if all 3 are na.
Alert condition: An alertcondition is added to signal when this condition is met.
This approach ensures that you get alerts when 1 or 2 out of the last 3 candles have a non-na background color.