NLA Value Map

NeverLoseAgainTV · study · 542 行 · 点赞 56 · TradingView 原页

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

Pine Script

//@version=6
indicator("NLA Value Map", overlay = true, max_boxes_count = 500, max_lines_count = 100, max_labels_count = 100)
 
// NLA Value Map · v1 (1 Nov 2026)
// Last week's (and optionally yesterday's, last month's, this or last year's) volume profile, with its value
// area carried into the current period. Built from real traded volume.
// Each value-area edge is drawn as a zone: from the edge outward, through
// the volume that sits just outside the value area, until that volume runs out.
// A map of where to watch.
 
// ── Inputs
string G0 = "Daily (New York cash session 09:30-16:00)"
bool showD   = input.bool(false, "Yesterday's value area", group = G0)
bool showD2  = input.bool(false, "Also the day before", group = G0)
string G1 = "Weekly"
bool showW   = input.bool(true,  "Last week's value area", group = G1)
bool showW2  = input.bool(false, "Also the week before", group = G1)
string G2 = "Monthly"
bool showM   = input.bool(false, "Last month's value area", group = G2)
bool showM2  = input.bool(false, "Also the month before", group = G2)
string G3 = "Yearly"
bool showYd  = input.bool(true,  "This year so far (developing)", group = G3)
bool showY   = input.bool(false, "Last year's value area", group = G3)
bool showY2  = input.bool(false, "Also the year before", group = G3)
string G4 = "Profile"
string histOn  = input.string("Last week", "Draw the profile of", options = ["Last week", "Yesterday (cash session)", "Last month", "This year so far", "Last year", "None"], group = G4)
int    rowsN   = input.int(128, "Rows", minval = 24, maxval = 200, group = G4)
int    vaInput = input.int(70, "Value area %", minval = 50, maxval = 95, group = G4)
int    widthPct = input.int(30, "Profile width (% of the period)", minval = 5, maxval = 100, group = G4)
string G5 = "Zones"
int    cutPct  = input.int(35, "Zone ends where volume drops below (% of the value area's average)", minval = 5, maxval = 100, group = G5)
int    maxPct  = input.int(40, "Deepest zone (% of the value area's height)", minval = 5, maxval = 100, group = G5)
string G6 = "Regime"
bool   showReg = input.bool(true, "Show the weekly regime", group = G6)
bool   twoStep = input.bool(true, "Trend needs two POC steps in a row", group = G6)
 
float vaPct = vaInput / 100.0
float cutF  = cutPct / 100.0
float maxF  = maxPct / 100.0
 
// ── NLA colours
color TEAL  = #00C896
color BLUE  = #0066FF
color SKY   = #00AAFF
color AMBER = #F59E0B
color WHITE = #FFFFFF
color CYAN  = #00D4CC
 
// ── Volume profile maths
 
// 3-row average, so one thin row doesn't end a zone on its own
f_sm(array<float> a, int i) =>
    int n = a.size()
    float s = a.get(i)
    int c = 1
    if i > 0
        s += a.get(i - 1)
        c += 1
    if i < n - 1
        s += a.get(i + 1)
        c += 1
    s / c
 
// Each candle's real volume is spread over the price rows it traded, in
// proportion to how much of its range sits in each row. Up candles
// (close >= open) and down candles are kept apart for the colours.
f_bins(array<float> hs, array<float> ls, array<float> vs, array<bool> us, int rows, float pLow, float rs) =>
    array<float> upB = array.new<float>(rows, 0.0)
    array<float> dnB = array.new<float>(rows, 0.0)
    int n = hs.size()
    for i = 0 to n - 1
        float h = hs.get(i)
        float l = ls.get(i)
        float v = vs.get(i)
        bool  u = us.get(i)
        if v > 0
            if h <= l
                int rr = math.max(0, math.min(rows - 1, int((l - pLow) / rs)))
                if u
                    upB.set(rr, upB.get(rr) + v)
                else
                    dnB.set(rr, dnB.get(rr) + v)
            else
                int r0 = math.max(0, math.min(rows - 1, int((l - pLow) / rs)))
                int r1 = math.max(r0, math.min(rows - 1, int((h - pLow) / rs)))
                for r = r0 to r1
                    float bot = pLow + r * rs
                    float ov  = math.min(h, bot + rs) - math.max(l, bot)
                    if ov > 0
                        float add = v * ov / (h - l)
                        if u
                            upB.set(r, upB.get(r) + add)
                        else
                            dnB.set(r, dnB.get(r) + add)
    [upB, dnB]
 
