Unbroke Volume Flow Profile

bryanhulinsky23 · study · 516 行 · 点赞 19 · TradingView 原页

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

Pine Script

// This Pine Script code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Unbroke - free & open-source. Not financial advice. For educational purposes only.
// volume flow profile 1.0.6
//@version=6
indicator("Unbroke Volume Flow Profile", shorttitle = "UB VolProf", overlay = true, max_boxes_count = 120, max_lines_count = 20, max_labels_count = 10, max_bars_back = 500)
// Session or visible-range volume profile with POC, VAH and VAL.
// APPROXIMATION: Pine has no tick volume-at-price. Each bar's volume is split evenly across the profile rows its
// high-low range covers. "Lower timeframe" mode does the same with 1-minute (or chosen) bars from
// request.security_lower_tf, which is finer but still an approximation; it falls back to chart bars where intrabar
// history is missing. Proof panel: first touches of the PRIOR session's POC / VAH / VAL vs random levels inside the
// prior session's range. Python port: tools/volume_flow_profile_mirror.py (chart-bar mode). Original code.
 
//==============================================================================
// INPUTS
//==============================================================================
G_V = "Profile"
vpMode = input.string("Session", "Profile range", options = ["Session", "Visible range"], group = G_V, tooltip = "Session = the current trading day (as the 1D bar). Visible range = the bars on screen (up to the last 5000). The stats always use whole sessions.", display = display.none)
vpSrc  = input.string("Chart bars", "Volume source", options = ["Chart bars", "Lower timeframe"], group = G_V, tooltip = "Both split each bar's volume evenly over its high-low range. Lower timeframe uses smaller bars, so the shape is finer.", display = display.none)
vpLtf  = input.timeframe("1", "Lower timeframe", group = G_V, display = display.none)
nRows  = input.int(50, "Rows", minval = 10, maxval = 100, group = G_V, display = display.none)
vaPct  = input.int(70, "Value area %", minval = 10, maxval = 95, group = G_V, display = display.none)
wBars  = input.int(40, "Profile width (bars)", minval = 5, maxval = 300, group = G_V, display = display.none)
showPr = input.bool(true, "Prior session POC / VAH / VAL lines", group = G_V, display = display.none)
prNear = input.float(4.0, "Hide prior levels farther than (x ATR 14)", minval = 0.5, maxval = 50.0, step = 0.5, group = G_V, tooltip = "Display only. Prior levels this far from price are not drawn, so far-off lines do not squeeze the chart. The stats still use every level.", display = display.none)
G_PRF = "Proof panel"
showPanel = input.bool(true, "Show Proof panel", inline = "pp", group = G_PRF, tooltip = "How often each signal ran 1 ATR(14) its way before 1 ATR against, compared with random bars scored the same way.", display = display.none)
panelPos  = input.string("Top Right", "", options = ["Top Right", "Bottom Left", "Middle Left", "Top Left", "Bottom Right", "Middle Right"], inline = "pp", group = G_PRF, display = display.none)
hzBars    = input.int(24, "Horizon (bars)", minval = 2, maxval = 500, group = G_PRF, tooltip = "A signal is a hit when price runs 1 ATR(14) its way before 1 ATR against within this many bars, from the signal bar close. Both on one bar = against.", display = display.none)
minN      = input.int(20, "Minimum cases for a verdict", minval = 5, maxval = 500, group = G_PRF, display = display.none)
pRand     = input.float(0.05, "Random entry rate (per bar)", minval = 0.005, maxval = 0.5, step = 0.005, group = G_PRF, tooltip = "Share of bars that get a random entry, 50/50 long or short, seeded so it is the same every time.", display = display.none)
seed      = input.int(7, "Random seed", group = G_PRF, display = display.none)
G_DSP = "Style"
ubThemeMode = input.string("Auto", "Theme", options = ["Auto", "Dark", "Light"], group = G_DSP, tooltip = "Auto follows the chart background.", display = display.none)
dashSize = input.string("Small", "Panel text size", options = ["Tiny", "Small", "Normal"], group = G_DSP, display = display.none)
xpOn     = input.bool(false, "Export mode (port check)", group = G_DSP, tooltip = "Adds 'VFP XP' columns to the Data Window and Export chart data, for tools/volume_flow_profile_mirror.py.", display = display.none)
 
