CVD Momentum Indicator

iO_FX · study · 70 行 · 点赞 22 · TradingView 原页

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

Pine Script

//@version=6
indicator("CVD Momentum Indicator", shorttitle="CVD Mom", precision=2)
 
import TradingView/ta/12
 
// --- Official CVD Settings ---
anchorInput = input.timeframe("1D", "Anchor period", group="CVD Settings")
dataAggregationTooltip = "Defines how lower timeframe data is aggregated for the delta calculation. \"1 tick\" computes the delta on tick data and is available only on expert plans."
dataAggregationInput = input.string("Time", "Data aggregation", options = ["Time", "1 tick"], tooltip = dataAggregationTooltip, group="CVD Settings")
useTicks = dataAggregationInput == "1 tick"
lowerTimeframeTooltip = "The indicator scans lower timeframe data to approximate up and down volume used in the delta calculation. By default, the timeframe is chosen automatically. These inputs override this with a custom timeframe.\n\nHigher timeframes provide more historical data, but the data will be less precise."
useCustomTimeframeInput = input.bool(false, "Use custom timeframe", tooltip = lowerTimeframeTooltip, active = not useTicks, group="CVD Settings")
lowerTimeframeInput = input.timeframe("1", "Timeframe", active = useCustomTimeframeInput and not useTicks, group="CVD Settings")
 
// --- Momentum Settings ---
i_fastLen = input.int(12, "CVD Fast EMA", group="Momentum Settings")
i_slowLen = input.int(26, "CVD Slow EMA", group="Momentum Settings")
i_sigLen  = input.int(9, "Signal Smoothing", group="Momentum Settings")
i_bbLen   = input.int(20, "Volatility Band Length", group="Momentum Settings")
i_bbMult  = input.float(2.0, "Volatility Band Multiplier", group="Momentum Settings")
 
var lowerTimeframe = switch
    useTicks                => "1T"
    useCustomTimeframeInput => lowerTimeframeInput
    timeframe.isseconds     => "1S"
    timeframe.isintraday    => "1"
    timeframe.isdaily       => "5"
    => "60"
 
// --- Request Official Volume Delta ---
[openVolume, maxVolume, minVolume, lastVolume] = ta.requestVolumeDelta(lowerTimeframe, anchorInput)
 
// Base CVD stream for momentum calculations
float cvd = nz(lastVolume, 0.0)
 
// --- Momentum Oscillators on CVD ---
float cvdFast   = ta.ema(cvd, i_fastLen)
float cvdSlow   = ta.ema(cvd, i_slowLen)
float cvdMom    = cvdFast - cvdSlow
float cvdSignal = ta.ema(cvdMom, i_sigLen)
float cvdHist   = cvdMom - cvdSignal
 
// --- Volatility Bands ---
float cvdBasis  = ta.sma(cvdMom, i_bbLen)
float cvdDev    = i_bbMult * ta.stdev(cvdMom, i_bbLen)
float upperBand = cvdBasis + cvdDev
float lowerBand = cvdBasis - cvdDev
 
// --- Visualizations ---
color histColor = cvdHist >= 0 ? (cvdHist > cvdHist[1] ? #26A69A : #80CBC4) : (cvdHist < cvdHist[1] ? #EF5350 : #EF9A9A)
 
plot(cvdMom, title="CVD Momentum Line", color=#2962FF, linewidth=2)
plot(cvdSignal, title="Signal Line", color=#FF6D00, linewidth=1)
plot(cvdHist, title="Momentum Histogram", color=histColor, style=plot.style_columns)
 
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dashed)
 
p1 = plot(upperBand, title="Upper Band", color=color.new(color.teal, 50))
p2 = plot(lowerBand, title="Lower Band", color=color.new(color.maroon, 50))
fill(p1, p2, title="Band Background", color=color.new(color.teal, 95))
 
// --- Error Handling ---
var cumVol = 0.
cumVol += nz(volume)
deltaIsNa = na(openVolume) and na(maxVolume) and na(minVolume) and na(lastVolume)
if barstate.islast
    if cumVol == 0
        runtime.error("The data vendor doesn't provide volume data for this symbol.")
    if str.endswith(lowerTimeframe, "T") and deltaIsNa
        runtime.error("Not enough tick depth for the current anchor period. The available tick depth ends before reaching the start of the anchor period (" + anchorInput + "). Please shorten the 'Anchor period' input or switch the calculation base from '1 Tick' to 'Time' or higher custom timeframe.")

← 返回列表