// Value area (70% around the POC, the standard method) plus the two zones.
// Zone rule: start at the value-area edge and walk outward row by row while
// the volume there is still meaningful (at least cut x the value area's
// average row). Where it runs out, the zone ends.
// Returns the rows as one array: up volume first, then down volume.
f_profile(array<float> hs, array<float> ls, array<float> vs, array<bool> us, int rows, float pct, float cut, float mx) =>
    float vah = na
    float val = na
    float poc = na
    float zt  = na
    float zb  = na
    float pLow = na
    float rs   = na
    array<float> hist = array.new<float>(0)
    if hs.size() > 0
        float pHigh = hs.max()
        pLow := ls.min()
        if pHigh > pLow
            rs := (pHigh - pLow) / rows
            [upB, dnB] = f_bins(hs, ls, vs, us, rows, pLow, rs)
            array<float> tot = array.new<float>(rows, 0.0)
            for r = 0 to rows - 1
                tot.set(r, upB.get(r) + dnB.get(r))
            float total = tot.sum()
            if total > 0
                int pocIdx = tot.indexof(tot.max())
                int lo = pocIdx
                int hi = pocIdx
                float acc = tot.get(pocIdx)
                while acc < total * pct and (lo > 0 or hi < rows - 1)
                    float upV = hi < rows - 1 ? tot.get(hi + 1) : -1.0
                    float dnV = lo > 0 ? tot.get(lo - 1) : -1.0
                    if upV >= dnV
                        hi += 1
                        acc += upV
                    else
                        lo -= 1
                        acc += dnV
                vah := pLow + (hi + 1) * rs
                val := pLow + lo * rs
                poc := pLow + (pocIdx + 0.5) * rs
                float vaSum = 0.0
                for r = lo to hi
                    vaSum += tot.get(r)
                float thr = vaSum / (hi - lo + 1) * cut
                int maxRows = math.max(1, int(math.round((hi - lo + 1) * mx)))
                int j = hi + 1
                while j <= rows - 1 and j - hi <= maxRows and f_sm(tot, j) >= thr
                    j += 1
                zt := pLow + math.max(j, hi + 2) * rs
                int k = lo - 1
                while k >= 0 and lo - k <= maxRows and f_sm(tot, k) >= thr
                    k -= 1
                zb := pLow + math.min(k + 1, lo - 1) * rs
                hist := upB.concat(dnB)
    [vah, val, poc, zt, zb, hist, pLow, rs]
 