//==============================================================================
// UNBROKE STYLE KIT for Pine v6 (style kit 1.3, pasted unchanged below the header). Original code. ASCII only.
//==============================================================================
// ---- 1. Palette (fixed brand constants) -------------------------------------
UB_GREEN_DEEP = #1B7A5A   // brand green (light-theme frame, deep fills)
UB_GREEN      = #3FBF8F   // brand green bright (dark-theme frame, bull)
UB_INK        = #141210   // dark background / text on light
UB_PAPER      = #F6F3EC   // light background / text on dark
UB_BULL       = #3FBF8F   // up / safe / reacted
UB_BEAR       = #E5534B   // down / danger / broke
UB_NEUTRAL    = #8C8A85   // mid lines, neutral state, muted marks
UB_AMBER      = #F2A93B   // caution, opening range
UB_SKY        = #4C9AFF   // info levels (previous day), entries
UB_VIOLET     = #8B5CF6   // secondary info (London, previous week)
UB_PINK       = #E0559A   // Asia session
UB_CYAN       = #14B8C4   // overnight session
 
// ---- 2. Theme detection -----------------------------------------------------
ub_lum(color c) =>
    0.299 * color.r(c) + 0.587 * color.g(c) + 0.114 * color.b(c)
ub_isLight() =>
    ub_lum(chart.bg_color) > 128 or ub_lum(chart.fg_color) < 100
