The below code is trying to print divergence labels based on price and a volume flow indicator. Currently no labels are plotting and I am unsure why. The logic of this looks correct. I would like to print a label when the close is greater than the highest price of the lookback period and when the current VFI reading is less than what the VFI reading was when price was highest.
calc_adjusted_vfi(fviPeriod, smoothLen, coef, vCoef, z2) =>
avg = request.security(syminfo.tickerid, tf_3h, hlc3) // Use 3-hour hlc3
avg := avg < 0.0001 ? 0.0001 : avg // Prevent log issues
inter = math.log(avg) - math.log(avg[1])
inter := inter < -10 or inter > 10 ? 0 : inter // Clamp extreme values
vInter = ta.stdev(inter, 30)
vInter := vInter < 0.0001 ? 0.0001 : vInter // Prevent small values
cutOff = coef * vInter * request.security(syminfo.tickerid, tf_3h, close) // Use 3-hour close
cutOff := cutOff < 0.0001 ? 0.0001 : cutOff // Ensure cutoff is significant
vAve = ta.sma(request.security(syminfo.tickerid, tf_3h, volume[1]), fviPeriod) // Use 3-hour volume
vAve := vAve < 1 ? 1 : vAve // Prevent division by zero
vMax = vAve * vCoef
vMax := vMax > 1e6 ? 1e6 : vMax // Prevent large volume cap
vC = math.min(request.security(syminfo.tickerid, tf_3h, volume), vMax)
vC := vC > 1e6 ? 1e6 : vC // Clamp volume component
mf = avg - avg[1]
mf := math.abs(mf) < 0.0001 ? 0 : mf // Ignore tiny money flow
vCp = mf > cutOff ? vC : mf < -cutOff ? -vC : 0
vCp := math.abs(vCp) > 1e6 ? 0 : vCp // Prevent extreme vCp
// Calculate sum of vCp over fviPeriod
sVfi = 0.0
for i = 0 to fviPeriod - 1
sVfi := sVfi + nz(vCp[i])
sVfi := sVfi / vAve
sVfi := math.abs(sVfi) > 1e6 ? 0 : sVfi // Prevent overflow
// Apply ALMA smoothing based on smoothLen
vfi = alma_calc(sVfi, smoothLen, alma_offset, alma_sigma)
// Adjust VFI based on VQI Z-Score
adjusted_vfi = vfi * (1 + z2 / 10)
adjusted_vfi
// Calculate the adjusted VFI
value_vfi = calc_adjusted_vfi(vfi_length, vfi_smoothLen, vfi_coef, vfi_volCutoff, z2)
// Calculate the highest high and the index of the highest high
phighestHigh = ta.highest(high, price_lookback)
var int highestHighIndex = na
// Call ta.highestbars consistently
int tempIndex = ta.highestbars(high, price_lookback)
highestHighIndex := na(tempIndex) or tempIndex < 0 ? na : tempIndex
// Ensure highestHighIndex is within valid bounds
vfiAtHighestHigh = na(highestHighIndex) or highestHighIndex < 0 ? na : value_vfi[highestHighIndex]
// Function to check for bearish divergence
isBearishDivergence(priceCurrent, priceHighest, vfiCurrent, vfiAtHighest) =>
priceCurrent > priceHighest and vfiCurrent < vfiAtHighest
// Check for bearish divergence with current price and VFI
bearish_div = isBearishDivergence(close, phighestHigh, value_vfi, vfiAtHighestHigh)
// Plot divergence labels as "X"
plotshape(series = bearish_div, location=location.abovebar, color=color.teal, style=shape.labeldown, size=size.large, title="Bearish Divergence", text="X")
I was initially getting a negative bar reference which is why i added some of the na and < statements.
I have also tried using the history reference operator [] in different ways too but with no luck