// Runs inside the requested timeframe: collects each period's candles,
// closes the profile when a new period starts, keeps the last three.
// A period is only used if the chart's history covers it from its first
// candle: a cut-off period would give a wrong profile, so it stays empty.
// With a session (daily), only candles inside it count, and the profile
// closes when the session ends, so it shows through the overnight hours.
f_periods(simple string ptf, simple string sess, int rows, float pct, float cut, float mx, bool wantH) =>
    var array<float> hs = array.new<float>()
    var array<float> ls = array.new<float>()
    var array<float> vs = array.new<float>()
    var array<bool>  us = array.new<bool>()
    var array<float> EMPTY = array.new<float>(0)
    var float vah1 = na
    var float val1 = na
    var float poc1 = na
    var float zt1  = na
    var float zb1  = na
    var float vah2 = na
    var float val2 = na
    var float poc2 = na
    var float zt2  = na
    var float zb2  = na
    var float vah3 = na
    var float val3 = na
    var float poc3 = na
    var int   st0 = na
    var int   st1 = na
    var int   st2 = na
    var array<float> h1 = array.new<float>(0)
    var float pl1 = na
    var float rs1 = na
    var bool  okCur = false
    bool inS = sess == "" or not na(time(timeframe.period, sess, "America/New_York"))
    bool chg = timeframe.change(ptf) and not barstate.isfirst
    bool closeEvt = sess == "" ? chg : not barstate.isfirst and not inS and inS[1]
    bool startEvt = sess == "" ? chg : not barstate.isfirst and inS and not inS[1]
    if closeEvt and hs.size() > 0
        [a, b, c, d, e, h, pl, r] = f_profile(hs, ls, vs, us, rows, pct, cut, mx)
        vah3 := vah2
        val3 := val2
        poc3 := poc2
        vah2 := vah1
        val2 := val1
        poc2 := poc1
        zt2  := zt1
        zb2  := zb1
        vah1 := okCur ? a : na
        val1 := okCur ? b : na
        poc1 := okCur ? c : na
        zt1  := okCur ? d : na
        zb1  := okCur ? e : na
        h1   := okCur ? h : EMPTY
        pl1  := pl
        rs1  := r
        st2  := st1
        st1  := st0
        hs.clear()
        ls.clear()
        vs.clear()
        us.clear()
        okCur := false
    if startEvt
        okCur := true
    if startEvt or na(st0)
        st0 := time
    if inS
        hs.push(high)
        ls.push(low)
        vs.push(nz(volume))
        us.push(close >= open)
    // the histogram rows only travel on the last bar (keeps memory low)
    [vah1, val1, poc1, zt1, zb1, vah2, val2, poc2, zt2, zb2, vah3, val3, poc3, st1, st2, wantH and barstate.islast ? h1 : EMPTY, pl1, rs1]
 
// The period still running (this year so far), built on the last bar only.
f_dev(simple string ptf, int rows, float pct, float cut, float mx, bool wantH) =>
    var array<float> hs = array.new<float>()
    var array<float> ls = array.new<float>()
    var array<float> vs = array.new<float>()
    var array<bool>  us = array.new<bool>()
    var array<float> EMPTY = array.new<float>(0)
    var int  st0 = na
    var bool okCur = false
    bool newP = timeframe.change(ptf) and not barstate.isfirst
    if newP
        hs.clear()
        ls.clear()
        vs.clear()
        us.clear()
        okCur := true
    if newP or na(st0)
        st0 := time
    hs.push(high)
    ls.push(low)
    vs.push(nz(volume))
    us.push(close >= open)
    float dVah = na
    float dVal = na
    float dPoc = na
    float dZt  = na
    float dZb  = na
    float dPl  = na
    float dRs  = na
    array<float> dH = EMPTY
    if barstate.islast and okCur
        [a, b, c, d, e, h, pl, r] = f_profile(hs, ls, vs, us, rows, pct, cut, mx)
        dVah := a
        dVal := b
        dPoc := c
        dZt  := d
        dZb  := e
        dPl  := pl
        dRs  := r
        if wantH
            dH := h
    [dVah, dVal, dPoc, dZt, dZb, st0, dH, dPl, dRs]
 
// Weekly from 15-minute candles: always on (it also drives the regime).
[wVah1, wVal1, wPoc1, wZt1, wZb1, wVah2, wVal2, wPoc2, wZt2, wZb2, wVah3, wVal3, wPoc3, wSt1, wSt2, wH, wPl, wRs] = request.security(syminfo.tickerid, "15", f_periods("W", "", rowsN, vaPct, cutF, maxF, histOn == "Last week"))
 
// Daily, monthly and yearly only run when switched on, so the indicator stays light.
var array<float> NOH = array.new<float>(0)
 
float dVah1 = na
float dVal1 = na
float dPoc1 = na
float dZt1 = na
float dZb1 = na
float dVah2 = na
float dVal2 = na
float dPoc2 = na
float dZt2 = na
float dZb2 = na
float dVah3 = na
float dVal3 = na
float dPoc3 = na
int   dSt1 = na
int   dSt2 = na
array<float> dH = NOH
float dPl = na
float dRs = na
if showD or showD2 or histOn == "Yesterday (cash session)"
    // yesterday's New York cash session, from 5-minute candles
    [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18] = request.security(syminfo.tickerid, "5", f_periods("D", "0930-1600", rowsN, vaPct, cutF, maxF, histOn == "Yesterday (cash session)"))
    dVah1 := a1
    dVal1 := a2
    dPoc1 := a3
    dZt1 := a4
    dZb1 := a5
    dVah2 := a6
    dVal2 := a7
    dPoc2 := a8
    dZt2 := a9
    dZb2 := a10
    dVah3 := a11
    dVal3 := a12
    dPoc3 := a13
    dSt1 := a14
    dSt2 := a15
    dH := a16
    dPl := a17
    dRs := a18
 
