script("Adaptive Flow Oscillator", kind="indicator") input volatilityLength = 14 input volumeLength = 30 input pressureLength = 5 input normalizationLength = 50 input biasLength = 21 input pressureScale = 40 input upperZone = 70 input lowerZone = -70 input displayCeiling = 100 input pivotStrength = 3 input divergenceMinBars = 5 input divergenceMaxBars = 60 input divergenceFilter = 20 input showPivots = 1 input showDivergences = 1 input showBackground = 1 input useVolume = 1 input bullColor = "#76F7B8" input bearColor = "#FF4422" input panelHeight = 28 atrPeriod = math.clamp(math.floor(volatilityLength), 1, 500) volumePeriod = math.clamp(math.floor(volumeLength), 1, 500) pressurePeriod = math.clamp(math.floor(pressureLength), 1, 200) normalPeriod = math.clamp(math.floor(normalizationLength), 2, 500) biasPeriod = math.clamp(math.floor(biasLength), 1, 500) strength = math.clamp(math.floor(pivotStrength), 1, 50) upper = math.clamp(upperZone, 10, 95) lower = math.clamp(lowerZone, -95, -10) ceiling = math.clamp(displayCeiling, 96, 110) volatility = ta.atr(atrPeriod) volumeMissing = na(volume) or volume <= 0 validVolume = volumeMissing ? 0 : volume volumeBaseline = ta.sma(validVolume, volumePeriod) volumeActive = useVolume and not volumeMissing and not na(volumeBaseline) and volumeBaseline > 0 relativeVolume = volumeActive ? volume / volumeBaseline : 1 effort = math.sqrt(math.clamp(relativeVolume, 0.0625, 16)) displacement = barIndex > 0 and volatility > 0 ? (close - close[1]) / volatility : 0 rawPressure = displacement * effort smoothedPressure = ta.ema(rawPressure, pressurePeriod) pressureDeviation = ta.stdev(smoothedPressure, normalPeriod) pressure = not na(pressureDeviation) ? (pressureDeviation > 0.000000000001 ? smoothedPressure / pressureDeviation * pressureScale : 0) : na bias = ta.ema(pressure, biasPeriod) upperRoom = ceiling - upper lowerRoom = ceiling + lower upperExcess = math.max(0, pressure - upper) lowerExcess = math.max(0, lower - pressure) display = pressure > upper ? upper + upperRoom * upperExcess / (upperRoom + upperExcess) : pressure < lower ? lower - lowerRoom * lowerExcess / (lowerRoom + lowerExcess) : pressure biasUpperExcess = math.max(0, bias - upper) biasLowerExcess = math.max(0, lower - bias) displayBias = bias > upper ? upper + upperRoom * biasUpperExcess / (upperRoom + biasUpperExcess) : bias < lower ? lower - lowerRoom * biasLowerExcess / (lowerRoom + biasLowerExcess) : bias rising = pressure > pressure[1] biasPositive = bias >= 0 zero = 0 zoneUpper = upper zoneLower = lower outerUpper = ceiling outerLower = -ceiling extremeUp = showBackground and pressure >= upper extremeDown = showBackground and pressure <= lower volumeFallback = useVolume and volumeMissing pane(title="Flow Pressure ยท independent", height=panelHeight, min=-120, max=120, note="Price-only mode: no volume", noteWhen=volumeFallback) fill(outerUpper, zoneUpper, color=bearColor, opacity=0.24, gradient=1) fill(zoneLower, outerLower, color=bullColor, opacity=0.24, gradient=1) bgcolor(extremeUp, color=bearColor, opacity=0.08) bgcolor(extremeDown, color=bullColor, opacity=0.08) plot(displayBias, title="Slow bias", style="histogram", color=bullColor, opacity=0.3) color(displayBias, biasPositive, up=bullColor, down=bearColor) plot(display, title="Flow pressure", color=bullColor, width=3) color(display, rising, up=bullColor, down=bearColor) hline(0, color="#999999", opacity=0.5, style="solid") slope = pressure - pressure[1] pivotUp = isClosed and not na(slope[1]) and slope > 0 and slope[1] <= 0 pivotDown = isClosed and not na(slope[1]) and slope < 0 and slope[1] >= 0 plotShape(display, when=showPivots and pivotUp, shape="circle", color=bullColor, position="center", size=4) plotShape(display, when=showPivots and pivotDown, shape="circle", color=bearColor, position="center", size=4) lowPivot = ta.pivotlow(pressure, strength, strength) highPivot = ta.pivothigh(pressure, strength, strength) lastLowPressure = barIndex > 0 ? lastLowPressure[1] : na lastLowPrice = barIndex > 0 ? lastLowPrice[1] : na lastLowBar = barIndex > 0 ? lastLowBar[1] : 0 lastHighPressure = barIndex > 0 ? lastHighPressure[1] : na lastHighPrice = barIndex > 0 ? lastHighPrice[1] : na lastHighBar = barIndex > 0 ? lastHighBar[1] : 0 bullRegular = 0 bullHidden = 0 bearRegular = 0 bearHidden = 0 if isClosed and not na(lowPivot) { lowBar = barIndex - strength lowPrice = low[strength] lowGap = lowBar - lastLowBar lowEligible = not na(lastLowPressure) and lowGap >= math.max(1, divergenceMinBars) and lowGap <= math.max(divergenceMinBars, divergenceMaxBars) and math.abs(lowPivot) >= divergenceFilter bullRegular = lowEligible and lowPrice < lastLowPrice and lowPivot > lastLowPressure bullHidden = lowEligible and lowPrice > lastLowPrice and lowPivot < lastLowPressure if showDivergences and (bullRegular or bullHidden) { draw.label("bull" + barIndex, lowBar, display[strength], bullRegular ? "R" : "H", color="#142820", background=bullColor, badge=1, position="below", fontSize=11) } lastLowPressure = lowPivot lastLowPrice = lowPrice lastLowBar = lowBar } if isClosed and not na(highPivot) { highBar = barIndex - strength highPrice = high[strength] highGap = highBar - lastHighBar highEligible = not na(lastHighPressure) and highGap >= math.max(1, divergenceMinBars) and highGap <= math.max(divergenceMinBars, divergenceMaxBars) and math.abs(highPivot) >= divergenceFilter bearRegular = highEligible and highPrice > lastHighPrice and highPivot < lastHighPressure bearHidden = highEligible and highPrice < lastHighPrice and highPivot > lastHighPressure if showDivergences and (bearRegular or bearHidden) { draw.label("bear" + barIndex, highBar, display[strength], bearRegular ? "R" : "H", color="#FFFFFF", background=bearColor, badge=1, position="above", fontSize=11) } lastHighPressure = highPivot lastHighPrice = highPrice lastHighBar = highBar } upperLevel = upper lowerLevel = lower crossUpper = ta.crossover(pressure, upperLevel) crossLower = ta.crossunder(pressure, lowerLevel) crossZeroUp = ta.crossover(pressure, zero) crossZeroDown = ta.crossunder(pressure, zero) crossBiasUp = ta.crossover(pressure, bias) crossBiasDown = ta.crossunder(pressure, bias) alertcondition(crossUpper, title="Upper pressure breach") alertcondition(crossLower, title="Lower pressure breach") alertcondition(crossZeroUp, title="Pressure crossed above zero") alertcondition(crossZeroDown, title="Pressure crossed below zero") alertcondition(crossBiasUp, title="Pressure crossed above bias") alertcondition(crossBiasDown, title="Pressure crossed below bias") alertcondition(pivotUp, title="Momentum turned up") alertcondition(pivotDown, title="Momentum turned down") alertcondition(bullRegular, title="Regular bullish divergence") alertcondition(bullHidden, title="Hidden bullish divergence") alertcondition(bearRegular, title="Regular bearish divergence") alertcondition(bearHidden, title="Hidden bearish divergence")