bool UB_LIGHT  = ubThemeMode == "Light" or (ubThemeMode == "Auto" and ub_isLight())
color T_BG     = color.new(UB_LIGHT ? UB_PAPER : ubThemeMode == "Auto" ? chart.bg_color : UB_INK, 0)
color T_ROW    = color.new(UB_LIGHT ? #FFFFFF : #1E1C19, 0)
color T_HEAD   = color.new(UB_LIGHT ? #E3F1EA : #0F2A20, 0)
color T_TEXT   = UB_LIGHT ? UB_INK : UB_PAPER
color T_MUTED  = UB_LIGHT ? #5C574F : #A8A39A
color T_FRAME  = color.new(UB_LIGHT ? UB_GREEN_DEEP : UB_GREEN, 0)
color T_BORDER = color.new(UB_LIGHT ? #E2DCCF : #2A2723, 0)
color T_TRACK  = color.new(UB_LIGHT ? #E2DCCF : #2E2B27, 0)
 
// Readable text on any fill color.
ub_txtOn(color c) =>
    ub_lum(c) > 150 ? UB_INK : #FFFFFF
 
// ---- 3. Sizes and positions -------------------------------------------------
ub_size(string s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Large" ? size.large : size.normal
ub_sizeUp(string s) =>
    s == "Tiny" ? size.small : s == "Small" ? size.normal : s == "Large" ? size.huge : size.large
ub_pos(string s) =>
    s == "Top Right" ? position.top_right : s == "Bottom Right" ? position.bottom_right : s == "Top Left" ? position.top_left : s == "Middle Left" ? position.middle_left : s == "Middle Right" ? position.middle_right : s == "Bottom Center" ? position.bottom_center : s == "Top Center" ? position.top_center : position.bottom_left
 
// ---- 4. Gradient colors -----------------------------------------------------
// Strength color: bear at lo, neutral at the midpoint, bull at hi.
ub_strength(float v, float lo, float hi) =>
    float mid = (lo + hi) / 2.0
    v >= mid ? color.from_gradient(v, mid, hi, UB_NEUTRAL, UB_BULL) : color.from_gradient(v, lo, mid, UB_BEAR, UB_NEUTRAL)
// Room color: 0 = red, 50 = amber, 100 = green (risk meters).
ub_room(float pct) =>
    pct >= 50 ? color.from_gradient(pct, 50, 100, UB_AMBER, UB_BULL) : color.from_gradient(pct, 0, 50, UB_BEAR, UB_AMBER)
 
// ---- 5. Glow for drawn lines (plots: see DESIGN_SYSTEM.md, plot() is global-only) --
// g = a var array<line> owned by the caller (created empty). Two halo lines are
// created once, then moved. Call on barstate.islast. x1 = start bar, y = price.
ub_glow(array<line> g, int x1, float y, color c, int baseW, bool on) =>
    if array.size(g) == 0
        array.push(g, line.new(na, na, na, na, xloc = xloc.bar_index, extend = extend.right, color = color.new(c, 88), width = baseW + 6))
        array.push(g, line.new(na, na, na, na, xloc = xloc.bar_index, extend = extend.right, color = color.new(c, 72), width = baseW + 3))
    bool ok = on and not na(y) and not na(x1)
    for i = 0 to 1
        line gl = array.get(g, i)
        line.set_x1(gl, ok ? x1 : na)
        line.set_x2(gl, ok ? x1 + 1 : na)
        line.set_y1(gl, ok ? y : na)
        line.set_y2(gl, ok ? y : na)
        line.set_color(gl, color.new(c, i == 0 ? 88 : 72))
 
// ---- 6. Dashboard builder ---------------------------------------------------
// Header row: "UNBROKE" | title (merged) | subtitle. ncols >= 2.
ub_header(table t, int ncols, string title, string sub, hsz) =>
    table.cell(t, 0, 0, "UNBROKE", text_color = T_FRAME, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_left, text_formatting = text.format_bold)
    if ncols == 2
        table.cell(t, 1, 0, title, text_color = T_TEXT, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_right, text_formatting = text.format_bold, tooltip = sub)
    else
        table.cell(t, 1, 0, title, text_color = T_TEXT, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_left, text_formatting = text.format_bold)
        table.cell(t, ncols - 1, 0, sub, text_color = T_MUTED, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_right)
        if ncols > 3
            table.merge_cells(t, 1, 0, ncols - 2, 0)
 
// Footer note across all columns (tiny, muted).
ub_footer(table t, int ncols, int row, string txt) =>
    table.cell(t, 0, row, txt, text_color = T_MUTED, bgcolor = T_BG, text_size = size.tiny, text_halign = text.align_left)
    if ncols > 1
        table.merge_cells(t, 0, row, ncols - 1, row)
 
// Segmented progress bar in table cells c0 .. c0+segs-1 of one row.
// Each lit segment takes the room color of its own position (red -> amber -> green).
ub_bar(table t, int c0, int row, int segs, float pct, string tip) =>
    for i = 0 to segs - 1
        float mark = (i + 0.5) * 100.0 / segs
        bool lit = not na(pct) and pct >= mark
        table.cell(t, c0 + i, row, "", bgcolor = lit ? ub_room(mark) : T_TRACK, width = 1.4, height = 1.6, tooltip = tip)
 
// ASCII meter, e.g. "######...." for 60% with n = 10.
ub_meter(float pct, int n) =>
    int k = na(pct) ? 0 : int(math.round(math.max(0.0, math.min(100.0, pct)) / 100.0 * n))
    str.repeat("#", k) + str.repeat(".", n - k)
 
// ---- 7. PROOF panel (signature element of every Unbroke indicator) ----------
// Percent helper: a of b, as 0-100 float, na when b = 0.
ub_pct(int a, int b) =>
    b > 0 ? a * 100.0 / b : na
// Verdict vs random: two-sample 95% margin (normal approximation).
// Returns [text, color]. Below minN samples on either side -> "Not enough data".
ub_verdict(float rate, int n, float base, int nBase, int minN) =>
    string txt = "Not enough data"
    color col = UB_LIGHT ? color.from_gradient(45, 0, 100, T_MUTED, UB_INK) : T_MUTED
    if n >= minN and nBase >= minN and not na(rate) and not na(base)
        float p1 = rate / 100.0
        float p0 = base / 100.0
        float m = 196.0 * math.sqrt(p1 * (1 - p1) / n + p0 * (1 - p0) / nBase)
        float edge = rate - base
        if edge > m
            txt := "Beats random"
            col := UB_LIGHT ? color.from_gradient(45, 0, 100, UB_BULL, UB_INK) : UB_BULL
        else if edge < -m
            txt := "Worse than random"
            col := UB_LIGHT ? color.from_gradient(35, 0, 100, UB_BEAR, UB_INK) : UB_BEAR
        else
            txt := "Inside noise"
            col := UB_LIGHT ? color.from_gradient(55, 0, 100, UB_AMBER, UB_INK) : UB_AMBER
    [txt, col]
 
// Proof column headers (5 columns): Signal | n | Hit % | vs random | Verdict.
ub_proofHead(table t, int row, string c0, tsz) =>
    table.cell(t, 0, row, c0, text_color = T_MUTED, bgcolor = T_BG, text_size = tsz, text_halign = text.align_left)
    table.cell(t, 1, row, "n", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
    table.cell(t, 2, row, "Hit %", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
    table.cell(t, 3, row, "vs random", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
    table.cell(t, 4, row, "Verdict", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
 
// One proof row. hits/n = this signal, baseRate/nBase = the random baseline.
ub_proofRow(table t, int row, string name, color c, int hits, int n, float baseRate, int nBase, int minN, tsz) =>
    float rate = ub_pct(hits, n)
    [vTxt, vCol] = ub_verdict(rate, n, baseRate, nBase, minN)
    float edge = na(rate) or na(baseRate) ? na : rate - baseRate
    string eTxt = na(edge) ? "-" : (edge >= 0 ? "+" : "") + str.tostring(edge, "#") + " pts"
    table.cell(t, 0, row, name, text_color = T_TEXT, bgcolor = color.from_gradient(UB_LIGHT ? 84 : 55, 0, 100, color.new(c, 0), T_ROW), text_size = tsz, text_halign = text.align_left)
    table.cell(t, 1, row, str.tostring(n), text_color = n >= minN ? T_TEXT : T_MUTED, bgcolor = T_ROW, text_size = tsz)
    color hitBg = na(rate) ? T_ROW : UB_LIGHT ? color.from_gradient(45, 0, 100, ub_strength(rate, 0, 100), T_ROW) : ub_strength(rate, 0, 100)
    table.cell(t, 2, row, na(rate) ? "-" : str.tostring(rate, "#") + "%", text_color = na(rate) ? T_MUTED : ub_txtOn(hitBg), bgcolor = hitBg, text_size = tsz, text_formatting = text.format_bold, tooltip = "Hit rate. Cell color: red = low, grey = 50%, green = high.")
    table.cell(t, 3, row, eTxt, text_color = na(edge) ? T_MUTED : edge >= 0 ? UB_BULL : UB_BEAR, bgcolor = T_ROW, text_size = tsz)
    table.cell(t, 4, row, vTxt, text_color = vCol, bgcolor = T_ROW, text_size = tsz, text_formatting = text.format_bold, tooltip = "95% margin test vs the random baseline. Needs at least " + str.tostring(minN) + " samples on both sides.")
 
//==============================================================================
// CALCULATIONS
//==============================================================================
float A = ta.atr(14)
int WARM = 300
bool ok = bar_index >= WARM and not na(A)
color C_UP = UB_LIGHT ? #0FA86E : #2EF0A6
color C_DN = UB_LIGHT ? #E0352B : #FF5C52
bool useLtf = vpSrc == "Lower timeframe"
[lH, lL, lV] = request.security_lower_tf(syminfo.tickerid, vpLtf, [high, low, volume], ignore_invalid_timeframe = true)
bool newS = timeframe.change("1D")
 
// Profile of (h, l, v) arrays: [poc, vah, val, lo, hi, row volumes]
f_prof(array<float> h, array<float> l, array<float> v, int nr, int va) =>
    array<float> vol = array.new<float>(nr, 0.0)
    float poc = na
    float vah = na
    float val = na
    float lo = h.size() > 0 ? l.min() : na
    float hi = h.size() > 0 ? h.max() : na
    if h.size() > 0 and hi > lo and v.sum() > 0
        float s = (hi - lo) / nr
        for j = 0 to h.size() - 1
            int i0 = math.min(nr - 1, int((l.get(j) - lo) / s))
            int i1 = math.min(nr - 1, int((h.get(j) - lo) / s))
            float w = v.get(j) / (i1 - i0 + 1)
            for k = i0 to i1
                vol.set(k, vol.get(k) + w)
        int p = vol.indexof(vol.max())
        int up = p
        int dn = p
        float tot = vol.get(p)
        float tgt = vol.sum() * va / 100.0
        while tot < tgt and (up < nr - 1 or dn > 0)
            float vu = up < nr - 1 ? vol.get(up + 1) : -1.0
            float vd = dn > 0 ? vol.get(dn - 1) : -1.0
            if vu >= vd
                up += 1
                tot += vol.get(up)
            else
                dn -= 1
                tot += vol.get(dn)
        poc := lo + (p + 0.5) * s
        vah := lo + (up + 1) * s
        val := lo + dn * s
    [poc, vah, val, lo, hi, vol]
 
// Session store and a rolling store for the visible range
var array<float> sH = array.new<float>()
var array<float> sL = array.new<float>()
var array<float> sV = array.new<float>()
var int sX = 0
var array<float> wH = array.new<float>()
var array<float> wL = array.new<float>()
var array<float> wV = array.new<float>()
var array<int>   wT = array.new<int>()
var array<int>   wX = array.new<int>()
var float pPoc = na
var float pVah = na
var float pVal = na
var array<float> lvX = array.new<float>()
var array<int>   lvK = array.new<int>()
 
//==============================================================================
// PROOF ENGINE: pending entries scored from the next bar on (1 ATR its way before 1 ATR against)
//==============================================================================
var array<int>   pI = array.new<int>()
var array<int>   pD = array.new<int>()
var array<float> pC = array.new<float>()
var array<float> pA = array.new<float>()
var array<int>   pK = array.new<int>()
var array<int> cN = array.new<int>(6, 0)
var array<int> cH = array.new<int>(6, 0)
array<int> xr = array.new<int>(2, 0)   // resolved, hits on this bar (all kinds)
 
f_hash(float k) =>
    int m = int(math.floor(time / 60000.0)) % 100003
    float x = math.sin(m * 12.9898 + seed * 4.1414 + k) * 43758.5453
    x - math.floor(x)
 
if pI.size() > 0
    for j = pI.size() - 1 to 0
        int d = pD.get(j)
        float cE = pC.get(j)
        float aE = pA.get(j)
        bool stp = d > 0 ? low <= cE - aE : high >= cE + aE
        bool run = d > 0 ? high >= cE + aE : low <= cE - aE
        int dec = stp ? 0 : run ? 1 : bar_index - pI.get(j) >= hzBars ? 0 : -1
        if dec >= 0
            int k = pK.get(j)
            cN.set(k, cN.get(k) + 1)
            cH.set(k, cH.get(k) + dec)
            xr.set(0, xr.get(0) + 1)
            xr.set(1, xr.get(1) + dec)
            pI.remove(j)
            pD.remove(j)
            pC.remove(j)
            pA.remove(j)
            pK.remove(j)
 
f_pend(int d, int k) =>
    pI.push(bar_index)
    pD.push(d)
    pC.push(close)
    pA.push(A)
    pK.push(k)
 
 
// kinds: 0 random levels (baseline), 1 random bars (info), 2 POC, 3 VAH, 4 VAL, 5 all POC/VA
if newS
    [q0, q1, q2, q3, q4, qv] = f_prof(sH, sL, sV, nRows, vaPct)
    pPoc := q0
    pVah := q1
    pVal := q2
    lvX.clear()
    lvK.clear()
    if not na(q0)
        lvX.push(q0)
        lvK.push(2)
        lvX.push(q1)
        lvK.push(3)
        lvX.push(q2)
        lvK.push(4)
        for k = 0 to 2
            lvX.push(q3 + (q4 - q3) * f_hash(31.7 + 7.3 * k))
            lvK.push(0)
    sH.clear()
    sL.clear()
    sV.clear()
    sX := bar_index
bool ltfOk = useLtf and lH.size() > 0
if ltfOk
    for j = 0 to lH.size() - 1
        sH.push(lH.get(j))
        sL.push(lL.get(j))
        sV.push(nz(lV.get(j)))
else
    sH.push(high)
    sL.push(low)
    sV.push(nz(volume))
if vpMode == "Visible range"
    wH.push(high)
    wL.push(low)
    wV.push(nz(volume))
    wT.push(time)
    wX.push(bar_index)
    if wH.size() > 5000
        wH.shift()
        wL.shift()
        wV.shift()
        wT.shift()
        wX.shift()
 
int touch = 0
if barstate.isconfirmed and lvX.size() > 0
    for j = lvX.size() - 1 to 0
        float x = lvX.get(j)
        int d = close[1] > x and low <= x ? 1 : close[1] < x and high >= x ? -1 : 0
        if d != 0
            int k = lvK.get(j)
            if ok
                f_pend(d, k)
                if k > 0
                    f_pend(d, 5)
            if k > 0
                touch := d
            lvX.remove(j)
            lvK.remove(j)
 
int xRnd = 0
if ok and barstate.isconfirmed and f_hash(0.0) < pRand
    xRnd := f_hash(78.233) < 0.5 ? 1 : -1
    f_pend(xRnd, 1)
 
//==============================================================================
// DRAWING: profile boxes (last bar only), prior session levels as plots
//==============================================================================
f_near(float v) =>
    not na(v) and not na(A) and math.abs(v - close) <= prNear * A
// Prior levels are drawn as lines on the last bar only (no plots, so no price-axis tags and no autoscale stretch).
// Each runs from the profile start to the last bar. Name chips for all levels are drawn together further down.
// Levels farther than prNear x ATR(14) from the last close are not drawn at all.
var array<line>  prL = array.new<line>()
var array<label> chT = array.new<label>()
// Pending chips: price, name, fill, bar
array<float>  ckY = array.new<float>()
array<string> ckN = array.new<string>()
array<color>  ckC = array.new<color>()
array<int>    ckX = array.new<int>()
int MIN_LB = 300   // minimum profile lookback in bars (24/7 symbols reset at 00:00 UTC); also the floor for the width base
if barstate.islast
    while prL.size() > 0
        line.delete(prL.pop())
    if showPr
        array<float> lv = array.from(pPoc, pVah, pVal)
        array<string> ln = array.from("pPOC", "pVAH", "pVAL")
        for i = 0 to 2
            float y = lv.get(i)
            if f_near(y)
                color lc = i == 0 ? color.new(UB_AMBER, 10) : color.new(UB_SKY, 20)
                int xs = sX > 0 and bar_index - sX >= MIN_LB ? sX : math.max(0, bar_index - MIN_LB + 1)
                prL.push(line.new(xs, y, bar_index, y, color = lc, width = i == 0 ? 2 : 1))
                ckY.push(y)
                ckN.push(ln.get(i))
                ckC.push(i == 0 ? UB_AMBER : UB_SKY)
                ckX.push(xs)
 
var array<box> pb = array.new<box>()
var line lPoc = na
var line lVah = na
var line lVal = na
var float dPoc = na
if barstate.islast
    array<float> aH = sH
    array<float> aL = sL
    array<float> aV = sV
    int x0 = sX
    if vpMode == "Session" and bar_index - sX < MIN_LB and bar_index >= MIN_LB
        // Short session so far (24/7 symbols right after 00:00 UTC): show at least MIN_LB chart bars. Display only.
        aH := array.new<float>()
        aL := array.new<float>()
        aV := array.new<float>()
        for k = MIN_LB - 1 to 0
            aH.push(high[k])
            aL.push(low[k])
            aV.push(nz(volume[k]))
        x0 := bar_index - MIN_LB + 1
    if vpMode == "Visible range" and wH.size() > 0
        aH := array.new<float>()
        aL := array.new<float>()
        aV := array.new<float>()
        x0 := na
        for j = 0 to wH.size() - 1
            int tj = wT.get(j)
            if tj >= chart.left_visible_bar_time and tj <= chart.right_visible_bar_time
                aH.push(wH.get(j))
                aL.push(wL.get(j))
                aV.push(wV.get(j))
                x0 := na(x0) ? wX.get(j) : x0
        x0 := nz(x0, bar_index)
    [c0, c1, c2, c3, c4, cv] = f_prof(aH, aL, aV, nRows, vaPct)
    dPoc := c0
    while pb.size() < nRows
        pb.push(box.new(bar_index, close, bar_index, close, border_color = color.new(UB_NEUTRAL, 100), bgcolor = color.new(UB_NEUTRAL, 100)))
    for j = 0 to pb.size() - 1
        box.set_bgcolor(pb.get(j), color.new(UB_NEUTRAL, 100))
    if not na(c0)
        float s = (c4 - c3) / nRows
        float mx = cv.max()
        int wEff = math.min(300, math.max(wBars, int(math.round(math.max(bar_index - x0 + 1, MIN_LB) * 0.45))))
        for j = 0 to nRows - 1
            float rBot = c3 + j * s
            float top = rBot + s
            bool inVa = rBot >= c2 - s * 0.01 and top <= c1 + s * 0.01
            bool isP = c0 > rBot and c0 < top
            color bc = isP ? UB_AMBER : inVa ? UB_SKY : UB_NEUTRAL
            int w = int(math.round(cv.get(j) / mx * wEff))
            box bx = pb.get(j)
            box.set_lefttop(bx, x0, top)
            box.set_rightbottom(bx, x0 + math.max(w, 0), rBot)
            box.set_bgcolor(bx, color.new(bc, isP ? 30 : inVa ? 60 : 78))
            box.set_border_color(bx, color.new(bc, 100))
        line.delete(lPoc)
        line.delete(lVah)
        line.delete(lVal)
        lPoc := line.new(x0, c0, bar_index, c0, color = UB_AMBER, width = 2)
        lVah := line.new(x0, c1, bar_index, c1, color = color.new(UB_SKY, 10), style = line.style_dashed)
        lVal := line.new(x0, c2, bar_index, c2, color = color.new(UB_SKY, 10), style = line.style_dashed)
        ckY.push(c0)
        ckN.push("POC")
        ckC.push(UB_AMBER)
        ckX.push(x0)
        ckY.push(c1)
        ckN.push("VAH")
        ckC.push(UB_SKY)
        ckX.push(x0)
        ckY.push(c2)
        ckN.push("VAL")
        ckC.push(UB_SKY)
        ckX.push(x0)
    // Name chips: solid fill, normal size, placed just LEFT of the line/profile start (off the POC band), nudged apart when levels are close.
    while chT.size() > 0
        label.delete(chT.pop())
    int nCk = ckY.size()
    if nCk > 0
        array<int> ord = ckY.sort_indices(order.ascending)
        float gap = nz(A, syminfo.mintick * 10) * 0.5
        float lastY = na
        for m = 0 to nCk - 1
            int q = ord.get(m)
            float yy = ckY.get(q)
            yy := na(lastY) ? yy : math.max(yy, lastY + gap)
            lastY := yy
            color fc = ckC.get(q)
            chT.push(label.new(ckX.get(q) - 1, yy, ckN.get(q), xloc = xloc.bar_index, style = label.style_label_right, color = color.new(fc, 0), textcolor = ub_txtOn(fc), size = size.normal, tooltip = ckN.get(q) + " " + str.tostring(ckY.get(q), format.mintick)))
 
alertcondition(touch == 1, "Prior level touch from above", "Unbroke Volume Flow Profile: prior POC/VAH/VAL touched from above")
alertcondition(touch == -1, "Prior level touch from below", "Unbroke Volume Flow Profile: prior POC/VAH/VAL touched from below")
 
plot(xpOn ? 100 : na, "VFP XP ver", display = display.data_window)
plot(xpOn ? pPoc : na, "VFP XP poc", display = display.data_window)
plot(xpOn ? pVah : na, "VFP XP vah", display = display.data_window)
plot(xpOn ? pVal : na, "VFP XP val", display = display.data_window)
plot(xpOn ? touch : na, "VFP XP touch", display = display.data_window)
plot(xpOn ? xRnd : na, "VFP XP rnd", display = display.data_window)
 
//==============================================================================
// PROOF PANEL
//==============================================================================
var table panel = na
if barstate.islast and showPanel
    if na(panel)
        panel := table.new(ub_pos(panelPos), 5, 11, bgcolor = na, frame_color = T_FRAME, frame_width = 1, border_color = T_BORDER, border_width = 1)
    panel.set_frame_color(T_FRAME)
    panel.set_border_color(T_BORDER)
    panel.clear(0, 0, 4, 10)
    tsz = ub_size(dashSize)
    ub_header(panel, 5, "Volume Profile Proof", timeframe.period + " | " + vpSrc, tsz)
    ub_proofHead(panel, 1, "Signal (history)", tsz)
    float base = ub_pct(cH.get(0), cN.get(0))
    int nB = cN.get(0)
    panel.cell(0, 2, "Random levels", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_halign = text.align_left, tooltip = "3 seeded random levels per session, inside the prior session's range, first touch scored with the same rule. This is the line to beat.")
    panel.cell(1, 2, str.tostring(nB), text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz)
    panel.cell(2, 2, na(base) ? "-" : str.tostring(base, "#") + "%", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_formatting = text.format_bold)
    panel.cell(3, 2, "baseline", text_color = T_MUTED, bgcolor = T_ROW, text_size = tsz)
    panel.cell(4, 2, "-", text_color = T_MUTED, bgcolor = T_ROW, text_size = tsz)
    ub_proofRow(panel, 3, "Random bars (info)", UB_NEUTRAL, cH.get(1), cN.get(1), base, nB, minN, tsz)
    ub_proofRow(panel, 4, "Prior POC touch", UB_AMBER, cH.get(2), cN.get(2), base, nB, minN, tsz)
    ub_proofRow(panel, 5, "Prior VAH touch", UB_SKY, cH.get(3), cN.get(3), base, nB, minN, tsz)
    ub_proofRow(panel, 6, "Prior VAL touch", UB_VIOLET, cH.get(4), cN.get(4), base, nB, minN, tsz)
    ub_proofRow(panel, 7, "All POC/VA touches", UB_GREEN, cH.get(5), cN.get(5), base, nB, minN, tsz)
    ub_footer(panel, 5, 8, "Approximation: bar volume split evenly over each bar's range, not true volume at price." + (useLtf and not ltfOk ? " No intrabar data on this bar: chart bars used." : ""))
    ub_footer(panel, 5, 9, "Now: POC " + (na(dPoc) ? "-" : str.tostring(dPoc, format.mintick)) + " | prior POC " + (na(pPoc) ? "-" : str.tostring(pPoc, format.mintick)) + ". Hit = 1 ATR(14) its way before 1 ATR against within " + str.tostring(hzBars) + " bars.")
    ub_footer(panel, 5, 10, "First touch per level per session, at bar close. Past results do not guarantee future results.")

← 返回列表