float mVah1 = na
float mVal1 = na
float mPoc1 = na
float mZt1 = na
float mZb1 = na
float mVah2 = na
float mVal2 = na
float mPoc2 = na
float mZt2 = na
float mZb2 = na
float mVah3 = na
float mVal3 = na
float mPoc3 = na
int   mSt1 = na
int   mSt2 = na
array<float> mH = NOH
float mPl = na
float mRs = na
if showM or showM2 or histOn == "Last month"
    // last month, from 30-minute candles
    [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18] = request.security(syminfo.tickerid, "30", f_periods("M", "", rowsN, vaPct, cutF, maxF, histOn == "Last month"))
    mVah1 := a1
    mVal1 := a2
    mPoc1 := a3
    mZt1 := a4
    mZb1 := a5
    mVah2 := a6
    mVal2 := a7
    mPoc2 := a8
    mZt2 := a9
    mZb2 := a10
    mVah3 := a11
    mVal3 := a12
    mPoc3 := a13
    mSt1 := a14
    mSt2 := a15
    mH := a16
    mPl := a17
    mRs := a18
 
float yVah1 = na
float yVal1 = na
float yPoc1 = na
float yZt1 = na
float yZb1 = na
float yVah2 = na
float yVal2 = na
float yPoc2 = na
float yZt2 = na
float yZb2 = na
float yVah3 = na
float yVal3 = na
float yPoc3 = na
int   ySt1 = na
int   ySt2 = na
array<float> yH = NOH
float yPl = na
float yRs = na
if showY or showY2 or histOn == "Last year"
    // last year, from 4-hour candles
    [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18] = request.security(syminfo.tickerid, "240", f_periods("12M", "", rowsN, vaPct, cutF, maxF, histOn == "Last year"))
    yVah1 := a1
    yVal1 := a2
    yPoc1 := a3
    yZt1 := a4
    yZb1 := a5
    yVah2 := a6
    yVal2 := a7
    yPoc2 := a8
    yZt2 := a9
    yZb2 := a10
    yVah3 := a11
    yVal3 := a12
    yPoc3 := a13
    ySt1 := a14
    ySt2 := a15
    yH := a16
    yPl := a17
    yRs := a18
 
float yDVah = na
float yDVal = na
float yDPoc = na
float yDZt  = na
float yDZb  = na
int   yDSt  = na
array<float> yDH = NOH
float yDPl  = na
float yDRs  = na
if showYd or histOn == "This year so far"
    // this year so far, from 4-hour candles
    [b1, b2, b3, b4, b5, b6, b7, b8, b9] = request.security(syminfo.tickerid, "240", f_dev("12M", rowsN, vaPct, cutF, maxF, histOn == "This year so far"))
    yDVah := b1
    yDVal := b2
    yDPoc := b3
    yDZt  := b4
    yDZb  := b5
    yDSt  := b6
    yDH   := b7
    yDPl  := b8
    yDRs  := b9
 
// ── Weekly regime: POC migration
// A POC steps up when it sits above the previous week's value area high,
// steps down when it sits below the previous week's value area low.
f_step(float poc, float vahPrev, float valPrev) =>
    na(poc) or na(vahPrev) or na(valPrev) ? 0 : poc > vahPrev ? 1 : poc < valPrev ? -1 : 0
 
int s1 = f_step(wPoc1, wVah2, wVal2)
int s2 = f_step(wPoc2, wVah3, wVal3)
int regime = twoStep ? (s1 == 1 and s2 == 1 ? 1 : s1 == -1 and s2 == -1 ? -1 : 0) : s1
 
