Indicator · Pine Script v6 · Own pane

Detrended CVD Z-Score

by tthanhvu002sgu· code credited to Vu

A Pine v6 separate-pane indicator that estimates delta from candle direction and volume, accumulates it and turns it into a Z-score over a rolling window, shown as a coloured histogram with a signal EMA and standard deviation lines.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code192
Settings9inputs
LicenceNone statedfile: MPL-2.0
Last update3 months ago14 Jul 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

192 lines of Pine Script v6 from indicator/Detrended CVD.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Detrended CVD.pine
// This Pine Script™ v6 indicator is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// © Vu
// Converted from MQ5: Detrended CVD (Z-Score Oscillator) + Divergence Detection

//@version=6
indicator("Detrended CVD Z-Score", shorttitle="DCVD Z", overlay=false, precision=2, max_lines_count=500, max_labels_count=500)

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// === INPUTS ===
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
length      = input.int(100, "Window Size", minval=2, group="Z-SCORE CONFIG")
limitStd    = input.float(2.0, "Standard Deviation Limit", step=0.1, group="Z-SCORE CONFIG")
signalEma   = input.int(9, "Signal Line EMA Period", minval=1, group="Z-SCORE CONFIG")
useRealVol  = input.bool(false, "Use Real Volume (False = Tick Volume)", group="Z-SCORE CONFIG")

// Divergence inputs
group_div    = "=== DIVERGENCE ==="
pivotLen     = input.int(5, "Pivot Lookback", minval=1, maxval=20, group=group_div)
showBullDiv  = input.bool(true, "Show Bullish Divergence", group=group_div)
showBearDiv  = input.bool(true, "Show Bearish Divergence", group=group_div)
showHiddenDiv = input.bool(false, "Show Hidden Divergence", group=group_div)
divLookback  = input.int(60, "Max Bars Between Pivots", minval=10, maxval=200, group=group_div)

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// === 1. Raw Delta ===
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
vol = useRealVol ? volume : volume
tick_delta = close > open ? vol : close < open ? -vol : 0.0

// === 2. Cumulative CVD (continuous, never resets) ===
rawCVD = ta.cum(tick_delta)

// === 3. Z-Score (Sliding Window SMA + Population StDev) ===
meanCVD = ta.sma(rawCVD, length)
variance = ta.sma(math.pow(rawCVD - meanCVD, 2), length)
stdCVD   = math.sqrt(variance)
zScore = stdCVD > 0 ? (rawCVD - meanCVD) / stdCVD : 0.0

// === 4. Signal Line (EMA of Z-Score) ===
signalLine = ta.ema(zScore, signalEma)

// === 5. Color Mapping ===
zColor = zScore > limitStd  ? color.lime   :
         zScore < -limitStd ? color.red    :
         zScore > 0         ? color.teal   :
                              color.maroon

