本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Custom Range Box + AVWAP", shorttitle="Range Box", overlay = true,
max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// =============================================================================
// CUSTOM RANGE BOX + ANCHORED VWAP
// Select a session window (e.g. Lunch IB 12:00-13:00 America/New_York).
// Draws a box of that window's high/low (box stops at window end).
// Internal levels + extensions keep extending until price touches them.
// Optional VWAP anchored to the first bar of the window.
// Every ratio, color, width, style and toggle is adjustable.
// =============================================================================
// ---------- Sessions ----------
grpS = "Sessions"
tz = input.string("America/New_York", "Timezone",
options = ["America/New_York", "America/Chicago", "America/Los_Angeles", "UTC", "Exchange"],
group = grpS)
rangeSess = input.session("1200-1300", "Range window",
tooltip = "Start–end of the range (end is exclusive in TradingView sessions). Example: 1200-1300 = 12:00–1:00 PM ET.",
group = grpS)
extendMode = input.string("End of RTH", "Levels max lifetime (untouched)",
options = ["Until next range", "End of RTH", "Fixed bars after range"],
tooltip = "Box always ends when the range window closes. Level lines keep extending until price touches them, or until this lifetime cap.",
group = grpS)
rthSess = input.session("0930-1600", "Trading session (for End of RTH)",
tooltip = "With End of RTH, the levels, AVWAP and bands stop when this session closes and stay off until the next range forms.",
group = grpS)
extendBars = input.int(48, "Fixed bars after range", minval = 1, maxval = 500, group = grpS)
histDays = input.int(5, "Days of history to keep", minval = 1, maxval = 30, group = grpS)
// ---------- Box ----------
grpB = "Box"
showBox = input.bool(true, "Show range box", group = grpB)
boxFill = input.color(color.new(#2962FF, 88), "Box fill", group = grpB)
boxBorder = input.color(color.new(#2962FF, 40), "Box border", group = grpB)
boxBorderW = input.int(1, "Border width", minval = 1, maxval = 4, group = grpB)
boxStyle = input.string("Solid", "Border style", options = ["Solid", "Dashed", "Dotted"], group = grpB)
// ---------- High / Low ----------
grpHL = "Range High / Low"
showHi = input.bool(true, "Show High", group = grpHL)
colHi = input.color(color.new(#089981, 0), "High color", group = grpHL)
wHi = input.int(2, "High width", minval = 1, maxval = 4, group = grpHL)
stHi = input.string("Solid", "High style", options = ["Solid", "Dashed", "Dotted"], group = grpHL)
lblHi = input.bool(true, "High label", group = grpHL)
showLo = input.bool(true, "Show Low", group = grpHL)
colLo = input.color(color.new(#F23645, 0), "Low color", group = grpHL)
wLo = input.int(2, "Low width", minval = 1, maxval = 4, group = grpHL)
stLo = input.string("Solid", "Low style", options = ["Solid", "Dashed", "Dotted"], group = grpHL)
lblLo = input.bool(true, "Low label", group = grpHL)
// ---------- Internal levels (between High and Low) ----------
grpI = "Internal Levels"
showI1 = input.bool(true, "Level 1", inline = "i1", group = grpI)
ratI1 = input.float(0.25, "", step = 0.001, inline = "i1", group = grpI)
colI1 = input.color(color.new(color.gray, 0), "", inline = "i1", group = grpI)
wI1 = input.int(1, "", minval = 1, maxval = 4, inline = "i1", group = grpI)
stI1 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "i1", group = grpI)
lblI1 = input.bool(true, "Label", inline = "i1b", group = grpI)
showI2 = input.bool(true, "Level 2 (mid)", inline = "i2", group = grpI)
ratI2 = input.float(0.50, "", step = 0.001, inline = "i2", group = grpI)
colI2 = input.color(color.new(color.orange, 0), "", inline = "i2", group = grpI)
wI2 = input.int(1, "", minval = 1, maxval = 4, inline = "i2", group = grpI)
stI2 = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "i2", group = grpI)
lblI2 = input.bool(true, "Label", inline = "i2b", group = grpI)
showI3 = input.bool(true, "Level 3", inline = "i3", group = grpI)
ratI3 = input.float(0.75, "", step = 0.001, inline = "i3", group = grpI)
colI3 = input.color(color.new(color.gray, 0), "", inline = "i3", group = grpI)
wI3 = input.int(1, "", minval = 1, maxval = 4, inline = "i3", group = grpI)
stI3 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "i3", group = grpI)
lblI3 = input.bool(true, "Label", inline = "i3b", group = grpI)
showI4 = input.bool(false, "Level 4", inline = "i4", group = grpI)
ratI4 = input.float(0.618, "", step = 0.001, inline = "i4", group = grpI)
colI4 = input.color(color.new(#9C27B0, 0), "", inline = "i4", group = grpI)
wI4 = input.int(1, "", minval = 1, maxval = 4, inline = "i4", group = grpI)
stI4 = input.string("Dotted", "", options = ["Solid", "Dashed", "Dotted"], inline = "i4", group = grpI)
lblI4 = input.bool(true, "Label", inline = "i4b", group = grpI)
// ---------- Extensions ABOVE high ----------
grpA = "Extensions Above"
showA1 = input.bool(true, "Above 1", inline = "a1", group = grpA)
ratA1 = input.float(0.25, "", step = 0.001, inline = "a1", group = grpA)
colA1 = input.color(color.new(#089981, 30), "", inline = "a1", group = grpA)
wA1 = input.int(1, "", minval = 1, maxval = 4, inline = "a1", group = grpA)
stA1 = input.string("Dotted", "", options = ["Solid", "Dashed", "Dotted"], inline = "a1", group = grpA)
lblA1 = input.bool(true, "Label", inline = "a1b", group = grpA)
showA2 = input.bool(true, "Above 2", inline = "a2", group = grpA)
ratA2 = input.float(0.50, "", step = 0.001, inline = "a2", group = grpA)
colA2 = input.color(color.new(#089981, 20), "", inline = "a2", group = grpA)
wA2 = input.int(1, "", minval = 1, maxval = 4, inline = "a2", group = grpA)
stA2 = input.string("Dotted", "", options = ["Solid", "Dashed", "Dotted"], inline = "a2", group = grpA)
lblA2 = input.bool(true, "Label", inline = "a2b", group = grpA)
showA3 = input.bool(true, "Above 3", inline = "a3", group = grpA)
ratA3 = input.float(0.75, "", step = 0.001, inline = "a3", group = grpA)
colA3 = input.color(color.new(#089981, 10), "", inline = "a3", group = grpA)
wA3 = input.int(1, "", minval = 1, maxval = 4, inline = "a3", group = grpA)
stA3 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "a3", group = grpA)
lblA3 = input.bool(true, "Label", inline = "a3b", group = grpA)
showA4 = input.bool(true, "Above 4", inline = "a4", group = grpA)
ratA4 = input.float(1.00, "", step = 0.001, inline = "a4", group = grpA)
colA4 = input.color(color.new(#089981, 0), "", inline = "a4", group = grpA)
wA4 = input.int(1, "", minval = 1, maxval = 4, inline = "a4", group = grpA)
stA4 = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "a4", group = grpA)
lblA4 = input.bool(true, "Label", inline = "a4b", group = grpA)
showA5 = input.bool(false, "Above 5", inline = "a5", group = grpA)
ratA5 = input.float(1.618, "", step = 0.001, inline = "a5", group = grpA)
colA5 = input.color(color.new(#26A69A, 0), "", inline = "a5", group = grpA)
wA5 = input.int(1, "", minval = 1, maxval = 4, inline = "a5", group = grpA)
stA5 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "a5", group = grpA)
lblA5 = input.bool(true, "Label", inline = "a5b", group = grpA)
// ---------- Extensions BELOW low ----------
grpL = "Extensions Below"
showB1 = input.bool(true, "Below 1", inline = "b1", group = grpL)
ratB1 = input.float(0.25, "", step = 0.001, inline = "b1", group = grpL)
colB1 = input.color(color.new(#F23645, 30), "", inline = "b1", group = grpL)
wB1 = input.int(1, "", minval = 1, maxval = 4, inline = "b1", group = grpL)
stB1 = input.string("Dotted", "", options = ["Solid", "Dashed", "Dotted"], inline = "b1", group = grpL)
lblB1 = input.bool(true, "Label", inline = "b1b", group = grpL)
showB2 = input.bool(true, "Below 2", inline = "b2", group = grpL)
ratB2 = input.float(0.50, "", step = 0.001, inline = "b2", group = grpL)
colB2 = input.color(color.new(#F23645, 20), "", inline = "b2", group = grpL)
wB2 = input.int(1, "", minval = 1, maxval = 4, inline = "b2", group = grpL)
stB2 = input.string("Dotted", "", options = ["Solid", "Dashed", "Dotted"], inline = "b2", group = grpL)
lblB2 = input.bool(true, "Label", inline = "b2b", group = grpL)
showB3 = input.bool(true, "Below 3", inline = "b3", group = grpL)
ratB3 = input.float(0.75, "", step = 0.001, inline = "b3", group = grpL)
colB3 = input.color(color.new(#F23645, 10), "", inline = "b3", group = grpL)
wB3 = input.int(1, "", minval = 1, maxval = 4, inline = "b3", group = grpL)
stB3 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "b3", group = grpL)
lblB3 = input.bool(true, "Label", inline = "b3b", group = grpL)
showB4 = input.bool(true, "Below 4", inline = "b4", group = grpL)
ratB4 = input.float(1.00, "", step = 0.001, inline = "b4", group = grpL)
colB4 = input.color(color.new(#F23645, 0), "", inline = "b4", group = grpL)
wB4 = input.int(1, "", minval = 1, maxval = 4, inline = "b4", group = grpL)
stB4 = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], inline = "b4", group = grpL)
lblB4 = input.bool(true, "Label", inline = "b4b", group = grpL)
showB5 = input.bool(false, "Below 5", inline = "b5", group = grpL)
ratB5 = input.float(1.618, "", step = 0.001, inline = "b5", group = grpL)
colB5 = input.color(color.new(#EF5350, 0), "", inline = "b5", group = grpL)
wB5 = input.int(1, "", minval = 1, maxval = 4, inline = "b5", group = grpL)
stB5 = input.string("Dashed", "", options = ["Solid", "Dashed", "Dotted"], inline = "b5", group = grpL)
lblB5 = input.bool(true, "Label", inline = "b5b", group = grpL)
lblSizeIn = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal", "Large"], group = "Labels")
lblRight = input.bool(true, "Labels on right edge", group = "Labels")
// ---------- Anchored VWAP ----------
grpV = "Anchored VWAP"
useVwap = input.bool(true, "Enable AVWAP (anchor = range open)", group = grpV)
vwapSrc = input.source(hlc3, "Source", group = grpV)
vwapCol = input.color(color.new(#FF9800, 0), "VWAP color", group = grpV)
vwapW = input.int(2, "VWAP width", minval = 1, maxval = 4, group = grpV)
// AVWAP uses plot() (solid). Width + color apply; dashed/dotted available on box levels.
vwapLbl = input.bool(true, "VWAP label", group = grpV)
vwapUntil = input.string("End of RTH", "AVWAP extends until",
options = ["Until next range", "End of RTH", "Fixed bars after range"],
group = grpV)
grpVB = "AVWAP Bands"
showSd1 = input.bool(true, "±1σ", inline = "sd1", group = grpVB)
mult1 = input.float(1.0, "×", step = 0.1, inline = "sd1", group = grpVB)
colSd1 = input.color(color.new(#FF9800, 50), "", inline = "sd1", group = grpVB)
wSd1 = input.int(1, "", minval = 1, maxval = 4, inline = "sd1", group = grpVB)
showSd2 = input.bool(true, "±2σ", inline = "sd2", group = grpVB)
mult2 = input.float(2.0, "×", step = 0.1, inline = "sd2", group = grpVB)
colSd2 = input.color(color.new(#FF9800, 65), "", inline = "sd2", group = grpVB)
wSd2 = input.int(1, "", minval = 1, maxval = 4, inline = "sd2", group = grpVB)
showSd3 = input.bool(false, "±3σ", inline = "sd3", group = grpVB)
mult3 = input.float(3.0, "×", step = 0.1, inline = "sd3", group = grpVB)
colSd3 = input.color(color.new(#FF9800, 75), "", inline = "sd3", group = grpVB)
wSd3 = input.int(1, "", minval = 1, maxval = 4, inline = "sd3", group = grpVB)
fillBands = input.bool(true, "Fill ±1σ band", group = grpVB)
fillCol = input.color(color.new(#FF9800, 92), "Band fill", group = grpVB)
bandLbl = input.bool(true, "Show band labels", group = grpVB)
bandLblPx = input.bool(false, "Include price in band labels", group = grpVB)
// =============================================================================
// Helpers
// =============================================================================
lineStyleOf(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
boxStyleOf(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
labelSizeOf(string s) =>
s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
tzEff = tz == "Exchange" ? syminfo.timezone : tz
inRange = not na(time(timeframe.period, rangeSess, tzEff))
inRth = not na(time(timeframe.period, rthSess, tzEff))
rangeStart = inRange and not inRange[1]
rangeEnd = not inRange and inRange[1]
// =============================================================================
// Live range tracking
// =============================================================================
var float curHi = na
var float curLo = na
var int curStartBar = na
var int curEndBar = na
var bool rangeLive = false
var bool rangeDone = false
if rangeStart
curHi := high
curLo := low
curStartBar := bar_index
curEndBar := bar_index
rangeLive := true
rangeDone := false
if rangeLive and inRange
curHi := math.max(nz(curHi, high), high)
curLo := math.min(nz(curLo, low), low)
curEndBar := bar_index
if rangeEnd
rangeLive := false
rangeDone := true
curEndBar := bar_index - 1
rng = nz(curHi) - nz(curLo)
// Session end: once the trading session closes after a range has formed,
// everything for that range stops until the next range starts.
rthEnd = inRth[1] and not inRth
var bool sessOver = false
if rangeStart
sessOver := false
if rthEnd and (rangeDone or rangeLive)
sessOver := true
// =============================================================================
// Anchored VWAP (from first bar of range)
// =============================================================================
var float sumPV = 0.0
var float sumV = 0.0
var float sumPV2 = 0.0
var bool vwapOn = false
var int vwapBars = 0
vol = volume > 0 ? volume : 1.0
src = vwapSrc
if rangeStart
sumPV := src * vol
sumV := vol
sumPV2 := src * src * vol
vwapOn := useVwap
vwapBars := 0
vwapStill = false
if vwapOn
if vwapUntil == "Until next range"
vwapStill := true
else if vwapUntil == "End of RTH"
vwapStill := not sessOver
else
vwapStill := vwapBars <= extendBars
if vwapOn and not rangeStart and (inRange or (rangeDone and vwapStill))
sumPV += src * vol
sumV += vol
sumPV2 += src * src * vol
vwapBars += 1
if vwapOn and not vwapStill and not inRange and rangeDone
// freeze values; keep last plotted until next range resets
vwapOn := vwapOn
avwap = sumV > 0 ? sumPV / sumV : na
variance = sumV > 0 ? math.max(sumPV2 / sumV - avwap * avwap, 0.0) : na
stdev = not na(variance) ? math.sqrt(variance) : na
showVwapNow = useVwap and not na(avwap) and (inRange or rangeDone) and (vwapUntil == "Until next range" or vwapStill or inRange)
pVwap = plot(showVwapNow ? avwap : na, "AVWAP", color = vwapCol, linewidth = vwapW, style = plot.style_linebr)
pUp1 = plot(showVwapNow and showSd1 and not na(stdev) ? avwap + mult1 * stdev : na, "+1σ", color = colSd1, linewidth = wSd1, style = plot.style_linebr)
pDn1 = plot(showVwapNow and showSd1 and not na(stdev) ? avwap - mult1 * stdev : na, "-1σ", color = colSd1, linewidth = wSd1, style = plot.style_linebr)
pUp2 = plot(showVwapNow and showSd2 and not na(stdev) ? avwap + mult2 * stdev : na, "+2σ", color = colSd2, linewidth = wSd2, style = plot.style_linebr)
pDn2 = plot(showVwapNow and showSd2 and not na(stdev) ? avwap - mult2 * stdev : na, "-2σ", color = colSd2, linewidth = wSd2, style = plot.style_linebr)
pUp3 = plot(showVwapNow and showSd3 and not na(stdev) ? avwap + mult3 * stdev : na, "+3σ", color = colSd3, linewidth = wSd3, style = plot.style_linebr)
pDn3 = plot(showVwapNow and showSd3 and not na(stdev) ? avwap - mult3 * stdev : na, "-3σ", color = colSd3, linewidth = wSd3, style = plot.style_linebr)
fill(pUp1, pDn1, color = fillBands and showSd1 ? fillCol : na, title = "±1σ fill")
// =============================================================================
// Drawing objects (history + live day)
// Box: always ends at the range window close.
// Levels: keep extending until price trades through them (or lifetime cap).
// Pine forbids mutating function args (CE10175) — helpers return new values.
// =============================================================================
type Lvl
line ln
label lb
float price
bool touched
type DayDraw
box bx
array<Lvl> lvls
int startBar
int boxEndBar
int endBar
float hi
float lo
bool locked
bool rangeClosed
var array<DayDraw> days = array.new<DayDraw>()
var DayDraw live = na
f_makeLine(int x1, float y, int x2, color c, int w, string st) =>
line.new(x1, y, x2, y, xloc = xloc.bar_index, color = c, width = w, style = lineStyleOf(st))
f_makeLbl(int x, float y, string txt, color c) =>
label.new(x, y, txt, xloc = xloc.bar_index, style = label.style_label_left,
color = color.new(c, 100), textcolor = c, size = labelSizeOf(lblSizeIn))
f_fmt(float r) =>
str.tostring(r, "#.###")
f_deleteDrawings(DayDraw dIn) =>
if not na(dIn.bx)
box.delete(dIn.bx)
if not na(dIn.lvls)
for L in dIn.lvls
if not na(L.ln)
line.delete(L.ln)
if not na(L.lb)
label.delete(L.lb)
true
f_pushLevel(array<Lvl> lvls, bool show, float y, string txt, color c, int w, string st, bool doLbl, int x1, int x2, int xl) =>
if show and not na(y)
ln = f_makeLine(x1, y, x2, c, w, st)
lb = doLbl ? f_makeLbl(xl, y, txt, c) : na
array.push(lvls, Lvl.new(ln, lb, y, false))
true
f_rebuild(DayDraw dIn, float hi, float lo, int x1, int boxX2, int lineX2, bool closed) =>
f_deleteDrawings(dIn)
array<Lvl> lvls = array.new<Lvl>()
xl = lblRight ? lineX2 : x1
rngD = hi - lo
box bx = na
if showBox
bx := box.new(x1, hi, boxX2, lo, xloc = xloc.bar_index,
border_color = boxBorder, bgcolor = boxFill,
border_width = boxBorderW, border_style = boxStyleOf(boxStyle))
f_pushLevel(lvls, showHi, hi, "High", colHi, wHi, stHi, lblHi, x1, lineX2, xl)
f_pushLevel(lvls, showLo, lo, "Low", colLo, wLo, stLo, lblLo, x1, lineX2, xl)
f_pushLevel(lvls, showI1, lo + ratI1 * rngD, f_fmt(ratI1), colI1, wI1, stI1, lblI1, x1, lineX2, xl)
f_pushLevel(lvls, showI2, lo + ratI2 * rngD, f_fmt(ratI2), colI2, wI2, stI2, lblI2, x1, lineX2, xl)
f_pushLevel(lvls, showI3, lo + ratI3 * rngD, f_fmt(ratI3), colI3, wI3, stI3, lblI3, x1, lineX2, xl)
f_pushLevel(lvls, showI4, lo + ratI4 * rngD, f_fmt(ratI4), colI4, wI4, stI4, lblI4, x1, lineX2, xl)
f_pushLevel(lvls, showA1, hi + ratA1 * rngD, "+" + f_fmt(ratA1), colA1, wA1, stA1, lblA1, x1, lineX2, xl)
f_pushLevel(lvls, showA2, hi + ratA2 * rngD, "+" + f_fmt(ratA2), colA2, wA2, stA2, lblA2, x1, lineX2, xl)
f_pushLevel(lvls, showA3, hi + ratA3 * rngD, "+" + f_fmt(ratA3), colA3, wA3, stA3, lblA3, x1, lineX2, xl)
f_pushLevel(lvls, showA4, hi + ratA4 * rngD, "+" + f_fmt(ratA4), colA4, wA4, stA4, lblA4, x1, lineX2, xl)
f_pushLevel(lvls, showA5, hi + ratA5 * rngD, "+" + f_fmt(ratA5), colA5, wA5, stA5, lblA5, x1, lineX2, xl)
f_pushLevel(lvls, showB1, lo - ratB1 * rngD, "-" + f_fmt(ratB1), colB1, wB1, stB1, lblB1, x1, lineX2, xl)
f_pushLevel(lvls, showB2, lo - ratB2 * rngD, "-" + f_fmt(ratB2), colB2, wB2, stB2, lblB2, x1, lineX2, xl)
f_pushLevel(lvls, showB3, lo - ratB3 * rngD, "-" + f_fmt(ratB3), colB3, wB3, stB3, lblB3, x1, lineX2, xl)
f_pushLevel(lvls, showB4, lo - ratB4 * rngD, "-" + f_fmt(ratB4), colB4, wB4, stB4, lblB4, x1, lineX2, xl)
f_pushLevel(lvls, showB5, lo - ratB5 * rngD, "-" + f_fmt(ratB5), colB5, wB5, stB5, lblB5, x1, lineX2, xl)
DayDraw.new(bx, lvls, x1, boxX2, lineX2, hi, lo, not na(dIn) ? dIn.locked : false, closed)
// After the range closes: keep box fixed; extend untouched levels until touch / cap.
f_extendLevels(DayDraw dIn, int x2, bool allowTouch) =>
array<Lvl> out = array.new<Lvl>()
maxEnd = dIn.boxEndBar
if not na(dIn.lvls)
for L in dIn.lvls
touched = L.touched
xEnd = line.get_x2(L.ln)
if not touched
hit = allowTouch and not na(L.price) and low <= L.price and high >= L.price
if hit
touched := true
xEnd := x2
line.set_x2(L.ln, xEnd)
if lblRight and not na(L.lb)
label.set_x(L.lb, xEnd)
else
xEnd := x2
line.set_x2(L.ln, xEnd)
if lblRight and not na(L.lb)
label.set_x(L.lb, xEnd)
maxEnd := math.max(maxEnd, xEnd)
array.push(out, Lvl.new(L.ln, L.lb, L.price, touched))
// Box stays at boxEndBar — never move it here
DayDraw.new(dIn.bx, out, dIn.startBar, dIn.boxEndBar, maxEnd, dIn.hi, dIn.lo, dIn.locked, true)
if rangeStart
if not na(live)
lockedLive = DayDraw.new(live.bx, live.lvls, live.startBar, live.boxEndBar, live.endBar, live.hi, live.lo, true, live.rangeClosed)
array.push(days, lockedLive)
while array.size(days) > histDays
old = array.shift(days)
f_deleteDrawings(old)
live := DayDraw.new(na, array.new<Lvl>(), bar_index, bar_index, bar_index, high, low, false, false)
// Also keep extending historical unlocked? No — only live day. Prior days are locked
// but their untouched levels should have stopped at lock. Optionally continue
// extending locked days' untouched levels until touch — user said levels continue
// until touched, so update historical days too while within their lifetime.
f_stillActive() =>
extendMode == "Until next range" ? true : extendMode == "End of RTH" ? not sessOver : (bar_index - nz(curEndBar)) <= extendBars
if not na(live) and not live.locked
x1 = nz(curStartBar, live.startBar)
if rangeLive or inRange
// Building the range: box + levels grow together; no touch tests yet
boxX2 = bar_index
lineX2 = bar_index
needBuild = na(live.lvls) or array.size(live.lvls) == 0
levelsMoved = curHi != live.hi or curLo != live.lo
if needBuild or levelsMoved or live.boxEndBar != boxX2
live := f_rebuild(live, curHi, curLo, x1, boxX2, lineX2, false)
else if rangeDone and f_stillActive()
// Range closed: freeze box at curEndBar; extend levels until touch
boxX2 = nz(curEndBar, live.boxEndBar)
if not live.rangeClosed or live.hi != curHi or live.lo != curLo or na(live.bx) and showBox
live := f_rebuild(live, curHi, curLo, x1, boxX2, boxX2, true)
live := f_extendLevels(live, bar_index, true)
f_bandTxt(string name, float px) =>
bandLblPx ? name + " " + str.tostring(px, format.mintick) : name
f_vwapLbl(float y, string txt, color c) =>
label.new(bar_index, y, txt, xloc = xloc.bar_index,
style = label.style_label_left, color = color.new(c, 100),
textcolor = c, size = labelSizeOf(lblSizeIn))
var array<label> vwapLabels = array.new<label>()
if array.size(vwapLabels) > 0
for lb in vwapLabels
label.delete(lb)
array.clear(vwapLabels)
if showVwapNow and barstate.islast
if vwapLbl and not na(avwap)
array.push(vwapLabels, f_vwapLbl(avwap, "AVWAP", vwapCol))
if bandLbl and not na(stdev)
if showSd1
up1 = avwap + mult1 * stdev
dn1 = avwap - mult1 * stdev
array.push(vwapLabels, f_vwapLbl(up1, f_bandTxt("+" + str.tostring(mult1, "#.#") + "σ", up1), colSd1))
array.push(vwapLabels, f_vwapLbl(dn1, f_bandTxt("-" + str.tostring(mult1, "#.#") + "σ", dn1), colSd1))
if showSd2
up2 = avwap + mult2 * stdev
dn2 = avwap - mult2 * stdev
array.push(vwapLabels, f_vwapLbl(up2, f_bandTxt("+" + str.tostring(mult2, "#.#") + "σ", up2), colSd2))
array.push(vwapLabels, f_vwapLbl(dn2, f_bandTxt("-" + str.tostring(mult2, "#.#") + "σ", dn2), colSd2))
if showSd3
up3 = avwap + mult3 * stdev
dn3 = avwap - mult3 * stdev
array.push(vwapLabels, f_vwapLbl(up3, f_bandTxt("+" + str.tostring(mult3, "#.#") + "σ", up3), colSd3))
array.push(vwapLabels, f_vwapLbl(dn3, f_bandTxt("-" + str.tostring(mult3, "#.#") + "σ", dn3), colSd3))