ETH Volume Profile [jfx]

joojix-fx · study · 518 行 · 点赞 27 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// ETH Volume Profile [jfx]
// The overnight / electronic (ETH) session profile: the full CME day, from the 18:00 New York
// open (the start of Asia) to the 17:00 close, RTH included. Replaces SVP: same footprint engine
// and look as RTH VP - volume at every tick, value area in colour.
//
// - Every day gets its profile inside its own session, starting at the Asia open.
// - A live delta profile of today so far sits to the right of price, like RTH VP's: net delta per
//   price row, the bars hanging left from a right edge, green where buying won and purple where
//   selling won, the bigger ones in full colour and the smaller in a lighter shade. By default it
//   sits beyond RTH VP's, so the two can be on the chart together.
// - Today's O VAH / O POC / O VAL ("O" = overnight) run from the Asia open to the latest candle.
// - Past days keep their levels inside their day, labelled VAH / POC / VAL at the day's end.
// - HVN / LVN zones on every day, as RTH VP's: the high-volume band around the POC (HVN), and just
//   outside VAH / VAL where the volume thins out (LVN). Today's move as the day trades.
//   Past days have no delta: TradingView swaps their footprint for made-up rows after the close.
//
// SVP (svp.pine) and RTH VP are untouched. Charts up to 30M only.
indicator('ETH Volume Profile [jfx]', 'ETH VP [jfx]', overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500, max_polylines_count = 100)
 
//#region Inputs
 
const string SESSION_GROUP = 'Session'
const string LEVEL_GROUP = 'Levels'
const string ZONE_GROUP = 'HVN / LVN zones'
const string DELTA_GROUP = 'Live delta profile (today)'
const string LOOK_GROUP = 'Appearance'
 
int KEEP = input.int(5, 'Days to show', minval = 1, maxval = 20, group = SESSION_GROUP, tooltip = 'Today plus this many minus one before it. Days start at the CME open, 18:00 New York (the Asia open).', display = display.none)
bool SHOW_TODAY = input.bool(true, 'Today\'s profile', group = SESSION_GROUP, tooltip = 'Build today\'s profile live from the Asia open.', display = display.none)
bool TODAY_LEVELS = input.bool(true, 'Today\'s O levels', group = SESSION_GROUP, tooltip = 'O VAH / O POC / O VAL: today\'s overnight (ETH) value area high, point of control and value area low, over the full CME day so far, from the Asia open to the latest candle. They move as the day trades.', display = display.none)
bool PAST_LABELS = input.bool(true, 'Label past days', group = SESSION_GROUP, tooltip = 'Write VAH / POC / VAL at the end of each past day\'s level lines, like SVP.', display = display.none)
 
// Fixed for ETH: today's volume profile stays in its session.
const string TODAY_PLACE = 'In session'
const int RIGHT_OFFSET = 0
const int RIGHT_WIDTH = 0
const string TZ = 'America/New_York'
 
