Delta and CVD

Alpha_Wizard · study · 181 行 · 点赞 48 · 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/
// © Alpha_Wizard
 
//@version=6
 
indicator("Delta and CVD", shorttitle = "Delta and CVD", overlay = false, format = format.volume, max_polylines_count = 20, max_labels_count = 20, max_lines_count = 20)
 
const string G_FLOW  = "Order flow"
const string G_CVD   = "CVD"
const string G_STYLE = "Style"
 
string deltaSrc = input.string("Intrabar", "Delta source", options = ["Intrabar", "Candle geometry"], group = G_FLOW)
string ltfIn    = input.string("Auto", "Intrabar timeframe", options = ["Auto", "1", "3", "5", "15", "60"], group = G_FLOW)
string anchor   = input.string("Visible range", "CVD anchor", options = ["Visible range", "Session", "Continuous"], group = G_CVD)
string sessTfIn = input.timeframe("D", "Session", group = G_CVD)
bool   showCvd  = input.bool(true, "Show CVD line", group = G_CVD, inline = "c")
bool   glowOn   = input.bool(true, "Glow", group = G_CVD, inline = "c")
int    maxStore = input.int(3000, "Bars kept in memory", minval = 500, maxval = 5000, group = G_CVD)
color  cUp   = input.color(#00FFD1, "Buy", group = G_STYLE, inline = "k")
color  cDn   = input.color(#FF3B5C, "Sell", group = G_STYLE, inline = "k")
color  cGold = input.color(#FFD23F, "CVD", group = G_STYLE, inline = "k")
 
const color C_GRID  = #8092AA
const color C_MUTED = #738396
const color C_DARK  = #1A1405
 
// ───────────────────────────── Delta ─────────────────────────────────────
int    tfSec   = timeframe.in_seconds()
string ltfAuto = tfSec <= 300 ? "1" : tfSec <= 3600 ? "5" : tfSec <= 14400 ? "15" : "60"
string ltf     = ltfIn == "Auto" ? ltfAuto : ltfIn
[ltfO, ltfC, ltfV] = request.security_lower_tf(syminfo.tickerid, ltf, [open, close, volume], ignore_invalid_timeframe = true)
bool newSess = timeframe.change(sessTfIn)
 
f_intrabar(array<float> ao, array<float> ac, array<float> av) =>
    float b = 0.0
    float t = 0.0
    float prevDir = 0.0
    int n = na(av) ? 0 : array.size(av)
    if n > 0
        for i = 0 to n - 1
            float vi = nz(array.get(av, i))
            float oi = array.get(ao, i)
            float ci = array.get(ac, i)
            float d = ci > oi ? 1.0 : ci < oi ? -1.0 : prevDir
            if d > 0
                b := b + vi
            else if d == 0
                b := b + vi * 0.5
            prevDir := d
            t := t + vi
    [b, t]
 
f_v(float v) =>
    float a = math.abs(v)
    string sg = v < 0 ? "-" : ""
    string out = a >= 1e9 ? sg + str.tostring(a / 1e9, "0.00") + "B" : a >= 1e6 ? sg + str.tostring(a / 1e6, "0.00") + "M" : a >= 1e3 ? sg + str.tostring(a / 1e3, a >= 1e5 ? "0" : "0.0") + "K" : sg + str.tostring(a, a < 10 ? "0.00" : "0")
    out
 
f_sv(float v) =>
    string sg = v > 0 ? "+" : ""
    sg + f_v(v)
 
[ibBuy, ibTot] = f_intrabar(ltfO, ltfC, ltfV)
float geoFrac = high > low ? (close - low) / (high - low) : 0.5
float buyFrac = deltaSrc == "Intrabar" and ibTot > 0 ? ibBuy / ibTot : geoFrac
float vol0    = nz(volume)
float delta   = vol0 * (2 * buyFrac - 1)
 
// Delta columns
color dCol = delta >= 0 ? color.new(cUp, 58) : color.new(cDn, 55)
plot(delta, "Delta", color = dCol, style = plot.style_columns, histbase = 0)
hline(0, "Zero", color = color.new(C_GRID, 80), linestyle = hline.style_solid)
 
// Running CVD for the status line and data window
int leftT = chart.left_visible_bar_time
var float cvdRun = 0.0
bool resetNow = anchor == "Visible range" ? time == leftT : anchor == "Session" ? newSess : false
if resetNow
    cvdRun := 0.0
cvdRun := cvdRun + delta
float cvdShown = anchor == "Visible range" and time < leftT ? na : cvdRun
plot(cvdShown, "CVD", color = cGold, display = display.status_line + display.data_window)
 
// ───────────────────────────── Storage ────────────────────────────────────
var array<int>   aI = array.new<int>()
var array<int>   aT = array.new<int>()
var array<float> aD = array.new<float>()
var array<bool>  aS = array.new<bool>()
array.push(aI, bar_index)
array.push(aT, time)
array.push(aD, delta)
array.push(aS, newSess)
if array.size(aI) > maxStore
    array.shift(aI)
    array.shift(aT)
    array.shift(aD)
    array.shift(aS)
 
var array<polyline> gPolys  = array.new<polyline>()
var array<label>    gLabels = array.new<label>()
 
// ───────────────────────────── Redraw ─────────────────────────────────────
if barstate.islast and showCvd
    int np = array.size(gPolys)
    if np > 0
        for i = 0 to np - 1
            polyline.delete(array.get(gPolys, i))
    array.clear(gPolys)
    int nl = array.size(gLabels)
    if nl > 0
        for i = 0 to nl - 1
            label.delete(array.get(gLabels, i))
    array.clear(gLabels)
 
    int n = array.size(aT)
    int rightT = chart.right_visible_bar_time
    int i0 = -1
    int i1 = -1
    for i = 0 to n - 1
        int ti = array.get(aT, i)
        if ti >= leftT and ti <= rightT
            if i0 < 0
                i0 := i
            i1 := i
    if i0 < 0
        i0 := int(math.max(0, n - 150))
        i1 := n - 1
 
    // CVD across the visible window with the chosen anchor
    array<float> cv = array.new<float>()
    float run  = 0.0
    float cmin = na
    float cmax = na
    float mad  = 0.0
    if anchor == "Continuous" and i0 > 0
        for i = 0 to i0 - 1
            run := run + array.get(aD, i)
    if anchor == "Session"
        int sStart = 0
        for i = 0 to i0
            if array.get(aS, i)
                sStart := i
        if sStart < i0
            for i = sStart to i0 - 1
                run := run + array.get(aD, i)
    for i = i0 to i1
        if anchor == "Session" and array.get(aS, i) and i > i0
            run := 0.0
        float d = array.get(aD, i)
        run  := run + d
        mad  := math.max(mad, math.abs(d))
        cmin := na(cmin) ? run : math.min(cmin, run)
        cmax := na(cmax) ? run : math.max(cmax, run)
        array.push(cv, run)
    if cmax - cmin < 1e-9
        cmax := cmax + 1
        cmin := cmin - 1
    mad := math.max(mad, 1e-9)
 
    // Map CVD into the delta scale: full pane height like the desk
    float span = mad * 0.96
    array<chart.point> pts = array.new<chart.point>()
    int m = array.size(cv)
    if m > 0
        for j = 0 to m - 1
            float y = (array.get(cv, j) - cmin) / (cmax - cmin) * 2 * span - span
            array.push(pts, chart.point.from_index(array.get(aI, i0 + j), y))
    if glowOn
        array.push(gPolys, polyline.new(pts, line_color = color.new(cGold, 90), line_width = 9))
        array.push(gPolys, polyline.new(pts, line_color = color.new(cGold, 78), line_width = 6))
        array.push(gPolys, polyline.new(pts, line_color = color.new(cGold, 52), line_width = 3))
    array.push(gPolys, polyline.new(pts, line_color = cGold, line_width = 1))
 
    // Axis-style tags: last CVD, visible max and min
    if m > 0
        float lastC = array.get(cv, m - 1)
        float yLast = (lastC - cmin) / (cmax - cmin) * 2 * span - span
        int   xTag  = bar_index + 2
        array.push(gLabels, label.new(xTag, yLast, f_sv(lastC), color = cGold, textcolor = C_DARK, style = label.style_label_left, size = size.tiny, text_font_family = font.family_monospace))
        array.push(gLabels, label.new(xTag, span, f_v(cmax), color = color.new(cGold, 100), textcolor = C_MUTED, style = label.style_label_left, size = size.tiny, text_font_family = font.family_monospace))
        array.push(gLabels, label.new(xTag, -span, f_v(cmin), color = color.new(cGold, 100), textcolor = C_MUTED, style = label.style_label_left, size = size.tiny, text_font_family = font.family_monospace))

← 返回列表