Value Area Levels ver 1

Holstie · study · 216 行 · 点赞 28 · TradingView 原页

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

Pine Script

//@version=6
indicator("Value Area Levels ver 1", "VA Levels", overlay=true, max_lines_count=60, max_labels_count=30)
 
// Draws value area high (VAH), point of control (POC) and value area low (VAL)
// for the previous week, the current week, the previous day and the current day.
// Each level's solid line covers only the bars of its own period. A dotted line
// then runs from the end of that period to a label past the last bar, so every
// level can be read off at the right edge of the chart. The previous week comes
// from a higher-timeframe request; the other three are built from chart bars.
 
gPr = "Profile"
dayRowT   = input.int(4, "Day Row Size (ticks)", minval=1, tooltip="Height of each price row in the daily profiles, in ticks. 4 ticks = 1.00 point on MNQ. Smaller rows give more precise levels.", group=gPr)
wkRowT    = input.int(8, "Week Row Size (ticks)", minval=1, tooltip="Height of each price row in the weekly profiles, in ticks. 8 ticks = 2.00 points on MNQ.", group=gPr)
vaPct     = input.float(70, "Value Area (%)", minval=1, maxval=100, tooltip="Share of the period's volume that the value area holds, built outward from the POC.", group=gPr)
hoursMode = input.string("Full session", "Profile Hours", options=["Full session", "RTH only"], tooltip="Full session counts every bar of the trading day (the CME day starts at 18:00 New York). RTH only counts just the bars inside the RTH window below.", group=gPr)
rthSess   = input.session("0930-1600", "RTH Window (New York)", tooltip="Used only when Profile Hours is RTH only.", group=gPr)
pwTf      = input.timeframe("15", "Previous Week Source Timeframe", tooltip="The previous week's profile is built from this timeframe's bars, fetched straight from TradingView, so it does not depend on how much history your chart has loaded. A single weekly bar has no inner volume detail, so the week is rebuilt from these smaller bars. Lower = more precise.", group=gPr)
 