// === PLOTS ===
plot(zScore, "Z-Score CVD", color=zColor, style=plot.style_histogram, linewidth=2)
plot(signalLine, "Signal Line", color=color.new(#FF8C00, 0), linewidth=1)

// === LEVELS ===
hline(0.0,       "Zero",       color=color.gray, linestyle=hline.style_dotted)
hline(limitStd,  "Upper Std",  color=color.gray, linestyle=hline.style_dotted)
hline(-limitStd, "Lower Std",  color=color.gray, linestyle=hline.style_dotted)

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// === 6. DIVERGENCE DETECTION ===
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// --- Detect Pivot Highs & Lows ---
// Trên giá (close) và trên chỉ báo (zScore)
// ta.pivothigh/low trả về giá trị tại pivot, hoặc na nếu không phải pivot
// Pivot xác nhận sau pivotLen bars (có offset)

osc = zScore  // Alias cho dễ đọc

// Pivot trên chỉ báo
osc_ph = ta.pivothigh(osc, pivotLen, pivotLen)
osc_pl = ta.pivotlow(osc, pivotLen, pivotLen)

// Pivot trên giá close
price_ph = ta.pivothigh(close, pivotLen, pivotLen)
price_pl = ta.pivotlow(close, pivotLen, pivotLen)

// --- Lưu vị trí và giá trị pivot gần nhất ---
// Khi phát hiện pivot mới, lưu lại pivot trước đó để so sánh

// == PIVOT LOW (cho Bullish Divergence) ==
var int   prev_pl_bar   = na    // Bar index pivot low trước
var float prev_pl_price = na    // Giá tại pivot low trước
var float prev_pl_osc   = na    // Oscillator tại pivot low trước

var int   curr_pl_bar   = na    // Bar index pivot low hiện tại
var float curr_pl_price = na
var float curr_pl_osc   = na

// == PIVOT HIGH (cho Bearish Divergence) ==
var int   prev_ph_bar   = na
var float prev_ph_price = na
var float prev_ph_osc   = na

var int   curr_ph_bar   = na
var float curr_ph_price = na
var float curr_ph_osc   = na

// --- Cập nhật Pivot Low ---
if not na(osc_pl)
    // Dịch pivot hiện tại thành pivot trước
    prev_pl_bar   := curr_pl_bar
    prev_pl_price := curr_pl_price
    prev_pl_osc   := curr_pl_osc
    // Ghi nhận pivot mới (offset pivotLen bars về trước)
    curr_pl_bar   := bar_index - pivotLen
    curr_pl_price := close[pivotLen]
    curr_pl_osc   := osc[pivotLen]

// --- Cập nhật Pivot High ---
if not na(osc_ph)
    prev_ph_bar   := curr_ph_bar
    prev_ph_price := curr_ph_price
    prev_ph_osc   := curr_ph_osc
    curr_ph_bar   := bar_index - pivotLen
    curr_ph_price := close[pivotLen]
    curr_ph_osc   := osc[pivotLen]

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// === 7. KIỂM TRA VÀ VẼ DIVERGENCE ===
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// Bullish Divergence (Regular):
//   Giá tạo đáy THẤP HƠN, chỉ báo tạo đáy CAO HƠN → tín hiệu đảo chiều tăng
//
// Bearish Divergence (Regular):
//   Giá tạo đỉnh CAO HƠN, chỉ báo tạo đỉnh THẤP HƠN → tín hiệu đảo chiều giảm
//
// Hidden Bullish:
//   Giá tạo đáy CAO HƠN, chỉ báo tạo đáy THẤP HƠN → continuation tăng
//
// Hidden Bearish:
//   Giá tạo đỉnh THẤP HƠN, chỉ báo tạo đỉnh CAO HƠN → continuation giảm

// Màu sắc
color bullColor     = color.new(#00E676, 0)    // Xanh lá sáng
color bearColor     = color.new(#FF1744, 0)    // Đỏ sáng
color hidBullColor  = color.new(#00E676, 50)   // Xanh lá nhạt
color hidBearColor  = color.new(#FF1744, 50)   // Đỏ nhạt

// --- Regular Bullish Divergence (tại Pivot Low) ---
if not na(osc_pl) and showBullDiv
    if not na(prev_pl_bar) and not na(curr_pl_bar)
        barsBack = curr_pl_bar - prev_pl_bar
        if barsBack > 0 and barsBack <= divLookback
            // Regular Bullish: Giá lower low + Osc higher low
            if curr_pl_price < prev_pl_price and curr_pl_osc > prev_pl_osc
                // Vẽ đường nối 2 đáy trên cửa sổ indicator
                line.new(prev_pl_bar, prev_pl_osc, curr_pl_bar, curr_pl_osc,
                     color=bullColor, width=2, style=line.style_solid)
                // Label tại pivot hiện tại
                label.new(curr_pl_bar, curr_pl_osc, "Bull",
                     color=color.new(bullColor, 80), textcolor=bullColor,
                     style=label.style_label_up, size=size.tiny)

// --- Regular Bearish Divergence (tại Pivot High) ---
if not na(osc_ph) and showBearDiv
    if not na(prev_ph_bar) and not na(curr_ph_bar)
        barsBack = curr_ph_bar - prev_ph_bar
        if barsBack > 0 and barsBack <= divLookback
            // Regular Bearish: Giá higher high + Osc lower high
            if curr_ph_price > prev_ph_price and curr_ph_osc < prev_ph_osc
                line.new(prev_ph_bar, prev_ph_osc, curr_ph_bar, curr_ph_osc,
                     color=bearColor, width=2, style=line.style_solid)
                label.new(curr_ph_bar, curr_ph_osc, "Bear",
                     color=color.new(bearColor, 80), textcolor=bearColor,
                     style=label.style_label_down, size=size.tiny)

// --- Hidden Bullish Divergence (tại Pivot Low) ---
if not na(osc_pl) and showHiddenDiv
    if not na(prev_pl_bar) and not na(curr_pl_bar)
        barsBack = curr_pl_bar - prev_pl_bar
        if barsBack > 0 and barsBack <= divLookback
            // Hidden Bullish: Giá higher low + Osc lower low
            if curr_pl_price > prev_pl_price and curr_pl_osc < prev_pl_osc
                line.new(prev_pl_bar, prev_pl_osc, curr_pl_bar, curr_pl_osc,
                     color=hidBullColor, width=2, style=line.style_dashed)
                label.new(curr_pl_bar, curr_pl_osc, "H.Bull",
                     color=color.new(hidBullColor, 80), textcolor=hidBullColor,
                     style=label.style_label_up, size=size.tiny)

// --- Hidden Bearish Divergence (tại Pivot High) ---
if not na(osc_ph) and showHiddenDiv
    if not na(prev_ph_bar) and not na(curr_ph_bar)
        barsBack = curr_ph_bar - prev_ph_bar
        if barsBack > 0 and barsBack <= divLookback
            // Hidden Bearish: Giá lower high + Osc higher high
            if curr_ph_price < prev_ph_price and curr_ph_osc > prev_ph_osc
                line.new(prev_ph_bar, prev_ph_osc, curr_ph_bar, curr_ph_osc,
                     color=hidBearColor, width=2, style=line.style_dashed)
                label.new(curr_ph_bar, curr_ph_osc, "H.Bear",
                     color=color.new(hidBearColor, 80), textcolor=hidBearColor,
                     style=label.style_label_down, size=size.tiny)

Overview

Detrended CVD Z-Score adds divergence detection between price and the Z-score using pivots, with toggles for bullish, bearish and hidden types and a maximum distance between pivots. Divergences confirm after the pivot bars. Delta is estimated, not real order flow.

Details

Published by

tthanhvu002sgu on GitHub

Code credit

Vu, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Volume, Divergence, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Window Sizeint100length
Standard Deviation Limitfloat2.0limitStd
Signal Line EMA Periodint9signalEma
Use Real Volume (False = Tick Volume)boolfalseuseRealVol
Pivot Lookbackint5pivotLen
Show Bullish DivergencebooltrueshowBullDiv
Show Bearish DivergencebooltrueshowBearDiv
Show Hidden DivergenceboolfalseshowHiddenDiv
Max Bars Between Pivotsint60divLookback
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Oscillator Suite

by TamaraKGreen
46 stars

Oscillator Suite plots a normalised price oscillator in a separate pane and overlays a signal line that can use SMA, EMA, WMA, HMA, RMA, VWMA, T3, ZEMA, KAMA, or GKEMA. It marks bullish and bearish divergences, a smoothed money-flow series, confluence areas an

Indicator v5 Oscillators & momentumDivergence
Updated 7 weeks ago · None stated GitHub repository of Oscillator Suite

MFI Divergences

by tistaharahap
25 stars

This indicator plots the Money Flow Index in a separate pane and marks regular and hidden bullish and bearish divergences between price pivots and the oscillator. It builds the index from positive and negative money flow, then finds pivot highs and lows using

Indicator v4 Oscillators & momentumVolume
Updated 6 years ago · None stated GitHub repository of MFI Divergences
11 stars

This indicator plots a smoothed Z-score of cumulative volume delta in a separate pane, showing whether buying or selling pressure is dominant and when the reading enters extreme zones. It marks spot accumulation, climax or exhaustion, and pivot-based price-vol

Indicator v6 Oscillators & momentumVolume Repaints
Updated 7 weeks ago · MIT GitHub repository of CVD Z-Score [NeuNue]
9 stars

This indicator plots a heuristic volume-balance oscillator in a separate pane. It estimates signed volume from the close location inside each bar, multiplies that by volume, and sums the proxy over a rolling window. The sum is scaled between minus one and plus

Indicator v6 VolumeOscillators & momentum
Updated 2 days ago · MIT GitHub repository of CVD Bias [WavesUnchained]

DC-B v2.5

by crissrocca
9 stars

This indicator is an oscillator suite inspired by Market Cipher B and VuManChu Cipher B that plots in a separate pane. It combines a WaveTrend oscillator, a Money Flow Index (MFI) area, and an RSI indicator with automatic divergence detection. Visual signals h

Indicator v4 Oscillators & momentumDivergence
Updated 3 years ago · MPL-2.0 GitHub repository of DC-B v2.5

QPulse

by JasonTeixeira
9 stars

QPulse is a multi-factor indicator that draws in a separate pane to evaluate market momentum, volume flow, and volatility dynamics. It calculates volume delta, a cumulative delta proxy, advanced adaptive ATR, and a hybrid volume-weighted RSI with overbought an

Indicator v6 Oscillators & momentumVolatility & bands
Updated 13 days ago · Custom licence GitHub repository of QPulse

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers