本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Copyright (c) 2026 SBP Trading Ecosystem.
indicator("SBP Value Momentum Navigator", overlay=true, max_lines_count=300, max_labels_count=50, max_boxes_count=100, max_bars_back=1500, calc_bars_count=1500)
bool showSignals = input.bool(true, "Buy / Sale Labels", tooltip="Show or hide Buy and Sale labels. Signal calculations continue when hidden.", group="01. Signals")
color buyLabelColour = input.color(color.green, "Buy Colour", inline="SignalColours", group="01. Signals")
color saleLabelColour = input.color(color.red, "Sale Colour", inline="SignalColours", group="01. Signals")
color signalTextColour = input.color(color.white, "Label Text Colour", group="01. Signals")
int signalRsiLength = input.int(14, "RSI Length", minval=2, maxval=100, group="01. Signals")
float signalBuyRsi = input.float(60.0, "Buy RSI Minimum", minval=0.0, maxval=100.0, step=1.0, group="01. Signals")
float signalSaleRsi = input.float(45.0, "Sale RSI Maximum", minval=0.0, maxval=100.0, step=1.0, group="01. Signals")
bool showSignalVwap = input.bool(true, "VWAP", inline="VWAPVisible", group="02. VWAP and SD Lines")
bool showSignalUpper = input.bool(true, "+1 SD", inline="VWAPVisible", group="02. VWAP and SD Lines")
bool showSignalLower = input.bool(true, "-1 SD", inline="VWAPVisible", group="02. VWAP and SD Lines")
color signalVwapColour = input.color(color.orange, "VWAP Colour", group="02. VWAP and SD Lines")
color signalUpperColour = input.color(color.blue, "Upper SD Colour", group="02. VWAP and SD Lines")
color signalLowerColour = input.color(color.blue, "Lower SD Colour", group="02. VWAP and SD Lines")
int signalBandWidth = input.int(1, "VWAP Line Width", minval=1, maxval=5, group="02. VWAP and SD Lines")
bool showDirectionalStrength = input.bool(true, "Bull / Bear Clouds", tooltip="Display only. Hiding this element does not disable its calculation or change signal rules.", group="03. Bull / Bear Clouds")
color bullCloudFillColour = input.color(color.new(color.blue, 45), "Bull Fill", inline="CloudFills", group="03. Bull / Bear Clouds")
color bearCloudFillColour = input.color(color.new(color.red, 45), "Bear Fill", inline="CloudFills", group="03. Bull / Bear Clouds")
color bullCloudBorderColour = input.color(color.rgb(45, 120, 220), "Bull Border", inline="BullBorderStyle", group="03. Bull / Bear Clouds")
int bullCloudWidth = input.int(1, "Width", minval=1, maxval=5, inline="BullBorderStyle", group="03. Bull / Bear Clouds")
color bearCloudBorderColour = input.color(color.rgb(210, 70, 80), "Bear Border", inline="BearBorderStyle", group="03. Bull / Bear Clouds")
int bearCloudWidth = input.int(1, "Width", minval=1, maxval=5, inline="BearBorderStyle", group="03. Bull / Bear Clouds")
bool showStructureLines = input.bool(false, "Structure Lines — VAH / VAL / POC / Outer", group="04. Structure Lines")
bool showCoreUpper = input.bool(false, "Upper Structure (VAH)", group="04. Structure Lines")
color vahColour = input.color(color.red, "VAH Colour", inline="VAHStyle", group="04. Structure Lines")
int vahWidth = input.int(1, "Width", minval=1, maxval=5, inline="VAHStyle", group="04. Structure Lines")
bool showCoreLower = input.bool(false, "Lower Structure (VAL)", group="04. Structure Lines")
color valColour = input.color(color.lime, "VAL Colour", inline="VALStyle", group="04. Structure Lines")
int valWidth = input.int(1, "Width", minval=1, maxval=5, inline="VALStyle", group="04. Structure Lines")
bool showControl = input.bool(false, "Volume Control (POC)", group="04. Structure Lines")
color pocColour = input.color(color.black, "POC Colour", inline="POCStyle", group="04. Structure Lines")
int pocWidth = input.int(1, "Width", minval=1, maxval=5, inline="POCStyle", group="04. Structure Lines")
bool showOuter = input.bool(false, "Outer Structure", group="04. Structure Lines")
color outerColour = input.color(color.blue, "Outer Colour", inline="OuterStyle", group="04. Structure Lines")
int outerWidth = input.int(1, "Width", minval=1, maxval=5, inline="OuterStyle", group="04. Structure Lines")
bool showDevelopingControl = input.bool(false, "Developing POC Trail", tooltip="Show horizontal confirmed POC segments for the current session. Display only; retains at most 250 segments.", group="04. Structure Lines")
color developingPocColour = input.color(color.rgb(191, 0, 255), "Trail Colour", inline="TrailStyle", group="04. Structure Lines")
int developingPocWidth = input.int(1, "Width", minval=1, maxval=5, inline="TrailStyle", group="04. Structure Lines")
bool showProfileHistogram = input.bool(false, "Session Volume Profile Histogram", group="05. Session Histogram")
int histogramWidth = input.int(25, "Maximum Width (Chart Bars)", minval=1, maxval=100, group="05. Session Histogram")
int histogramTransparency = input.int(65, "Transparency", minval=0, maxval=95, group="05. Session Histogram")
string volumeSampling = input.string("Auto", "Profile Volume Sampling", options=["Auto", "Chart", "1S", "1", "5", "15", "30", "60"], tooltip="Controls the estimated daily profile only. Auto selects sampling by timeframe and instrument type; Chart uses chart candles. Sampling changes profile levels and histogram, not Buy / Sale conditions.", group="06. Profile Calculations")
int profileRows = input.int(24, "Volume Profile Rows", minval=10, maxval=100, group="06. Profile Calculations")
float profileCoverage = input.float(70.0, "Value Area Coverage (%)", minval=50.0, maxval=95.0, step=5.0, group="06. Profile Calculations")
f_limit(float value, float lowValue, float highValue) =>
math.max(lowValue, math.min(highValue, value))
f_rowStep(float rangeLow, float rangeHigh, int targetRows) =>
float ticks = math.max(math.round((rangeHigh - rangeLow) / syminfo.mintick), 1.0)
int floorTicks = math.max(int(math.floor(ticks / targetRows)), 1)
int ceilingTicks = math.max(int(math.ceil(ticks / targetRows)), 1)
((math.abs(math.ceil(ticks / floorTicks) - targetRows)) < (math.abs(math.ceil(ticks / ceilingTicks) - targetRows)) ? floorTicks : ceilingTicks) * syminfo.mintick
f_allocate(array<float> bins, float base, float step, float lo, float hi, float vol) =>
int rows = array.size(bins)
if vol > 0.0 and rows > 0 and step > 0.0 and not na(lo) and not na(hi)
int firstRow = int(f_limit(math.floor((lo - base) / step), 0, rows - 1))
int lastRow = int(f_limit(math.floor((hi - base) / step), 0, rows - 1))
if hi - lo <= 0.0
array.set(bins, firstRow, array.get(bins, firstRow) + vol)
else
for row = firstRow to lastRow
float rowBase = base + row * step
float overlap = math.max(math.min(hi, rowBase + step) - math.max(lo, rowBase), 0.0)
if overlap > 0.0
array.set(bins, row, array.get(bins, row) + vol * overlap / (hi - lo))
f_bounds(array<float> bins, int peak, float total, float fraction, bool includeCrossing) =>
int lower = peak
int upper = peak
int rows = array.size(bins)
float accumulated = array.get(bins, peak)
while accumulated < (total * fraction)
if not (lower > 0) and not (upper < rows - 1)
break
float lowerVolume = (lower > 0) ? array.get(bins, lower - 1) : -1.0
float upperVolume = (upper < rows - 1) ? array.get(bins, upper + 1) : -1.0
bool preferUpper = ((upper < rows - 1) and (not (lower > 0) or upperVolume > lowerVolume or (upperVolume == lowerVolume and upper - peak <= peak - lower)))
float nextVolume = preferUpper ? upperVolume : lowerVolume
if not includeCrossing and accumulated + nextVolume > (total * fraction)
break
if preferUpper
upper += 1
else
lower -= 1
accumulated += nextVolume
[lower, upper]
f_sessionProfile(array<float> profileLows, array<float> profileHighs, array<float> profileVolumes, int profileRows, float profileCoverage, simple bool rebuildOnGrowth) =>
var array<float> storedLow = array.new_float()
var array<float> storedHigh = array.new_float()
var array<float> storedVolume = array.new_float()
var array<float> bins = array.new_float()
var float rangeLow = na
var float rangeHigh = na
var float cachedLow = na
var float cachedStep = na
var float cachedHigh = na
var int startTime = na
var float resultLower = na
var float resultUpper = na
var float resultOuterLower = na
var float resultOuterUpper = na
var float resultControl = na
if (barstate.isconfirmed)
if (barstate.isfirst or timeframe.change("D")) or na(startTime)
array.clear(storedLow)
array.clear(storedHigh)
array.clear(storedVolume)
array.clear(bins)
rangeLow := low
rangeHigh := high
cachedLow := na
cachedStep := na
cachedHigh := na
startTime := time
int previousCount = array.size(storedLow)
int sampleCount = math.min(array.size(profileLows), math.min(array.size(profileHighs), array.size(profileVolumes)))
float availableVolume = sampleCount > 0 ? array.sum(profileVolumes) : 0.0
if previousCount + (sampleCount > 0 and nz(availableVolume, 0.0) > 0.0 ? sampleCount : 1) > 95000
runtime.error("Session exceeds the profile sample budget. Select coarser Profile Volume Sampling.")
if sampleCount > 0 and nz(availableVolume, 0.0) > 0.0
for index = 0 to sampleCount - 1
float lo = array.get(profileLows, index)
float hi = array.get(profileHighs, index)
array.push(storedLow, lo)
array.push(storedHigh, hi)
array.push(storedVolume, math.max(nz(array.get(profileVolumes, index), 0.0), 0.0))
rangeLow := math.min(rangeLow, lo)
rangeHigh := math.max(rangeHigh, hi)
else
array.push(storedLow, low)
array.push(storedHigh, high)
array.push(storedVolume, math.max(nz(volume, 0.0), 0.0))
rangeLow := math.min(rangeLow, low)
rangeHigh := math.max(rangeHigh, high)
float step = f_rowStep(rangeLow, rangeHigh, profileRows)
int rows = math.max(int(math.ceil((rangeHigh - rangeLow) / step)), 1)
bool topEdgeNeedsRebuild = rows > array.size(bins) and not na(cachedHigh) and cachedHigh >= rangeLow + array.size(bins) * step
bool rebuild = na(cachedLow) or rangeLow != cachedLow or step != cachedStep or (rebuildOnGrowth ? array.size(bins) != rows : array.size(bins) > rows or topEdgeNeedsRebuild)
if rebuild
bins := array.new_float(rows, 0.0)
else
while array.size(bins) < rows
array.push(bins, 0.0)
if (rebuild ? 0 : previousCount) < array.size(storedLow)
for index = (rebuild ? 0 : previousCount) to array.size(storedLow) - 1
f_allocate(bins, rangeLow, step, array.get(storedLow, index), array.get(storedHigh, index), array.get(storedVolume, index))
cachedLow := rangeLow
cachedStep := step
cachedHigh := rangeHigh
float total = array.sum(bins)
int peak = array.indexof(bins, array.max(bins))
[coreLow, coreHigh] = f_bounds(bins, peak, total, (profileCoverage / 100.0), false)
[outerLow, outerHigh] = if showStructureLines and showOuter
f_bounds(bins, peak, total, 0.94, false)
else
[0, 0]
resultLower := total > 0.0 ? rangeLow + coreLow * step : na
resultUpper := total > 0.0 ? math.min(rangeHigh, rangeLow + (coreHigh + 1) * step) : na
resultOuterLower := showStructureLines and showOuter and total > 0.0 ? rangeLow + outerLow * step : na
resultOuterUpper := showStructureLines and showOuter and total > 0.0 ? math.min(rangeHigh, rangeLow + (outerHigh + 1) * step) : na
resultControl := total > 0.0 ? (rangeLow + peak * step + (math.min(rangeHigh, rangeLow + (peak + 1) * step))) * 0.5 : na
[resultLower, resultUpper, resultOuterLower, resultOuterUpper, resultControl, startTime, bins, rangeLow, cachedStep]
f_ratio(float numerator, float denominator) =>
denominator != 0.0 ? numerator / denominator : 0.0
float sharedAtr14 = ta.atr(14)
float atr = math.max(sharedAtr14, syminfo.mintick)
float sharedCloseEma5 = ta.ema(close, 5)
string autoSamplingFrame = timeframe.in_seconds() <= 60 ? "1S" : (syminfo.type == "futures" or syminfo.type == "spread") ? (timeframe.in_seconds() <= 300 ? "1" : timeframe.in_seconds() <= 900 ? "5" : timeframe.in_seconds() <= 1800 ? "15" : timeframe.in_seconds() <= 3600 ? "30" : timeframe.in_seconds() <= 14400 ? "60" : "240") : "1"
string mainSamplingFrame = volumeSampling == "Auto" ? autoSamplingFrame : volumeSampling == "Chart" ? timeframe.period : volumeSampling
[sampleLows, sampleHighs, sampleVolumes] = request.security_lower_tf(syminfo.tickerid, mainSamplingFrame, [low, high, volume], ignore_invalid_timeframe=true)
f_level(line id, bool visible, int startTime, float price, color lineColor, int lineWidth) =>
line result = id
if visible and not na(price)
if na(result)
result := line.new(startTime, price, time_close, price, xloc=xloc.bar_time, extend=extend.none, color=lineColor, width=lineWidth)
else
line.set_xy1(result, startTime, price)
line.set_xy2(result, time_close, price)
else if not na(result)
line.delete(result)
result := na
result
[coreLower, coreUpper, outerLower, outerUpper, volumeControl, profileStartTime, histogramBins, histogramBase, histogramStep] = f_sessionProfile(sampleLows, sampleHighs, sampleVolumes, profileRows, profileCoverage, false)
bool developingReady = not na(profileStartTime) and (not timeframe.change("D") or barstate.isconfirmed)
float slowReference = ta.ema(close, 13)
float swRange = math.max(high - low, syminfo.mintick)
float swVolumeAverage = ta.sma(volume, 20)
float swRawCenter = ta.vwma(hlc3, 180)
float swFallbackCenter = ta.ema(hlc3, 180)
float swVolumeCenter = na(swRawCenter) ? swFallbackCenter : swRawCenter
float swDeviation = math.max(nz(ta.stdev(hlc3, 180), atr), atr * 0.55)
float swParticipationBias = f_limit(f_ratio(ta.ema(((((not na(volume) and volume > 0 and not na(swVolumeAverage) and swVolumeAverage > 0)) ? volume : 1.0) * (((f_ratio(close - low, swRange)) - 0.5) * 2.0)), 14), (ta.ema((((not na(volume) and volume > 0 and not na(swVolumeAverage) and swVolumeAverage > 0)) ? volume : 1.0), 14))), -1.0, 1.0)
float swChannelBasis = ta.ema(hlc3, 5)
float shortHorizonRecentHigh = ta.highest(high, 12)
float shortHorizonRecentLow = ta.lowest(low, 12)
bool modeCloudBullEvidence = ((swParticipationBias * 0.45 + (f_limit(f_ratio(swVolumeCenter - swVolumeCenter[1], atr), -1.0, 1.0)) * 0.3 + (f_limit(f_ratio(close - swVolumeCenter, swDeviation), -1.0, 1.0)) * 0.25) >= 0.15 and (f_ratio(swChannelBasis - swChannelBasis[1], atr)) >= 0.0 and close >= swVolumeCenter + atr * 0.03) or (close > slowReference + atr * 0.12 and close > sharedCloseEma5 and sharedCloseEma5 > slowReference and sharedCloseEma5 > sharedCloseEma5[1] and (f_ratio(swChannelBasis - swChannelBasis[1], atr)) >= 0.025 and close - shortHorizonRecentLow >= atr * 1.0)
bool modeCloudBearEvidence = ((swParticipationBias * 0.45 + (f_limit(f_ratio(swVolumeCenter - swVolumeCenter[1], atr), -1.0, 1.0)) * 0.3 + (f_limit(f_ratio(close - swVolumeCenter, swDeviation), -1.0, 1.0)) * 0.25) <= -0.15 and (f_ratio(swChannelBasis - swChannelBasis[1], atr)) <= 0.0 and close <= swVolumeCenter - atr * 0.03) or (close < slowReference - atr * 0.12 and close < sharedCloseEma5 and sharedCloseEma5 < slowReference and sharedCloseEma5 < sharedCloseEma5[1] and (f_ratio(swChannelBasis - swChannelBasis[1], atr)) <= -0.025 and shortHorizonRecentHigh - close >= atr * 1.0)
bool shortHorizonRibbonBullRaw = modeCloudBullEvidence
bool shortHorizonRibbonBearRaw = modeCloudBearEvidence
float cloudSmoothWidth = ta.ema(math.min(math.max((math.max(nz(atr, high - low), syminfo.mintick)) * 0.35, math.abs(close - swChannelBasis) * 0.45), (math.max(nz(atr, high - low), syminfo.mintick)) * 0.75), 2)
bool cloudBullContext = barstate.isconfirmed and shortHorizonRibbonBullRaw
bool cloudBearContext = barstate.isconfirmed and shortHorizonRibbonBearRaw
bool cloudBullAligned = cloudBullContext and sharedCloseEma5 > slowReference and sharedCloseEma5 > sharedCloseEma5[1] and close > sharedCloseEma5 and ((swChannelBasis - swChannelBasis[3]) / ((math.max(nz(atr, high - low), syminfo.mintick)) * 3.0)) >= 0.02
bool cloudBearAligned = cloudBearContext and sharedCloseEma5 < slowReference and sharedCloseEma5 < sharedCloseEma5[1] and close < sharedCloseEma5 and ((swChannelBasis - swChannelBasis[3]) / ((math.max(nz(atr, high - low), syminfo.mintick)) * 3.0)) <= -0.02
f_cloudState(simple bool bullish, bool context, bool aligned, float basis, float width) =>
var bool wasActive = false
var float top = na
var float bottom = na
var bool stopped = false
var float reclaim = na
bool active = aligned
if barstate.isconfirmed
bool continuing = wasActive and not barstate.isfirst
top := aligned ? bullish ? continuing ? math.max(basis + width, top) : basis + width : continuing ? math.min(basis, top) : basis : na
bottom := aligned ? bullish ? continuing ? math.max(basis, bottom) : basis : continuing ? math.min(basis - width, bottom) : basis - width : na
wasActive := aligned
stopped := not context ? false : stopped
reclaim := not context ? na : reclaim
bool broken = active[1] and (bullish ? close < open and not na(bottom[1]) and high < bottom[1] : close > open and not na(top[1]) and low > top[1])
stopped := broken ? true : stopped
reclaim := broken ? bullish ? top[1] : bottom[1] : reclaim
stopped := stopped and not na(reclaim) and (bullish ? close > open and close > reclaim : close < open and close < reclaim) ? false : stopped
active := active and not stopped
[active, top, bottom]
[cloudBullActive, cloudBullUpperPrice, cloudBullLowerPrice] = f_cloudState(true, cloudBullContext, cloudBullAligned, swChannelBasis, cloudSmoothWidth)
[cloudBearActive, cloudBearUpperPrice, cloudBearLowerPrice] = f_cloudState(false, cloudBearContext, cloudBearAligned, swChannelBasis, cloudSmoothWidth)
// Labels depend exclusively on VWAP +/- 1 SD, RSI and alternation.
bool signalAnchor = timeframe.change("D") or barstate.isfirst
[signalVwap, signalUpperBand, signalLowerBand] = ta.vwap(hlc3, signalAnchor, 1.0)
float signalRsi = ta.rsi(close, signalRsiLength)
bool signalReady = barstate.isconfirmed and chart.is_standard and not na(signalUpperBand) and not na(signalLowerBand) and not na(signalRsi)
var int lastSignalDirection = 0
bool finalBuy = signalReady and close > signalUpperBand and signalRsi >= signalBuyRsi and lastSignalDirection != 1
bool finalSale = signalReady and close < signalLowerBand and signalRsi <= signalSaleRsi and lastSignalDirection != -1
if finalBuy
lastSignalDirection := 1
else if finalSale
lastSignalDirection := -1
plot(showSignalVwap ? signalVwap : na, title="Daily VWAP", color=signalVwapColour, linewidth=signalBandWidth, editable=false)
plot(showSignalUpper ? signalUpperBand : na, title="VWAP +1 SD", color=signalUpperColour, linewidth=signalBandWidth, editable=false)
plot(showSignalLower ? signalLowerBand : na, title="VWAP -1 SD", color=signalLowerColour, linewidth=signalBandWidth, editable=false)
plot(signalRsi, title="Signal RSI", display=display.data_window, editable=false)
pBullUpper = plot((showDirectionalStrength and cloudBullActive) ? cloudBullUpperPrice : na, "Bull Cloud Upper", color=bullCloudBorderColour, linewidth=bullCloudWidth, style=plot.style_linebr, editable=false)
pBullLower = plot((showDirectionalStrength and cloudBullActive) ? cloudBullLowerPrice : na, "Bull Cloud Lower", color=bullCloudBorderColour, linewidth=bullCloudWidth, style=plot.style_linebr, editable=false)
fill(pBullUpper, pBullLower, color=(showDirectionalStrength and cloudBullActive) ? bullCloudFillColour : na, title="Bull Cloud", fillgaps=false, editable=false)
pBearUpper = plot((showDirectionalStrength and cloudBearActive) ? cloudBearUpperPrice : na, "Bear Cloud Upper", color=bearCloudBorderColour, linewidth=bearCloudWidth, style=plot.style_linebr, editable=false)
pBearLower = plot((showDirectionalStrength and cloudBearActive) ? cloudBearLowerPrice : na, "Bear Cloud Lower", color=bearCloudBorderColour, linewidth=bearCloudWidth, style=plot.style_linebr, editable=false)
fill(pBearUpper, pBearLower, color=(showDirectionalStrength and cloudBearActive) ? bearCloudFillColour : na, title="Bear Cloud", fillgaps=false, editable=false)
plotshape(showSignals and finalBuy, title="Buy", text="Buy", style= shape.labelup, location= location.belowbar, color=buyLabelColour, textcolor=signalTextColour, size= size.tiny, offset=0, editable= false, display=display.pane)
plotshape(showSignals and finalSale, title="Sale", text="Sale", style= shape.labeldown, location= location.abovebar, color=saleLabelColour, textcolor=signalTextColour, size= size.tiny, offset=0, editable= false, display=display.pane)
var line coreUpperLine = na
var line coreLowerLine = na
var line outerUpperLine = na
var line outerLowerLine = na
var line controlLine = na
if barstate.islastconfirmedhistory or (barstate.islast and barstate.isconfirmed)
coreUpperLine := f_level(coreUpperLine, showStructureLines and showCoreUpper, (nz(profileStartTime, time)), coreUpper, vahColour, vahWidth)
coreLowerLine := f_level(coreLowerLine, showStructureLines and showCoreLower, (nz(profileStartTime, time)), coreLower, valColour, valWidth)
outerUpperLine := f_level(outerUpperLine, showStructureLines and showOuter, (nz(profileStartTime, time)), outerUpper, outerColour, outerWidth)
outerLowerLine := f_level(outerLowerLine, showStructureLines and showOuter, (nz(profileStartTime, time)), outerLower, outerColour, outerWidth)
controlLine := f_level(controlLine, showStructureLines and showControl, (nz(profileStartTime, time)), volumeControl, pocColour, pocWidth)
var array<line> developingPocSegments = array.new_line()
var line developingPocSegment = na
var float developingPocLevel = na
var int developingPocSession = na
if timeframe.change("D")
while array.size(developingPocSegments) > 0
line.delete(array.pop(developingPocSegments))
developingPocSegment := na
developingPocLevel := na
developingPocSession := na
if showDevelopingControl and barstate.isconfirmed and developingReady and not na(volumeControl)
if na(developingPocSegment) or (na(developingPocLevel) or volumeControl != developingPocLevel) or (na(developingPocSession) or developingPocSession != profileStartTime)
developingPocSegment := line.new(time, volumeControl, time_close, volumeControl, xloc=xloc.bar_time, extend=extend.none, color=developingPocColour, width=developingPocWidth)
array.push(developingPocSegments, developingPocSegment)
if array.size(developingPocSegments) > 250
line.delete(array.shift(developingPocSegments))
developingPocLevel := volumeControl
developingPocSession := profileStartTime
else
line.set_x2(developingPocSegment, time_close)
// Current-session histogram: reuse existing bins, retaining at most 100 boxes.
var array<box> histogramBoxes = array.new_box()
if barstate.islastconfirmedhistory or (barstate.islast and barstate.isconfirmed)
if array.size(histogramBoxes) > 0
for item in histogramBoxes
box.delete(item)
array.clear(histogramBoxes)
bool histogramReady = showProfileHistogram and array.size(histogramBins) > 0 and not na(histogramBase) and not na(histogramStep) and histogramStep > 0 and not na(profileStartTime)
if histogramReady
float largestBin = array.max(histogramBins)
int chartBarMillis = math.max(time_close - time, 1)
int availableMillis = math.max(time_close - profileStartTime, 1)
int maxWidthMillis = math.min(histogramWidth * chartBarMillis, availableMillis)
if largestBin > 0
for row = 0 to math.min(array.size(histogramBins), 100) - 1
float rowVolume = array.get(histogramBins, row)
if rowVolume > 0
float rowLow = histogramBase + row * histogramStep
float rowHigh = rowLow + histogramStep
int rightTime = profileStartTime + math.max(int(math.round(maxWidthMillis * rowVolume / largestBin)), 1)
color rowColour = rowLow <= volumeControl and rowHigh >= volumeControl ? pocColour : rowHigh <= coreLower ? color.teal : rowLow >= coreUpper ? color.orange : color.blue
array.push(histogramBoxes, box.new(profileStartTime, rowHigh, rightTime, rowLow, xloc=xloc.bar_time, border_color=na, bgcolor=color.new(rowColour, histogramTransparency)))