gLv = "Levels"
showPW  = input.bool(true, "Previous Week", inline="pw", group=gLv)
colPW   = input.color(#9C27B0, "", inline="pw", group=gLv)
showW   = input.bool(true, "Current Week", inline="w", group=gLv)
colW    = input.color(#2962FF, "", inline="w", group=gLv)
showPD  = input.bool(true, "Previous Day", inline="pd", group=gLv)
colPD   = input.color(#FF9800, "", inline="pd", group=gLv)
showD   = input.bool(true, "Current Day", inline="d", group=gLv)
colD    = input.color(#00BCD4, "", inline="d", group=gLv)
showVah = input.bool(true, "VAH", inline="lv", group=gLv)
showPoc = input.bool(true, "POC", inline="lv", group=gLv)
showVal = input.bool(true, "VAL", inline="lv", group=gLv)
 
gSt = "Style"
pocWidth  = input.int(2, "POC Line Width", minval=1, maxval=5, group=gSt)
vaWidth   = input.int(1, "VAH / VAL Line Width", minval=1, maxval=5, group=gSt)
lblOffset = input.int(15, "Label Distance Past Last Bar (bars)", minval=1, maxval=300, tooltip="How far to the right of the last bar the labels sit. The dotted lines run out to them.", group=gSt)
showPx    = input.bool(true, "Show Price In Labels", group=gSt)
lblSizeIn = input.string("Small", "Label Size", options=["Tiny", "Small", "Normal"], group=gSt)
 
// --- Globals -----------------------------------------------------------------
float rsD    = dayRowT * syminfo.mintick
float rsW    = wkRowT * syminfo.mintick
bool  inRth  = not na(time(timeframe.period, rthSess, "America/New_York"))
bool  inHours = hoursMode == "Full session" or inRth
bool  newDay  = timeframe.change("D")
bool  newWeek = timeframe.change("W")
string lblSz  = lblSizeIn == "Tiny" ? size.tiny : lblSizeIn == "Normal" ? size.normal : size.small
 
// A profile being built. t1/t2 are the times of the first and last bars that
// added volume. full is false when the chart's history began mid-period.
type Period
    map<int, float> prof
    float rowSz
    int   t1
    int   t2
    bool  full
 
// The finished levels of a completed period.
type Levels
    float poc
    float vah
    float val
    int   t1
    int   t2
    bool  full
 
// --- Profile helpers ---------------------------------------------------------
// Spreads one bar's volume evenly across the price rows its range covers.
f_addBar(map<int, float> m, float h, float l, float v, float rowSz) =>
    int rLo = int(math.floor(l / rowSz))
    int rHi = int(math.floor(h / rowSz))
    for r = rLo to rHi
        float share = h > l ? (math.min(h, (r + 1) * rowSz) - math.max(l, r * rowSz)) / (h - l) : 1.0
        if share > 0
            m.put(r, nz(m.get(r)) + v * share)
    true
 
// Returns [poc, vah, val] for a profile, or na values when it holds no volume.
// The value area grows from the POC row, one row at a time toward the side with
// more volume, until it holds vaP percent of the total.
f_levels(map<int, float> m, float rowSz, float vaP) =>
    float poc = na
    float vah = na
    float val = na
    if m.size() > 0
        array<int> ks = m.keys()
        int rLo = int(array.min(ks))
        int rHi = int(array.max(ks))
        float total = 0.0
        float maxV = -1.0
        int pocRow = rLo
        for r = rLo to rHi
            float vr = nz(m.get(r))
            total += vr
            if vr > maxV
                maxV := vr
                pocRow := r
        if total > 0
            int upRow = pocRow
            int dnRow = pocRow
            float run = maxV
            float limit = total * vaP / 100
            while run < limit and (upRow < rHi or dnRow > rLo)
                float vUp = upRow < rHi ? nz(m.get(upRow + 1)) : -1.0
                float vDn = dnRow > rLo ? nz(m.get(dnRow - 1)) : -1.0
                if vUp >= vDn
                    upRow += 1
                    run += vUp
                else
                    dnRow -= 1
                    run += vDn
            poc := math.round_to_mintick((pocRow + 0.5) * rowSz)
            vah := math.round_to_mintick((upRow + 1) * rowSz)
            val := math.round_to_mintick(dnRow * rowSz)
    [poc, vah, val]
 
// Closes a period: returns its levels, or keeps the old ones if it had no volume.
f_finish(Period p, Levels old) =>
    Levels out = old
    if p.prof.size() > 0
        [pc, vh, vl] = f_levels(p.prof, p.rowSz, vaPct)
        out := Levels.new(pc, vh, vl, p.t1, p.t2, p.full)
    out
 
// Empties a period so the next one can start.
f_reset(Period p) =>
    p.prof.clear()
    p.t1 := na
    p.t2 := na
    p.full := bar_index > 0
    true
 
// Adds this bar to a period when it falls inside the profile hours.
f_accum(Period p) =>
    if inHours
        f_addBar(p.prof, high, low, nz(volume), p.rowSz)
        if na(p.t1)
            p.t1 := time
        p.t2 := time
    true
 
// Runs on the source timeframe's bars (via request.security) and returns the
// last completed week's levels, so chart history depth does not matter.
f_prevWeekHtf() =>
    var Period p = Period.new(map.new<int, float>(), rsW, na, na, false)
    var Levels lv = Levels.new(na, na, na, na, na, false)
    bool rthBar = not na(time(timeframe.period, rthSess, "America/New_York"))
    bool useBar = hoursMode == "Full session" or rthBar
    if timeframe.change("W")
        lv := f_finish(p, lv)
        f_reset(p)
    if useBar
        f_addBar(p.prof, high, low, nz(volume), p.rowSz)
        if na(p.t1)
            p.t1 := time
        p.t2 := time_close
    [lv.poc, lv.vah, lv.val, lv.t1, lv.t2, lv.full]
 
[pwPoc, pwVah, pwVal, pwT1, pwT2, pwFull] = request.security(syminfo.tickerid, pwTf, f_prevWeekHtf(), lookahead=barmerge.lookahead_off)
 
// --- State -------------------------------------------------------------------
var Period wk = Period.new(map.new<int, float>(), rsW, na, na, false)
var Period dy = Period.new(map.new<int, float>(), rsD, na, na, false)
var Levels pd = Levels.new(na, na, na, na, na, false)
 
if newWeek
    f_reset(wk)
if newDay
    pd := f_finish(dy, pd)
    f_reset(dy)
 
f_accum(wk)
f_accum(dy)
 
// --- Drawing -----------------------------------------------------------------
// One level: a solid line over its own period, a dotted line from the period's
// end to the label, and the label past the last bar.
f_level(array<line> ls, array<label> bs, float y, int t1, int t2, int tLbl, color c, int w, string name, string tail) =>
    if not na(y) and not na(t1)
        ls.push(line.new(x1=t1, y1=y, x2=t2, y2=y, xloc=xloc.bar_time, extend=extend.none, color=c, style=line.style_solid, width=w))
        ls.push(line.new(x1=t2, y1=y, x2=tLbl, y2=y, xloc=xloc.bar_time, extend=extend.none, color=color.new(c, 30), style=line.style_dotted, width=1))
        string txt = name + (showPx ? "  " + str.tostring(y, format.mintick) : "") + tail
        bs.push(label.new(x=tLbl, y=y, text=txt, xloc=xloc.bar_time, yloc=yloc.price, color=c, style=label.style_label_left, textcolor=color.white, size=lblSz))
    true
 
// All three levels of one period.
f_group(array<line> ls, array<label> bs, float poc, float vah, float val, int t1, int t2, bool full, int tLbl, color c, string name) =>
    string tail = full ? "" : " (partial)"
    if showVah
        f_level(ls, bs, vah, t1, t2, tLbl, c, vaWidth, name + " VAH", tail)
    if showPoc
        f_level(ls, bs, poc, t1, t2, tLbl, c, pocWidth, name + " POC", tail)
    if showVal
        f_level(ls, bs, val, t1, t2, tLbl, c, vaWidth, name + " VAL", tail)
    true
 
var array<line>  drawnL = array.new<line>()
var array<label> drawnB = array.new<label>()
 
if barstate.islast
    for ln in drawnL
        ln.delete()
    for lb in drawnB
        lb.delete()
    drawnL.clear()
    drawnB.clear()
    int tLbl = time + lblOffset * timeframe.in_seconds() * 1000
    if showPW
        f_group(drawnL, drawnB, pwPoc, pwVah, pwVal, pwT1, pwT2, pwFull, tLbl, colPW, "Prev Week")
    if showW
        [wPoc, wVah, wVal] = f_levels(wk.prof, wk.rowSz, vaPct)
        f_group(drawnL, drawnB, wPoc, wVah, wVal, wk.t1, wk.t2, wk.full, tLbl, colW, "Week")
    if showPD
        f_group(drawnL, drawnB, pd.poc, pd.vah, pd.val, pd.t1, pd.t2, pd.full, tLbl, colPD, "Prev Day")
    if showD
        [dPoc, dVah, dVal] = f_levels(dy.prof, dy.rowSz, vaPct)
        f_group(drawnL, drawnB, dPoc, dVah, dVal, dy.t1, dy.t2, dy.full, tLbl, colD, "Day")

← 返回列表