Volume Delta Pivot Matrix [BigBeluga]

BigBeluga · study · 180 行 · 点赞 2,739 · TradingView 原页

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

Pine Script

// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International  
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
 
//@version=6
indicator("Volume Delta Pivot Matrix [BigBeluga]", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 5000, calc_bars_count = 5000)
 
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
g_piv    = "Pivot Settings"
lbars    = input.int(5, "Pivot Swing Length", minval=1, group=g_piv)
rbars    = lbars
mxlvl    = input.int(10, "Max Active Levels to Plot", minval=1, maxval=50, group=g_piv)
mdpct    = input.float(20.0, "Minimum Delta % Filter", minval=0.0, maxval=100.0, step=5.0, group=g_piv)
lopt     = input.string("Solid", "Line Style", options=["Solid", "Dotted", "Dashed"], group=g_piv)
lwidth   = input.int(3, "Level Width", group=g_piv)
 
g_sty    = "Visual Styling"
cbull    = input.color(color.rgb(24, 206, 33), "Bullish Delta / Support Color", group=g_sty)
cbear    = input.color(#FF3D00, "Bearish Delta / Resistance Color", group=g_sty)
// }
 
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
//  USER DEFINED TYPE 
type Plvl
    float lvl
    float dlta
    bool  supp
    int   bidx
    line  ln
 
//  PERSISTENT STORAGE ARRAYS 
var parr          = array.new<Plvl>()
var label[] clabs = array.new<label>()
var label[] alabs = array.new<label>()
 
//  LINE STYLE HELPER 
gstln(string styl) =>
    switch styl
        "Solid"  => line.style_solid
        "Dotted" => line.style_dotted
        "Dashed" => line.style_dashed
        => line.style_dotted
 
//  DETECT PIVOTS IN REAL-TIME & COLLECT 
float ph = ta.pivothigh(high, lbars, rbars)
float pl = ta.pivotlow(low, lbars, rbars)
 
int pbidx = bar_index - rbars
 
if not na(ph)
    float wdlta = 0.0
    for i = 0 to (lbars + rbars)
        wdlta += (close[i] >= open[i] ? volume[i] : -volume[i])
    array.push(parr, Plvl.new(ph, wdlta, false, pbidx, na))
 
if not na(pl)
    float wdlta = 0.0
    for i = 0 to (lbars + rbars)
        wdlta += (close[i] >= open[i] ? volume[i] : -volume[i])
    array.push(parr, Plvl.new(pl, wdlta, true, pbidx, na))
 
//  MANAGEMENT: CHECK BREAKOUTS (iterate safely forward, remove from the end)
if array.size(parr) > 0
    for i = array.size(parr) - 1 to 0
        Plvl clvl = array.get(parr, i)
 
        bool csupp = clvl.supp and close <= clvl.lvl and barstate.isconfirmed
        bool cres  = not clvl.supp and close >= clvl.lvl and barstate.isconfirmed
 
        if csupp or cres
            if not na(clvl.ln)
                line.delete(clvl.ln)
            array.remove(parr, i)
// }
 
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
if barstate.islast
 
    //  ENFORCE MAX LEVELS CAPACITY (trim oldest, keep newest mxlvl)
    if array.size(parr) > mxlvl
        int excs = array.size(parr) - mxlvl
        for i = 0 to excs - 1
            Plvl olvl = array.shift(parr)
            if not na(olvl.ln)
                line.delete(olvl.ln)
 
    // clear previous labels
    if array.size(alabs) > 0
        for i = 0 to array.size(alabs) - 1
            label.delete(array.get(alabs, i))
        array.clear(alabs)
 
    if array.size(clabs) > 0
        for i = 0 to array.size(clabs) - 1
            label.delete(array.get(clabs, i))
        array.clear(clabs)
 
    int titem = array.size(parr)
    lsten = gstln(lopt)
 
    if titem > 0
        // find max delta for percentage scaling
        float mdelt = 0.0001
        for i = 0 to titem - 1
            mdelt := math.max(mdelt, math.abs(array.get(parr, i).dlta))
 
        // plot only valid levels (pct >= mdpct), newest first, up to mxlvl
        int plotted = 0
        for i = titem - 1 to 0
            if plotted >= mxlvl
                break
 
            Plvl lvl = array.get(parr, i)
            float rato = math.abs(lvl.dlta) / mdelt
            float pct  = rato * 100.0
 
            // skip low-delta levels: hide any existing line, do not plot
            if pct < mdpct
                if not na(lvl.ln)
                    line.delete(lvl.ln)
                    lvl.ln := na
                continue
 
            color mcol = lvl.supp ? cbull : cbear
 
            if na(lvl.ln)
                lvl.ln := line.new(lvl.bidx, lvl.lvl, bar_index, lvl.lvl, color=color.new(mcol, 40), width=lwidth, style=lsten)
            else
                line.set_xy2(lvl.ln, bar_index, lvl.lvl)
                line.set_color(lvl.ln, color.new(mcol, 40))
 
            int ncirc = math.min(4, math.max(1, math.ceil(pct / 25.0)))
 
            for c = ncirc to 1
                string bsize = c == 1 ? size.tiny : c == 2 ? size.small : c == 3 ? size.normal : size.large
                color gcol = lvl.supp ?
                     color.from_gradient(c, 1, 4, color.new(#FFD600, 50), color.new(cbull, 50)) :
                     color.from_gradient(c, 1, 4, color.new(#FFD600, 50), color.new(cbear, 50))
 
                label clbl = label.new(
                    x             = lvl.bidx,
                    y             = lvl.lvl,
                    xloc          = xloc.bar_index,
                    yloc          = yloc.price,
                    color         = gcol,
                    style         = label.style_circle,
                    size          = bsize,
                    text          = c == ncirc ? str.tostring(pct, format.percent) : "",
                    force_overlay = true
                 )
                array.push(clabs, clbl)
 
            label lbl = label.new(
                 x         = bar_index,
                 y         = lvl.lvl,
                 text      = str.tostring(lvl.lvl, "#.##  "),
                 xloc      = xloc.bar_index,
                 yloc      = yloc.price,
                 color     = color.new(mcol, 100),
                 style     = label.style_label_left,
                 textcolor = mcol,
                 size      = size.normal
             )
            array.push(alabs, lbl)
 
            label ecirc = label.new(
                x         = bar_index,
                y         = lvl.lvl,
                xloc      = xloc.bar_index,
                yloc      = yloc.price,
                color     = color.new(mcol, 100),
                style     = label.style_label_center,
                size      = size.normal,
                text      = "●",
                textcolor = color.new(mcol, 0)
            )
            array.push(clabs, ecirc)
 
            plotted += 1
// }

← 返回列表