Volume Delta Pivot Matrix with alerts

JaneAutomates · study · 94 行 · 点赞 43 · TradingView 原页

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

Pine Script

//@version=6
indicator("Volume Delta Pivot Matrix with alerts", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 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", 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)
 
g_sty  = "Visual Styling"
cbull  = input.color(color.rgb(24, 206, 33), "Support Color", group = g_sty)
cbear  = input.color(#FF3D00, "Resistance Color", group = g_sty)
showLn = input.bool(true, "Show Level Lines", group = g_sty)
lopt   = input.string("Solid", "Line Style", options = ["Solid", "Dotted", "Dashed"], group = g_sty)
lwidth = input.int(2, "Line Width", minval = 1, group = g_sty)
// }
 
// CALCULATIONS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
type Plvl
    float lvl
    float dlta
    bool  supp
    line  ln
 
var parr = array.new<Plvl>()
 
lnStyle = switch lopt
    "Dotted" => line.style_dotted
    "Dashed" => line.style_dashed
    => line.style_solid
 
// Volume delta over the whole pivot window (close position within each bar's range)
pivotDelta() =>
    float d = 0.0
    for i = 0 to (lbars + rbars)
        float rng = high[i] - low[i]
        d += rng > 0 ? volume[i] * (((close[i] - low[i]) / rng) * 2 - 1) : (close[i] >= open[i] ? volume[i] : -volume[i])
    d
 
float ph   = ta.pivothigh(high, lbars, rbars)
float pl   = ta.pivotlow(low, lbars, rbars)
float dNow = pivotDelta()
 
// 1) BREAKS: a level is broken when a bar CLOSES through it. Line stops there (not deleted).
if barstate.isconfirmed and array.size(parr) > 0
    for i = array.size(parr) - 1 to 0
        Plvl l = array.get(parr, i)
        if (l.supp and close <= l.lvl) or (not l.supp and close >= l.lvl)
            if not na(l.ln)
                line.set_x2(l.ln, bar_index)
            array.remove(parr, i)
 
// 2) NEW POINTS: delta % is measured against the biggest delta among active levels AT THIS BAR
float maxD = math.abs(dNow)
for l in parr
    maxD := math.max(maxD, math.abs(l.dlta))
maxD := math.max(maxD, 0.0001)
float pct = math.abs(dNow) / maxD * 100.0
 
bool supportPoint    = barstate.isconfirmed and not na(pl) and pct >= mdpct
bool resistancePoint = barstate.isconfirmed and not na(ph) and pct >= mdpct
 
addLevel(float price, bool isSupp) =>
    color c  = isSupp ? cbull : cbear
    line  ln = showLn ? line.new(bar_index - rbars, price, bar_index, price, color = color.new(c, 30), width = lwidth, style = lnStyle) : na
    array.push(parr, Plvl.new(price, dNow, isSupp, ln))
    string tip = (isSupp ? "Support " : "Resistance ") + str.tostring(price, format.mintick) + "\nDelta: " + str.tostring(dNow, format.volume) + "\nStrength: " + str.tostring(pct, "#.#") + "%"
    label.new(bar_index, price, "", xloc = xloc.bar_index, yloc = yloc.price, color = c,
         style = isSupp ? label.style_triangleup : label.style_triangledown, size = size.tiny, tooltip = tip)
 
if supportPoint
    addLevel(pl, true)
if resistancePoint
    addLevel(ph, false)
 
// 3) CAPACITY: keep only the newest `mxlvl` levels active; older ones simply stop extending
while array.size(parr) > mxlvl
    array.shift(parr)
 
// 4) EXTEND active lines to the current bar
for l in parr
    if not na(l.ln)
        line.set_x2(l.ln, bar_index)
// }
 
// STRATEGY BUILDER OUTPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――{
// 1 on the bar the triangle appears, 0 otherwise. Same bar as the triangle on the chart.
plot(supportPoint    ? 1 : 0, "Support Point",    color = cbull, display = display.data_window)
plot(resistancePoint ? 1 : 0, "Resistance Point", color = cbear, display = display.data_window)
 
alertcondition(supportPoint,    "Support Point",    "New support point on {{ticker}} at {{close}}")
alertcondition(resistancePoint, "Resistance Point", "New resistance point on {{ticker}} at {{close}}")
// }

← 返回列表