int VALUE_AREA = input.int(70, 'Value area %', minval = 1, maxval = 100, group = LEVEL_GROUP, tooltip = 'Share of the session volume inside VAH-VAL. Grows from the POC one level at a time towards the busier side.', display = display.none)
int TICKS_PER_LEVEL = input.int(0, 'Ticks per level', minval = 0, maxval = 100, group = LEVEL_GROUP, tooltip = 'Price step the volume is counted in. 0 = Auto: 1 tick (0.25 on NQ) up to 15M, 4 ticks on 30M, where big candles can come without a 1-tick footprint.', display = display.none)
bool SHOW_POC = input.bool(true, 'POC', inline = 'poc', group = LEVEL_GROUP, tooltip = 'Point of control: the price with the most volume.', display = display.none)
color POC_COLOR = input.color(#d1d4dc, '', inline = 'poc', group = LEVEL_GROUP, display = display.none)
bool SHOW_VA = input.bool(true, 'VAH / VAL', inline = 'va', group = LEVEL_GROUP, tooltip = 'Value area high and low.', display = display.none)
color VAH_COLOR = input.color(#d1d4dc, '', inline = 'va', group = LEVEL_GROUP, display = display.none)
color VAL_COLOR = input.color(#d1d4dc, '', inline = 'va', group = LEVEL_GROUP, display = display.none)
int LINE_WIDTH = input.int(1, 'Line width', minval = 1, maxval = 4, group = LEVEL_GROUP, tooltip = 'Width of the O level lines.', display = display.none)
bool SHOW_PRICES = input.bool(false, 'Prices in labels', group = LEVEL_GROUP, tooltip = 'Write the price after O VAH / O POC / O VAL.', display = display.none)
 
bool SHOW_ZONES = input.bool(true, 'HVN / LVN zones', inline = 'zone', group = ZONE_GROUP, tooltip = 'Shaded bands inside each day, from its Asia open to its end (today: to the latest candle, moving as the day trades). HVN (high-volume node): around the POC, where most of the volume traded. LVN (low-volume node): just outside VAH and VAL, where the volume thins out.', display = display.none)
color ZONE_COLOR = input.color(#43465166, '', inline = 'zone', group = ZONE_GROUP, display = display.none)
float POC_ZONE_PCT = input.float(75, 'HVN zone: down to %', minval = 10, maxval = 95, step = 5, group = ZONE_GROUP, tooltip = 'The HVN zone reaches up and down from the POC until the (smoothed) volume falls below this share of the POC\'s. Lower = wider zone. It never goes past VAH or VAL.', display = display.none)
float REACH_PCT = input.float(50, 'LVN zone: search reach %', minval = 5, maxval = 100, step = 5, group = ZONE_GROUP, tooltip = 'How far above VAH / below VAL to look for the thin area, as a share of the value area height. If the volume never thins out within reach, there is no zone at that edge.', display = display.none)
float THIN_PCT = input.float(25, 'LVN zone: thin below %', minval = 5, maxval = 80, step = 5, group = ZONE_GROUP, tooltip = 'Outside VAH / VAL, the zone starts at the first price where the (smoothed) volume falls below this share of the POC volume, and runs on outwards while it stays below. Higher = the zone starts closer to the edge line.', display = display.none)
float ZONE_MAX_PCT = input.float(10, 'LVN zone: max height %', minval = 2, maxval = 50, step = 1, group = ZONE_GROUP, tooltip = 'Tallest an LVN zone can be, as a share of the value area height.', display = display.none)
float SMOOTH_PCT = input.float(1.5, 'Smoothing %', minval = 0.1, maxval = 10, step = 0.1, group = ZONE_GROUP, tooltip = 'Before finding the zones the profile is blurred over roughly this share of its height, so single-tick spikes do not count. Zones only: the drawn profile and the levels are not blurred.', display = display.none)
 
bool SHOW_DELTA = input.bool(true, 'Live delta profile', group = DELTA_GROUP, tooltip = 'The net delta at every price row of today so far, from the Asia open (bought at the ask minus sold at the bid), to the right of price. Updates with every tick. Past days have no delta: TradingView swaps their footprint for made-up rows after the close.', display = display.none)
color BUY_COLOR = input.color(#388e3c, 'Buying', group = DELTA_GROUP, tooltip = 'Rows where buying at the ask won. Smaller rows are drawn in a lighter shade of it.', display = display.none)
color SELL_COLOR = input.color(#673ab7, 'Selling', group = DELTA_GROUP, tooltip = 'Rows where selling at the bid won. Smaller rows are drawn in a lighter shade of it.', display = display.none)
int STRONG_PCT = input.int(40, 'Full colour from %', minval = 0, maxval = 100, step = 5, group = DELTA_GROUP, tooltip = 'Rows with at least this share of the largest delta are drawn in the full colour, the rest in the lighter shade. 0 = all full colour.', display = display.none)
int ROW_TICKS = input.int(4, 'Row size (ticks)', minval = 1, maxval = 100, group = DELTA_GROUP, tooltip = 'Price height of one delta row. 4 ticks = 1 point on NQ. Rows are grouped further if the day would need more than 2,400.', display = display.none)
int LIVE_OFFSET = input.int(60, 'Gap from price (bars)', minval = 0, maxval = 300, group = DELTA_GROUP, tooltip = 'Bars between the latest candle and the left edge of the delta profile (where its largest row reaches). 60 puts it beyond RTH VP\'s live delta profile at its defaults.', display = display.none)
int LIVE_DELTA_BARS = input.int(40, 'Width (bars)', minval = 2, maxval = 300, group = DELTA_GROUP, tooltip = 'Width of the largest delta row, in chart bars.', display = display.none)
 
int WIDTH_PCT = input.int(25, 'Profile width %', minval = 5, maxval = 100, step = 5, group = LOOK_GROUP, tooltip = 'Width of the busiest price of a profile, as a share of the day (23 hours). Today\'s profile uses the same scale, so it grows through the day.', display = display.none)
int MAX_ROWS = input.int(2400, 'Most rows', minval = 100, maxval = 4000, group = LOOK_GROUP, tooltip = 'Most rows a profile is drawn with. A session with more prices than this has neighbouring prices added together, the same number in every row (2, 3, ...). Looks only: the levels and zones always use every price. A full NQ day at 1 tick is usually 1,500-3,500 prices.', display = display.none)
color PROFILE_COLOR = input.color(#787b8673, 'Profile', group = LOOK_GROUP, tooltip = 'Prices outside the value area.', display = display.none)
color VA_COLOR = input.color(#2962ffb3, 'Value area', group = LOOK_GROUP, tooltip = 'Prices inside the value area.', display = display.none)
bool OUTLINE = input.bool(true, 'Outline', group = LOOK_GROUP, tooltip = 'A thin, lighter line along the edge of the volume bars, so each price stands out.', display = display.none)
 
//#endregion
 
//#region Data
 
int TF_SECONDS = timeframe.in_seconds()
bool OFF = timeframe.isdwm or TF_SECONDS > 30 * 60
int TICKS = TICKS_PER_LEVEL > 0 ? TICKS_PER_LEVEL : TF_SECONDS <= 15 * 60 ? 1 : 4
float LEVEL_SIZE = syminfo.mintick * TICKS
// A session's levels are marked "~" when less than this share of its volume is real footprint.
const float COVERAGE_OK = 0.97
// A session's delta is drawn in colour only if real (not made-up) footprint covers this share.
const float DELTA_COVERAGE = 0.9
 
// A CME day is 23 hours; the profile widths scale to it.
const int SESSION_MS = 23 * 60 * 60 * 1000
 
// Real traded volume and bid / ask split per price row for this candle. No calc_bars_count in
// indicator(): it changes the footprint data a script receives.
footprint fp = request.footprint(TICKS, VALUE_AREA)
 
// Every candle belongs to a CME day; a new day starts at the 18:00 New York open.
bool inRth = true
bool rthStart = timeframe.change('D') or barstate.isfirst
 
levelOf(float price) =>
    int(math.floor(price / LEVEL_SIZE + 1e-6))
 
// A level's price is the middle of the ticks it holds: the traded price itself at 1 tick.
levelPrice(int level) =>
    level * LEVEL_SIZE + (TICKS - 1) * syminfo.mintick / 2
 
// POC = the first busiest level. The value area grows from it one level at a time towards the
// busier neighbour until it holds VALUE_AREA % of the volume.
valueArea(array<float> d) =>
    int n = d.size()
    int poc = 0
    float best = d.get(0)
    if n > 1
        for i = 1 to n - 1
            if d.get(i) > best
                best := d.get(i)
                poc := i
    float target = d.sum() * VALUE_AREA / 100
    float cur = d.get(poc)
    int hi = poc
    int lo = poc
    while cur < target
        bool canUp = hi < n - 1
        bool canDn = lo > 0
        if not canUp and not canDn
            break
        float above = canUp ? d.get(hi + 1) : 0.
        float below = canDn ? d.get(lo - 1) : 0.
        if above >= below and canUp
            cur += above
            hi += 1
        else if canDn
            cur += below
            lo -= 1
        else
            break
    [poc, hi, lo]
 
//#endregion
 
//#region Zones
 
type Zone
    float lo
    float hi
    string name
 
// Three passes of a moving average over SMOOTH_PCT of the profile's height.
smooth(float[] v) =>
    int n = v.size()
    int h = math.max(1, int(math.round(n * SMOOTH_PCT / 200)))
    float[] s = v.copy()
    float[] cs = array.new_float(n + 1, 0.)
    for pass = 1 to 3
        for i = 0 to n - 1
            cs.set(i + 1, cs.get(i) + s.get(i))
        for i = 0 to n - 1
            int lo = math.max(0, i - h)
            int hi = math.min(n - 1, i + h)
            s.set(i, (cs.get(hi + 1) - cs.get(lo)) / (hi - lo + 1))
    s
 
// The HVN / LVN zones of a session.
// POC: from the POC up and down while the smoothed volume stays at POC_ZONE_PCT of the POC's,
// inside the value area.
// VAH / VAL: stepping out from the edge (above VAH, below VAL), the first price within reach
// where the smoothed volume falls below THIN_PCT of the POC's; the zone runs on outwards while it
// stays thin, at most ZONE_MAX_PCT of the value area height. Tested on 10 Nov NQ: 25% / 10% gave
// 27881-27906 and 27566-27591 against George's hand-marked 27880-27905 and 27565-27590.
edgeZones(float[] v, int minL, int poc, int vah, int val) =>
    Zone[] out = array.new<Zone>()
    int n = v.size()
    if n >= 5
        float[] s = smooth(v)
        float thrPoc = s.get(poc) * POC_ZONE_PCT / 100
        int pLo = poc
        int pHi = poc
        while pLo > val and s.get(pLo - 1) >= thrPoc
            pLo -= 1
        while pHi < vah and s.get(pHi + 1) >= thrPoc
            pHi += 1
        out.push(Zone.new((minL + pLo) * LEVEL_SIZE, (minL + pHi + 1) * LEVEL_SIZE, 'POC'))
 
        float thrThin = s.get(poc) * THIN_PCT / 100
        int vaH = vah - val + 1
        int reach = math.max(2, int(math.round(vaH * REACH_PCT / 100)))
        int maxH = math.max(2, int(math.round(vaH * ZONE_MAX_PCT / 100)))
        for k = 0 to 1
            bool top = k == 0
            int dir = top ? 1 : -1
            int e = top ? vah : val
            // First thin price beyond the edge, within reach.
            int i = e + dir
            while i >= 0 and i < n and math.abs(i - e) <= reach and s.get(i) >= thrThin
                i += dir
            if i >= 0 and i < n and math.abs(i - e) <= reach
                // Outwards while it stays thin, up to the max height.
                int j = i
                while j + dir >= 0 and j + dir < n and s.get(j + dir) < thrThin and math.abs(j + dir - i) < maxH
                    j += dir
                int lo = math.min(i, j)
                int hi = math.max(i, j)
                out.push(Zone.new((minL + lo) * LEVEL_SIZE, (minL + hi + 1) * LEVEL_SIZE, top ? 'VAH' : 'VAL'))
    out
 
//#endregion
 
//#region Session
 
type Session
    map<int, float> vol
    map<int, float> net
    int minL
    int maxL
    int startTime
    int endTime
    bool complete = true
    bool closed = false
    int candles = 0
    float chartVolume = 0.
    float footprintVolume = 0.
    float deltaVolume = 0.
    array<Zone> zones
    bool drawn = false
    array<polyline> polys
    array<line> lines
    array<label> labels
    array<box> boxes
 
newSession(int startTime, bool complete) =>
    Session.new(vol = map.new<int, float>(), net = map.new<int, float>(), startTime = startTime, endTime = startTime, complete = complete, zones = array.new<Zone>(), polys = array.new<polyline>(), lines = array.new<line>(), labels = array.new<label>(), boxes = array.new<box>())
 
method addVolume(Session this, int level, float v) =>
    this.vol.put(level, nz(this.vol.get(level)) + v)
    this.minL := na(this.minL) ? level : math.min(this.minL, level)
    this.maxL := na(this.maxL) ? level : math.max(this.maxL, level)
 
// One candle in: its footprint rows as they traded. A candle with no footprint is spread over its
// range (volume only). A made-up footprint (all on one side, every row the same, as older 1M
// candles used to come) adds its volume but not its delta.
method storeCandle(Session this, footprint f) =>
    this.candles += 1
    float v = nz(volume)
    this.chartVolume += v
    array<volume_row> rows = na(f) ? array.new<volume_row>() : f.rows()
    if rows.size() == 0
        int lo = levelOf(low)
        int hi = levelOf(high)
        for level = lo to hi
            this.addVolume(level, v / (hi - lo + 1))
    else
        float buyTotal = 0.
        float sellTotal = 0.
        float rowMax = 0.
        float rowMin = 1e12
        for r in rows
            buyTotal += nz(r.buy_volume())
            sellTotal += nz(r.sell_volume())
            rowMax := math.max(rowMax, nz(r.total_volume()))
            rowMin := math.min(rowMin, nz(r.total_volume()))
        bool madeUp = (buyTotal == 0 or sellTotal == 0) and (rows.size() == 1 or rowMax - rowMin <= 0.0001)
        for r in rows
            float rv = nz(r.total_volume())
            float rd = nz(r.buy_volume()) - nz(r.sell_volume())
            this.footprintVolume += rv
            if not madeUp
                this.deltaVolume += rv
            int lo = levelOf(r.down_price())
            int hi = math.max(lo, levelOf(r.up_price()) - 1)
            int k = hi - lo + 1
            for level = lo to hi
                this.addVolume(level, rv / k)
                if not madeUp
                    this.net.put(level, nz(this.net.get(level)) + rd / k)
 
method volumes(Session this) =>
    float[] v = array.new_float(this.maxL - this.minL + 1, 0.)
    for [lvl, x] in this.vol
        v.set(lvl - this.minL, x)
    v
 
method deltas(Session this) =>
    float[] d = array.new_float(this.maxL - this.minL + 1, 0.)
    for [lvl, x] in this.net
        if lvl >= this.minL and lvl <= this.maxL
            d.set(lvl - this.minL, x)
    d
 
method coverage(Session this) =>
    this.chartVolume > 0 ? this.footprintVolume / this.chartVolume : 0.
 
method deltaReal(Session this) =>
    this.chartVolume > 0 and this.deltaVolume >= this.chartVolume * DELTA_COVERAGE
 
method clear(Session this) =>
    this.drawn := false
    for p in this.polys
        p.delete()
    for l in this.lines
        l.delete()
    for l in this.labels
        l.delete()
    for b in this.boxes
        b.delete()
    this.polys.clear()
    this.lines.clear()
    this.labels.clear()
    this.boxes.clear()
 
// The session has ended: its HVN / LVN zones are found once.
method finish(Session this) =>
    this.closed := true
    // Past days draw no delta.
    this.net.clear()
    if this.vol.size() > 0
        float[] v = this.volumes()
        [poc, vah, val] = valueArea(v)
        this.zones := edgeZones(v, this.minL, poc, vah, val)
 
// One filled stepped outline over rows from..to, growing from baseTime: right (dir 1) for the
// volume profile, left (dir -1) for the delta profile. Two points per row, so a fine profile
// stays one shape.
stepShape(float[] w, int rowFrom, int rowTo, int baseTime, int dir, float bottom, float step, color c) =>
    points = array.new<chart.point>()
    points.push(chart.point.from_time(baseTime, bottom + rowFrom * step))
    for i = rowFrom to rowTo
        int x = baseTime + dir * int(math.round(w.get(i)))
        float b = bottom + i * step
        points.push(chart.point.from_time(x, b))
        points.push(chart.point.from_time(x, b + step))
    points.push(chart.point.from_time(baseTime, bottom + (rowTo + 1) * step))
    // Outline: the fill colour, lighter and more solid.
    color edge = OUTLINE ? color.new(color.from_gradient(30, 0, 100, color.new(c, 0), #ffffff), 30) : #00000000
    polyline.new(points, closed = true, line_color = edge, fill_color = c, xloc = xloc.bar_time)
 
// Separate bars for the rows with w > 0, hanging left from right edge baseTime, a small gap
// between rows. One polyline: between bars the outline runs back along the edge.
barShape(float[] w, int baseTime, float bottom, float step, color c) =>
    points = array.new<chart.point>()
    float g = step * 0.12
    for i = 0 to w.size() - 1
        float x = w.get(i)
        if x > 0
            int xl = baseTime - int(math.round(x))
            float b = bottom + i * step
            points.push(chart.point.from_time(baseTime, b + g))
            points.push(chart.point.from_time(xl, b + g))
            points.push(chart.point.from_time(xl, b + step - g))
            points.push(chart.point.from_time(baseTime, b + step - g))
    points.size() > 0 ? polyline.new(points, closed = true, line_color = #00000000, fill_color = c, xloc = xloc.bar_time) : na
 
// Today's delta profile: net delta per row of ROW_TICKS, rows on a fixed price grid so they do not
// shift as the day grows. Bars hang left from right edge baseTime, the largest width ms wide.
method drawDelta(Session this, int baseTime, float width) =>
    float[] d = this.deltas()
    int k = math.max(1, int(math.round(ROW_TICKS / TICKS)))
    int startL = int(math.floor(this.minL / k)) * k
    int rows = int((this.maxL - startL) / k) + 1
    while rows > 2400
        k *= 2
        startL := int(math.floor(this.minL / k)) * k
        rows := int((this.maxL - startL) / k) + 1
    float[] rd = array.new_float(rows, 0.)
    for i = 0 to d.size() - 1
        int r = int((this.minL + i - startL) / k)
        rd.set(r, rd.get(r) + d.get(i))
    float maxD = math.max(rd.max(), -rd.min())
    if maxD > 0
        float[] buyStrong = array.new_float(rows, 0.)
        float[] buyWeak = array.new_float(rows, 0.)
        float[] sellStrong = array.new_float(rows, 0.)
        float[] sellWeak = array.new_float(rows, 0.)
        for i = 0 to rows - 1
            float x = rd.get(i)
            float wx = math.abs(x) / maxD * width
            bool strong = math.abs(x) >= maxD * STRONG_PCT / 100
            if x > 0
                (strong ? buyStrong : buyWeak).set(i, wx)
            else if x < 0
                (strong ? sellStrong : sellWeak).set(i, wx)
        float bottom = startL * LEVEL_SIZE
        float step = k * LEVEL_SIZE
        // The lighter shade: the colour taken halfway to white.
        color buyLight = color.from_gradient(50, 0, 100, BUY_COLOR, #ffffff)
        color sellLight = color.from_gradient(50, 0, 100, SELL_COLOR, #ffffff)
        for p in array.from(barShape(buyStrong, baseTime, bottom, step, BUY_COLOR), barShape(buyWeak, baseTime, bottom, step, buyLight), barShape(sellStrong, baseTime, bottom, step, SELL_COLOR), barShape(sellWeak, baseTime, bottom, step, sellLight))
            if not na(p)
                this.polys.push(p)
 
method levelLine(Session this, int t0, int t1, float price, color c, string style, int w, string name, string mark, string tip, bool withLabel) =>
    this.lines.push(line.new(t0, price, t1, price, xloc = xloc.bar_time, color = c, width = w, style = style))
    if withLabel
        string t = name + (SHOW_PRICES ? ' ' + str.tostring(price, format.mintick) : '') + mark
        this.labels.push(label.new(t1, price, t, xloc = xloc.bar_time, color = #00000000, textcolor = c, style = label.style_label_left, size = size.small, tooltip = tip))
 
// age 0 = today (forming, with the O levels), 1+ = past days (levels inside their day).
method render(Session this, int age, int nowTime, int extTime) =>
    this.clear()
    this.drawn := true
    if this.vol.size() > 0
        float[] v = this.volumes()
        int n = v.size()
        [poc, vah, val] = valueArea(v)
        int t0 = this.startTime
        // In its session the volume grows right from the session start.
        bool onRight = age == 0 and TODAY_PLACE == 'Right of price'
        int barMs = TF_SECONDS * 1000
        int base = onRight ? nowTime + (RIGHT_OFFSET + RIGHT_WIDTH) * barMs : t0
        int volDir = onRight ? -1 : 1
 
        // Profile: prices grouped into rows only if there are more than MAX_ROWS of them, and then
        // the same number in every row (2, 3, ...), so no row looks wider for holding an extra price.
        int k = int(math.ceil(n / MAX_ROWS))
        int rows = int(math.ceil(n / k))
        float step = k * LEVEL_SIZE
        float bottom = this.minL * LEVEL_SIZE
        float[] rv = array.new_float(rows, 0.)
        for i = 0 to n - 1
            int r = int(i / k)
            rv.set(r, rv.get(r) + v.get(i))
        int vaLo = int(val / k)
        int vaHi = int(vah / k)
        float maxV = rv.max()
        float width = onRight ? RIGHT_WIDTH * barMs : SESSION_MS * WIDTH_PCT / 100
        float[] w = array.new_float(rows, 0.)
        for i = 0 to rows - 1
            w.set(i, maxV > 0 ? rv.get(i) / maxV * width : 0.)
        if vaLo > 0
            this.polys.push(stepShape(w, 0, vaLo - 1, base, volDir, bottom, step, PROFILE_COLOR))
        this.polys.push(stepShape(w, vaLo, vaHi, base, volDir, bottom, step, VA_COLOR))
        if vaHi < rows - 1
            this.polys.push(stepShape(w, vaHi + 1, rows - 1, base, volDir, bottom, step, PROFILE_COLOR))
 
        // Today's live delta profile, right of price: its right edge LIVE_OFFSET + LIVE_DELTA_BARS
        // bars after the latest candle, the bars hanging left from it.
        if SHOW_DELTA and age == 0
            this.drawDelta(nowTime + (LIVE_OFFSET + LIVE_DELTA_BARS) * barMs, LIVE_DELTA_BARS * barMs)
 
        // HVN / LVN zones inside the day; today's found afresh on every redraw.
        if SHOW_ZONES
            Zone[] zs = age == 0 ? edgeZones(v, this.minL, poc, vah, val) : this.zones
            int zEnd = age == 0 ? nowTime : this.endTime
            for z in zs
                this.boxes.push(box.new(t0, z.hi, zEnd, z.lo, xloc = xloc.bar_time, bgcolor = ZONE_COLOR, border_color = #00000000, border_width = 0))
 
        // Levels.
        float pocPrice = levelPrice(this.minL + poc)
        float vahPrice = (this.minL + vah + 1) * LEVEL_SIZE
        float valPrice = (this.minL + val) * LEVEL_SIZE
        float cov = this.coverage()
        string mark = cov < COVERAGE_OK ? ' ~' : ''
        string tip = 'CME day from ' + str.format_time(t0, 'EEE d MMM HH:mm', TZ) + ' New York: ' + str.tostring(this.candles) + ' candles, footprint covers ' + str.tostring(math.min(cov, 1) * 100, '#.#') + '% of the volume' + (cov < COVERAGE_OK ? ' (the rest spread over candle ranges, so treat the levels as approximate)' : '') + '.' + (age == 0 ? ' Delta ' + (this.deltaReal() ? 'from real bid / ask data.' : 'not shown in colour: the footprint split looks made up.') : '')
        if age == 0
            if TODAY_LEVELS
                string oTip = 'O = overnight (ETH): today' + "'" + 's full CME day so far, from the 18:00 New York open (Asia) to now, RTH included. ' + tip
                if SHOW_POC
                    this.levelLine(t0, nowTime, pocPrice, POC_COLOR, line.style_solid, LINE_WIDTH, 'O POC', mark, oTip, true)
                if SHOW_VA
                    this.levelLine(t0, nowTime, vahPrice, VAH_COLOR, line.style_solid, LINE_WIDTH, 'O VAH', mark, oTip, true)
                    this.levelLine(t0, nowTime, valPrice, VAL_COLOR, line.style_solid, LINE_WIDTH, 'O VAL', mark, oTip, true)
        else
            if SHOW_POC
                this.levelLine(t0, this.endTime, pocPrice, POC_COLOR, line.style_solid, 1, 'POC', mark, tip, PAST_LABELS)
            if SHOW_VA
                this.levelLine(t0, this.endTime, vahPrice, VAH_COLOR, line.style_solid, 1, 'VAH', mark, tip, PAST_LABELS)
                this.levelLine(t0, this.endTime, valPrice, VAL_COLOR, line.style_solid, 1, 'VAL', mark, tip, PAST_LABELS)
 
// The previous session, already drawn: only its right ends move with the latest candle.
method moveEnds(Session this, int extTime) =>
    for l in this.lines
        l.set_x2(extTime)
    for l in this.labels
        l.set_x(extTime)
    for b in this.boxes
        b.set_right(extTime)
 
// A session has ended: store it, and redraw every past one (their ages just changed).
retire(Session s, array<Session> done) =>
    s.finish()
    s.clear()
    for p in done
        p.clear()
    done.push(s)
    while done.size() > math.max(1, KEEP - 1)
        done.shift().clear()
 
//#endregion
 
//#region Main
 
var Session cur = na
var array<Session> done = array.new<Session>()
 
if not OFF
    if rthStart
        if not na(cur)
            retire(cur, done)
        // The chart's first candle may be mid-session: that session is missing volume.
        cur := newSession(time, bar_index > 0)
    if inRth and not na(cur)
        cur.storeCandle(fp)
        cur.endTime := time + TF_SECONDS * 1000
    else if not inRth and not na(cur)
        retire(cur, done)
        cur := na
 
    if barstate.islast
        int nowTime = time
        int k = done.size()
        for [i, s] in done
            int age = k - i
            if not s.complete
                s.clear()
            else if not s.drawn
                s.render(age, nowTime, nowTime)
        if not na(cur)
            if SHOW_TODAY and cur.complete
                cur.render(0, nowTime, nowTime)
            else
                cur.clear()
 
//#endregion

← 返回列表