// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © arleay //@version=6 indicator("CVD Divergence+", shorttitle = "CVDD+ | @arleay", overlay = false, format = format.volume, max_lines_count = 500, max_labels_count = 500) const string HANDLE = "@arle" + "ay" + "nt" const string MARK = "‍​‌‌​​​​‌​‌‌‌​​‌​​‌‌​‌‌​​​‌‌​​‌​‌​‌‌​​​​‌​‌‌‌‌​​‌‍" const string CR = "free script by " + HANDLE + " · if you paid for this, you were scammed :(" + MARK const string G_C = "CVD" const string G_V = "Divergence" const string G_D = "Display" const string G_S = "Style" const string G_I = "Panel" bool iUseLtf = input.bool(true, "Estimate delta from 1m bars", group = G_C, tooltip = "each 1 minute bar inside the chart bar counts as buying if it closed up and selling if it closed down. off, or where 1m history runs out: the chart bar itself is used.") string iReset = input.string("Session", "Reset", options = ["Session", "Week", "None"], group = G_C, tooltip = "when the running total goes back to zero. with a reset on, both pivots of a divergence must sit in the same period.") bool iReg = input.bool(true, "Regular", inline = "v1", group = G_V, tooltip = "bearish: price higher high, cvd lower high. bullish: price lower low, cvd higher low.") bool iHid = input.bool(false, "Hidden", inline = "v1", group = G_V, tooltip = "bearish: price lower high, cvd higher high. bullish: price higher low, cvd lower low.") int iL = input.int(5, "Pivot bars left", minval = 1, maxval = 50, group = G_V) int iR = input.int(3, "Pivot bars right", minval = 1, maxval = 50, group = G_V, tooltip = "a pivot is only known this many bars after it prints, so marks and alerts arrive that late.") int iMaxBars = input.int(80, "Max bars between pivots", minval = 5, maxval = 500, group = G_V) int iMinTicks = input.int(4, "Min price difference (ticks)", minval = 0, group = G_V) float iMinD = input.float(1.0, "Min CVD difference (x average bar delta)", minval = 0.0, step = 0.25, group = G_V, tooltip = "the two cvd readings must differ by at least this multiple of the 50 bar average of absolute bar delta.") string iMode = input.string("Minimal", "View", options = ["Minimal", "Full"], group = G_D, tooltip = "minimal removes a divergence once price trades past its second pivot. full keeps it, faded.") int iMax = input.int(12, "Max visible", minval = 1, maxval = 100, group = G_D, tooltip = "display cap. the oldest divergence is removed whole.") bool iPriceLn = input.bool(true, "Lines on price", inline = "d1", group = G_D) bool iPaneLn = input.bool(true, "Lines on CVD", inline = "d1", group = G_D) bool iTags = input.bool(true, "Tags", inline = "d2", group = G_D) bool iNums = input.bool(false, "CVD change in tag", inline = "d2", group = G_D) bool iBars = input.bool(false, "Bar delta columns", group = G_D) color cLine = input.color(#000000, "Lines and tags", group = G_S) string iSty = input.string("Dotted", "Regular", options = ["Solid", "Dashed", "Dotted"], inline = "s1", group = G_S) string iStyH = input.string("Dashed", "Hidden", options = ["Solid", "Dashed", "Dotted"], inline = "s1", group = G_S) int iW = input.int(1, "Line width", minval = 1, maxval = 4, group = G_S) string tSz = input.string("Tiny", "Tag size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_S) color cCvd = input.color(#000000, "CVD line", group = G_S) color cUp = input.color(#787B86, "Delta up", inline = "s2", group = G_S) color cDn = input.color(#363A45, "Delta down", inline = "s2", group = G_S) bool iInfo = input.bool(true, "Show", inline = "i1", group = G_I) bool iCompact = input.bool(false, "Compact", inline = "i1", group = G_I) string iInfoPos = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_I) f_sz(string s) => switch s "Huge" => size.huge "Large" => size.large "Normal" => size.normal "Small" => size.small => size.tiny f_ls(string s) => switch s "Solid" => line.style_solid "Dashed" => line.style_dashed => line.style_dotted f_pos(string s) => switch s "Top Right" => position.top_right "Bottom Right" => position.bottom_right "Top Left" => position.top_left => position.bottom_left f_delta() => float v = nz(volume) float pc = nz(close[1], close) close > open ? v : close < open ? -v : close > pc ? v : close < pc ? -v : 0.0 type Div line lp line lc label lb bool bear float lvl bool dead = false bool full = iMode == "Full" string ltf = timeframe.in_seconds() > 60 ? "1" : timeframe.period float chartD = f_delta() array dA = request.security_lower_tf(syminfo.tickerid, ltf, f_delta()) bool ltfOk = iUseLtf and dA.size() > 0 float delta = ltfOk ? dA.sum() : chartD int td = time_tradingday bool rst = iReset == "Session" ? td != td[1] : iReset == "Week" ? timeframe.change("W") : false var float cvd = 0.0 var int pid = 0 if rst cvd := 0.0 pid += 1 cvd += delta float avgAbs = ta.sma(math.abs(delta), 50) float ph = ta.pivothigh(high, iL, iR) float pl = ta.pivotlow(low, iL, iR) float cvdP = cvd[iR] int pidP = pid[iR] int barP = bar_index - iR var array
divs = array.new
() var float hP = na var float hC = na var int hB = na var int hId = na var float lP = na var float lC = na var int lB = na var int lId = na var string lastTag = "none" var int lastBar = na f_del(Div dv) => line.delete(dv.lp) line.delete(dv.lc) label.delete(dv.lb) f_mk(bool bear, bool hid, int b1, float p1, float c1, int b2, float p2, float c2) => string sty = f_ls(hid ? iStyH : iSty) string nm = (hid ? "hCVD" : "CVD") + (bear ? "-" : "+") string chg = str.tostring(c1, format.volume) + " to " + str.tostring(c2, format.volume) string what = bear ? (hid ? "price lower high, cvd higher high" : "price higher high, cvd lower high") : (hid ? "price higher low, cvd lower low" : "price lower low, cvd higher low") string tip = what + "\nprice " + str.tostring(p1, format.mintick) + " to " + str.tostring(p2, format.mintick) + "\ncvd " + chg + "\n" + CR line lp = iPriceLn ? line.new(b1, p1, b2, p2, color = cLine, style = sty, width = iW, force_overlay = true) : na line lc = iPaneLn ? line.new(b1, c1, b2, c2, color = cLine, style = sty, width = iW) : na label lb = na if iTags lb := label.new(b2, p2, nm + (iNums ? " " + chg : ""), style = bear ? label.style_label_down : label.style_label_up, color = color.new(cLine, 100), textcolor = cLine, size = f_sz(tSz), text_font_family = font.family_monospace, tooltip = tip, force_overlay = true) divs.push(Div.new(lp, lc, lb, bear, p2)) if divs.size() > iMax f_del(divs.shift()) nm bool sigBear = false bool sigBull = false if barstate.isconfirmed if divs.size() > 0 for i = divs.size() - 1 to 0 Div dv = divs.get(i) if not dv.dead and (dv.bear ? high > dv.lvl : low < dv.lvl) dv.dead := true if full line.set_color(dv.lp, color.new(cLine, 65)) line.set_color(dv.lc, color.new(cLine, 65)) label.set_textcolor(dv.lb, color.new(cLine, 65)) if not full f_del(dv) divs.remove(i) if not na(ph) if not na(hP) float dp = ph - hP float dc = cvdP - hC bool span = barP - hB <= iMaxBars and (iReset == "None" or pidP == hId) bool big = math.abs(dp) >= iMinTicks * syminfo.mintick and math.abs(dc) >= iMinD * nz(avgAbs) if span and big if iReg and dp > 0 and dc < 0 lastTag := f_mk(true, false, hB, hP, hC, barP, ph, cvdP) lastBar := bar_index sigBear := true else if iHid and dp < 0 and dc > 0 lastTag := f_mk(true, true, hB, hP, hC, barP, ph, cvdP) lastBar := bar_index sigBear := true hP := ph hC := cvdP hB := barP hId := pidP if not na(pl) if not na(lP) float dp = pl - lP float dc = cvdP - lC bool span = barP - lB <= iMaxBars and (iReset == "None" or pidP == lId) bool big = math.abs(dp) >= iMinTicks * syminfo.mintick and math.abs(dc) >= iMinD * nz(avgAbs) if span and big if iReg and dp < 0 and dc > 0 lastTag := f_mk(false, false, lB, lP, lC, barP, pl, cvdP) lastBar := bar_index sigBull := true else if iHid and dp > 0 and dc < 0 lastTag := f_mk(false, true, lB, lP, lC, barP, pl, cvdP) lastBar := bar_index sigBull := true lP := pl lC := cvdP lB := barP lId := pidP hline(0, "Zero", color = color.new(#787B86, 40), linestyle = hline.style_dotted) plot(iBars ? delta : na, "Bar delta", color = color.new(delta >= 0 ? cUp : cDn, 55), style = plot.style_columns) plot(cvd, "CVD", color = cCvd, linewidth = 1) var table info = table.new(f_pos(iInfoPos), 2, 6, bgcolor = color.new(chart.bg_color, 6), frame_width = 0, border_color = color.new(chart.bg_color, 6), border_width = 3) f_cell(int r, string k, string v, string tip) => table.cell(info, 0, r, k, text_color = color.new(chart.fg_color, 45), text_size = f_sz(tSz), text_halign = text.align_left, text_font_family = font.family_monospace) table.cell(info, 1, r, v, text_color = chart.fg_color, bgcolor = color.new(#787B86, 82), text_size = f_sz(tSz), text_font_family = font.family_monospace, tooltip = tip) if barstate.islast and iInfo table.clear(info, 0, 0, 1, 5) table.cell(info, 0, 0, "CVD Divergence+" + MARK, text_color = chart.fg_color, text_size = f_sz(tSz), text_halign = text.align_left, text_formatting = text.format_bold, text_font_family = font.family_monospace) table.merge_cells(info, 0, 0, 1, 0) f_cell(1, "CVD", str.tostring(cvd, format.volume), "running total since the last reset") f_cell(2, "Last", lastTag + (na(lastBar) ? "" : " · " + str.tostring(bar_index - lastBar) + " bars"), "most recent divergence and how many bars ago it was confirmed") int r = 3 if not iCompact f_cell(3, "Bar", str.tostring(delta, format.volume), "delta estimate of the current bar") f_cell(4, "Data", nz(volume) == 0 and cvd == 0 ? "no volume" : ltfOk ? "1m bars" : "chart bars", "what the delta estimate is built from on this bar") r := 5 table.cell(info, 0, r, "free · " + HANDLE + " on X", text_color = color.new(chart.fg_color, 65), text_size = f_sz(tSz), text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = CR) table.merge_cells(info, 0, r, 1, r) alertcondition(sigBear, "CVD Divergence — Bearish", "Bearish CVD divergence confirmed on {{ticker}} at {{close}} · " + CR) alertcondition(sigBull, "CVD Divergence — Bullish", "Bullish CVD divergence confirmed on {{ticker}} at {{close}} · " + CR)