// ── Drawing: only the current set, redrawn on the last bar
var array<box>   bxs = array.new<box>()
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
var table regTable = table.new(position.top_right, 1, 3, bgcolor = color.new(#0A0A0A, 10), border_width = 0, frame_color = color.new(SKY, 55), frame_width = 1)
 
f_hist(array<float> h, float pl, float rs, int t0, int t1, float vah, float val) =>
    int n = int(h.size() / 2)
    if n > 0 and not na(pl) and not na(rs) and not na(t0) and not na(t1) and t1 > t0 and not na(vah)
        float mx = 0.0
        for r = 0 to n - 1
            mx := math.max(mx, h.get(r) + h.get(n + r))
        if mx > 0
            float span = (t1 - t0) * widthPct / 100.0
            float gap = rs * 0.12
            for r = 0 to n - 1
                float u = h.get(r)
                float d = h.get(n + r)
                if u + d > 0
                    float bot = pl + r * rs
                    float top = bot + rs
                    bool inVA = bot >= val - rs * 0.5 and top <= vah + rs * 0.5
                    int xu = t0 + int(span * u / mx)
                    int xd = xu + int(span * d / mx)
                    if xu > t0
                        bxs.push(box.new(t0, top - gap, xu, bot, xloc = xloc.bar_time, bgcolor = color.new(TEAL, inVA ? 25 : 65), border_color = na))
                    if xd > xu
                        bxs.push(box.new(xu, top - gap, xd, bot, xloc = xloc.bar_time, bgcolor = color.new(BLUE, inVA ? 20 : 62), border_color = na))
 
f_set(float vah, float val, float poc, float zt, float zb, int lineL, int zoneL, int zoneR, int txtX, color c, string name, bool older, int hl) =>
    // hl: 1 = the VAL zone is the best R:R zone, -1 = the VAH zone, 0 = none
    if not na(vah) and not na(val) and vah > val and not na(zoneL) and not na(zoneR)
        string sty = older ? line.style_dashed : line.style_solid
        string bestTxt = "  ·  BEST R:R ZONE · WAIT FOR THE REACTION"
        int tH = hl == -1 ? 62 : older ? 90 : 82
        int tL = hl == 1 ? 62 : older ? 90 : 82
        color bH = hl == -1 ? SKY : color.new(c, 55)
        color bL = hl == 1 ? SKY : color.new(c, 55)
        // labels sit just right of the latest candle, so they stay in view
        int tx = math.min(zoneR, txtX)
        color tc = color.new(WHITE, older ? 35 : 0)
        color noFill = color.new(#000000, 100)
        if not na(zt)
            bxs.push(box.new(zoneL, zt, zoneR, vah, xloc = xloc.bar_time, bgcolor = color.new(c, tH), border_color = bH, border_width = 1, border_style = sty))
            lbs.push(label.new(tx, (vah + zt) / 2, name + " VAH " + str.tostring(math.round_to_mintick(vah), "#,##0.00") + (hl == -1 ? bestTxt : ""), xloc = xloc.bar_time, style = label.style_label_right, color = noFill, textcolor = tc, size = size.small))
        if not na(zb)
            bxs.push(box.new(zoneL, val, zoneR, zb, xloc = xloc.bar_time, bgcolor = color.new(c, tL), border_color = bL, border_width = 1, border_style = sty))
            lbs.push(label.new(tx, (val + zb) / 2, name + " VAL " + str.tostring(math.round_to_mintick(val), "#,##0.00") + (hl == 1 ? bestTxt : ""), xloc = xloc.bar_time, style = label.style_label_right, color = noFill, textcolor = tc, size = size.small))
        int lL = na(lineL) ? zoneL : lineL
        lns.push(line.new(lL, vah, zoneR, vah, xloc = xloc.bar_time, color = color.new(WHITE, older ? 45 : 0), width = 1, style = sty))
        lns.push(line.new(lL, val, zoneR, val, xloc = xloc.bar_time, color = color.new(WHITE, older ? 45 : 0), width = 1, style = sty))
        if not na(poc)
            lns.push(line.new(lL, poc, zoneR, poc, xloc = xloc.bar_time, color = color.new(AMBER, older ? 40 : 0), width = 1, style = sty))
            lbs.push(label.new(tx, poc, name + " POC " + str.tostring(math.round_to_mintick(poc), "#,##0.00") + "  ·  MIDDLE · WAIT", xloc = xloc.bar_time, style = label.style_label_right, color = noFill, textcolor = color.new(AMBER, older ? 35 : 0), size = size.small))
 
if barstate.islast
    for b in bxs
        b.delete()
    bxs.clear()
    for l in lns
        l.delete()
    lns.clear()
    for lb in lbs
        lb.delete()
    lbs.clear()
 
    int wl = time("W")
    int wr = nz(time_close("W"), wl + 7 * 86400000)
    int ml = time("M")
    int mr = nz(time_close("M"), ml + 31 * 86400000)
    int dl = time("D")
    int dr = nz(time_close("D"), dl + 86400000)
    int yl = time("12M")
    int yr = nz(time_close("12M"), yl + 366 * 86400000)
    int tX = time + 20 * timeframe.in_seconds() * 1000
 
    if histOn == "Last week"
        f_hist(wH, wPl, wRs, wSt1, wl, wVah1, wVal1)
    else if histOn == "Yesterday (cash session)"
        f_hist(dH, dPl, dRs, dSt1, dSt1 + 23400000, dVah1, dVal1)
    else if histOn == "Last month"
        f_hist(mH, mPl, mRs, mSt1, ml, mVah1, mVal1)
    else if histOn == "This year so far"
        f_hist(yDH, yDPl, yDRs, yDSt, time, yDVah, yDVal)
    else if histOn == "Last year"
        f_hist(yH, yPl, yRs, ySt1, yl, yVah1, yVal1)
 
    int hlW = showReg ? regime : 0
 
    if showY2
        f_set(yVah2, yVal2, yPoc2, yZt2, yZb2, ySt2, yl, yr, tX, WHITE, "YEAR BEFORE", true, 0)
    if showYd
        f_set(yDVah, yDVal, yDPoc, yDZt, yDZb, yDSt, yl, yr, tX, WHITE, "THIS YEAR", false, 0)
    if showY
        f_set(yVah1, yVal1, yPoc1, yZt1, yZb1, ySt1, yl, yr, tX, WHITE, "LAST YEAR", false, 0)
    if showM2
        f_set(mVah2, mVal2, mPoc2, mZt2, mZb2, mSt2, ml, mr, tX, TEAL, "MONTH BEFORE", true, 0)
    if showM
        f_set(mVah1, mVal1, mPoc1, mZt1, mZb1, mSt1, ml, mr, tX, TEAL, "LAST MONTH", false, 0)
    if showW2
        f_set(wVah2, wVal2, wPoc2, wZt2, wZb2, wSt2, wl, wr, tX, BLUE, "WEEK BEFORE", true, 0)
    if showW
        f_set(wVah1, wVal1, wPoc1, wZt1, wZb1, wSt1, wl, wr, tX, BLUE, "LAST WEEK", false, hlW)
    if showD2
        f_set(dVah2, dVal2, dPoc2, dZt2, dZb2, dSt2, dl, dr, tX, CYAN, "DAY BEFORE", true, 0)
    if showD
        f_set(dVah1, dVal1, dPoc1, dZt1, dZb1, dSt1, dl, dr, tX, CYAN, "YESTERDAY", false, 0)
 
    if showReg
        string regTxt = regime == 1 ? "BULLISH" : regime == -1 ? "BEARISH" : "BALANCED"
        color regCol = regime == 1 ? TEAL : regime == -1 ? SKY : color.new(WHITE, 40)
        string zoneTxt = regime == 1 ? "Best R:R zone: last week's VAL" : regime == -1 ? "Best R:R zone: last week's VAH" : "Both edges · middle = wait"
        table.cell(regTable, 0, 0, "WEEKLY REGIME", text_color = color.new(WHITE, 30), text_size = size.small, text_halign = text.align_left)
        table.cell(regTable, 0, 1, regTxt, text_color = regCol, text_size = size.large, text_halign = text.align_left)
        table.cell(regTable, 0, 2, zoneTxt + "\nWait for the reaction.", text_color = color.new(WHITE, 15), text_size = size.small, text_halign = text.align_left)

← 返回列表