NQ Weekly Globex Volume Profile

shane10kelly · study · 177 行 · 点赞 23 · TradingView 原页

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

Pine Script

//@version=6
indicator("NQ Weekly Globex Volume Profile — Price Anchored", overlay=true, max_boxes_count=500, max_lines_count=10, max_labels_count=10)
 
// ── Settings ────────────────────────────────────────────────
string TZ = "America/New_York"
 
rowSize       = input.float(5.0, "Price per row (NQ points)", minval=0.25, step=0.25)
valueAreaPct  = input.float(70.0, "Value area (%)", minval=50, maxval=99, step=1)
rightOffset   = input.int(5, "Gap from latest bar (bars)", minval=1, maxval=100)
maxWidth      = input.int(35, "Maximum profile width (bars)", minval=5, maxval=100)
showPOC       = input.bool(true, "Show POC")
showVA        = input.bool(true, "Show VAH / VAL")
showLabels    = input.bool(true, "Show price labels")
showOutsideVA = input.bool(true, "Show volume outside value area")
 
profileColor  = input.color(color.new(color.blue, 65), "Profile color")
valueAreaColor = input.color(color.new(color.teal, 45), "Value area color")
pocColor      = input.color(color.orange, "POC color")
vaColor       = input.color(color.aqua, "VAH / VAL color")
 
// ── Storage ─────────────────────────────────────────────────
var map<int, float> profile = map.new<int, float>()
var array<box> drawnBoxes = array.new<box>()
var array<line> drawnLines = array.new<line>()
var array<label> drawnLabels = array.new<label>()
 
// ── Sunday 18:00 New York Globex reset ──────────────────────
int nyDay = dayofweek(time, TZ)
int nyHour = hour(time, TZ)
 
bool sundayGlobex = nyDay == dayofweek.sunday and nyHour >= 18
bool newGlobexWeek = sundayGlobex and not sundayGlobex[1]
 
if newGlobexWeek
    map.clear(profile)
 
// ── Accumulate approximate volume-at-price ──────────────────
if not na(volume) and not na(high) and not na(low)
    int firstRow = int(math.floor(low / rowSize))
    int lastRow = int(math.floor(high / rowSize))
    int rowCount = lastRow - firstRow + 1
 
    if rowCount > 0 and rowCount <= 500
        float volumePerRow = volume / rowCount
 
        for r = firstRow to lastRow
            float previousVolume = map.contains(profile, r) ? map.get(profile, r) : 0.0
            map.put(profile, r, previousVolume + volumePerRow)
 
// ── Redraw anchored profile on the latest bar ────────────────
if barstate.islast
    while array.size(drawnBoxes) > 0
        box.delete(array.pop(drawnBoxes))
    while array.size(drawnLines) > 0
        line.delete(array.pop(drawnLines))
    while array.size(drawnLabels) > 0
        label.delete(array.pop(drawnLabels))
 
    array<int> keys = map.keys(profile)
    array.sort(keys)
 
    int n = array.size(keys)
 
    if n > 0
        float totalVolume = 0.0
        float maxVolume = 0.0
        int pocIndex = 0
 
        for i = 0 to n - 1
            int key = array.get(keys, i)
            float v = map.get(profile, key)
            totalVolume += v
 
            if v > maxVolume
                maxVolume := v
                pocIndex := i
 
        // Build contiguous value area around POC.
        float targetVolume = totalVolume * valueAreaPct / 100.0
        int lowIndex = pocIndex
        int highIndex = pocIndex
        float accumulated = map.get(profile, array.get(keys, pocIndex))
 
        while accumulated < targetVolume and (lowIndex > 0 or highIndex < n - 1)
            float belowVolume = lowIndex > 0 ? map.get(profile, array.get(keys, lowIndex - 1)) : -1.0
            float aboveVolume = highIndex < n - 1 ? map.get(profile, array.get(keys, highIndex + 1)) : -1.0
 
            if highIndex < n - 1 and (lowIndex == 0 or aboveVolume >= belowVolume)
                highIndex += 1
                accumulated += map.get(profile, array.get(keys, highIndex))
            else if lowIndex > 0
                lowIndex -= 1
                accumulated += map.get(profile, array.get(keys, lowIndex))
            else
                break
 
        int pocRow = array.get(keys, pocIndex)
        int vahRow = array.get(keys, highIndex)
        int valRow = array.get(keys, lowIndex)
 
        float pocPrice = (pocRow + 0.5) * rowSize
        float vahPrice = (vahRow + 1.0) * rowSize
        float valPrice = valRow * rowSize
 
        // Use timestamps for horizontal anchors and actual prices vertically.
        int barMs = int(timeframe.in_seconds() * 1000)
        int rightTime = time + barMs * (rightOffset + maxWidth)
        int lineLeftTime = time - barMs * 5
 
        for i = 0 to n - 1
            int key = array.get(keys, i)
            float v = map.get(profile, key)
            int widthBars = maxVolume > 0 ? math.max(1, int(math.round(v / maxVolume * maxWidth))) : 1
            bool inValueArea = i >= lowIndex and i <= highIndex
 
            if inValueArea or showOutsideVA
                int leftTime = rightTime - barMs * widthBars
                color rowColor = inValueArea ? valueAreaColor : profileColor
 
                box b = box.new(
                     left=leftTime,
                     top=(key + 1) * rowSize,
                     right=rightTime,
                     bottom=key * rowSize,
                     xloc=xloc.bar_time,
                     border_color=color.new(rowColor, 100),
                     bgcolor=rowColor)
 
                array.push(drawnBoxes, b)
 
        if showPOC
            line lp = line.new(
                 x1=lineLeftTime, y1=pocPrice,
                 x2=rightTime, y2=pocPrice,
                 xloc=xloc.bar_time, color=pocColor, width=2)
            array.push(drawnLines, lp)
 
        if showVA
            line lh = line.new(
                 x1=lineLeftTime, y1=vahPrice,
                 x2=rightTime, y2=vahPrice,
                 xloc=xloc.bar_time, color=vaColor, width=1,
                 style=line.style_dashed)
 
            line ll = line.new(
                 x1=lineLeftTime, y1=valPrice,
                 x2=rightTime, y2=valPrice,
                 xloc=xloc.bar_time, color=vaColor, width=1,
                 style=line.style_dashed)
 
            array.push(drawnLines, lh)
            array.push(drawnLines, ll)
 
        if showLabels
            if showPOC
                label pl = label.new(
                     x=rightTime, y=pocPrice,
                     text="W-POC " + str.tostring(pocPrice, format.mintick),
                     xloc=xloc.bar_time, style=label.style_label_left,
                     color=pocColor, textcolor=color.black, size=size.small)
                array.push(drawnLabels, pl)
 
            if showVA
                label hl = label.new(
                     x=rightTime, y=vahPrice,
                     text="W-VAH " + str.tostring(vahPrice, format.mintick),
                     xloc=xloc.bar_time, style=label.style_label_left,
                     color=vaColor, textcolor=color.black, size=size.small)
 
                label vl = label.new(
                     x=rightTime, y=valPrice,
                     text="W-VAL " + str.tostring(valPrice, format.mintick),
                     xloc=xloc.bar_time, style=label.style_label_left,
                     color=vaColor, textcolor=color.black, size=size.small)
 
                array.push(drawnLabels, hl)
                array.push(drawnLabels, vl)

← 返回列表