本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("CVD Ultimate MG ", format = format.volume)
import TradingView/ta/12
// ==================== INPUTS ====================
showCVD = input.bool(true, "Show CVD", group = "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(true, "Custom timeframe", tooltip = lowerTimeframeTooltip, active = not useTicks, group = "CVD Settings")
lowerTimeframeInput = input.timeframe("", "Timeframe", active = useCustomTimeframeInput and not useTicks, group = "CVD Settings")
// ==================== CVD MOVING AVERAGES ====================
cvdMALength = input.int(15, "CVD EMA Length", minval = 1, group = "CVD Settings")
cvdEMAColor = input.color(color.white, "CVD EMA Color", group = "CVD Settings")
cvdVWAPColor = input.color(color.rgb(242, 4, 190), "CVD VWAP Color", group = "CVD Settings")
// ==================== CVD SESSION LINE SETTINGS ====================
showSessionCVDLine = input.bool(true, "Show First 5-Min CVD Level", group = "CVD Session Line")
bullLineColor = input.color(color.rgb(57, 221, 3), "Bullish Line Color", group = "CVD Session Line")
bearLineColor = input.color(color.rgb(249, 5, 5), "Bearish Line Color", group = "CVD Session Line")
sessionLineWidth = input.int(2, "Line Width", minval = 1, maxval = 5, group = "CVD Session Line")
// ==================== CVD SECTION ====================
var lowerTimeframe = switch
useTicks => "1T"
useCustomTimeframeInput => lowerTimeframeInput
timeframe.isseconds => "1S"
timeframe.isintraday => "1"
timeframe.isdaily => "5"
=> "60"
[openVolume, maxVolume, minVolume, lastVolume] = ta.requestVolumeDelta(lowerTimeframe, anchorInput)
// Color
col = lastVolume >= openVolume ? color.rgb(33, 243, 65) : color.rgb(247, 1, 1)
// Always call plot functions in global scope
hline(0, "Zero", color = color.rgb(244, 247, 247), linestyle = hline.style_dotted, linewidth = 1)
// ==================== CVD PLOT ====================
plotcandle(
showCVD ? openVolume : na,
showCVD ? maxVolume : na,
showCVD ? minVolume : na,
showCVD ? lastVolume : na,
"CVD",
color = col,
bordercolor = col,
wickcolor = col)
// ==================== CVD EMA AND VWAP ====================
// Detect the beginning of a new daily session
newCVDSession = timeframe.change("1D")
// ==================== CVD EMA ====================
// EMA resets at the start of every session
var float cvdSessionEMA = na
emaAlpha = 2.0 / (cvdMALength + 1.0)
if newCVDSession or na(cvdSessionEMA[1])
cvdSessionEMA := lastVolume
else
cvdSessionEMA := na(lastVolume) ? cvdSessionEMA[1] : emaAlpha * lastVolume + (1.0 - emaAlpha) * cvdSessionEMA[1]
// ==================== CVD VWAP ====================
// Session-based volume-weighted average of CVD
var float cvdVWAPNumerator = 0.0
var float cvdVWAPDenominator = 0.0
if newCVDSession
cvdVWAPNumerator := 0.0
cvdVWAPDenominator := 0.0
if not na(lastVolume) and not na(volume)
cvdVWAPNumerator += lastVolume * volume
cvdVWAPDenominator += volume
cvdSessionVWAP = cvdVWAPDenominator > 0 ? cvdVWAPNumerator / cvdVWAPDenominator : na
// ==================== PLOT CVD EMA ====================
plot(
showCVD ? cvdSessionEMA : na,
"CVD EMA",
color = cvdEMAColor,
linewidth = 1)
// ==================== PLOT CVD VWAP ====================
plot(
showCVD ? cvdSessionVWAP : na,
"CVD VWAP",
color = cvdVWAPColor,
linewidth = 2)
// ==================== FIRST 5-MIN CVD SESSION LINE ====================
indiaTZ = "Asia/Kolkata"
isFiveMinuteChart = timeframe.isminutes and timeframe.multiplier == 5
indiaHour = hour(time, indiaTZ)
indiaMinute = minute(time, indiaTZ)
isFirstFiveMinBar = isFiveMinuteChart and indiaHour == 9 and indiaMinute == 15
isIndiaSession = isFiveMinuteChart and (indiaHour > 9 or (indiaHour == 9 and indiaMinute >= 15)) and (indiaHour < 15 or (indiaHour == 15 and indiaMinute < 30))
var line sessionCVDLine = na
if isFirstFiveMinBar
sessionCVDLine := na
bullishFirstCVD = not na(openVolume) and not na(lastVolume) and lastVolume > openVolume and lastVolume > 0
bearishFirstCVD = not na(openVolume) and not na(lastVolume) and lastVolume < openVolume and openVolume <= 0
if showSessionCVDLine
if bullishFirstCVD
sessionCVDLine := line.new(
x1 = bar_index,
y1 = math.max(openVolume, lastVolume),
x2 = bar_index + 1,
y2 = math.max(openVolume, lastVolume),
xloc = xloc.bar_index,
extend = extend.none,
color = bullLineColor,
width = sessionLineWidth)
else if bearishFirstCVD
sessionCVDLine := line.new(
x1 = bar_index,
y1 = math.min(openVolume, lastVolume),
x2 = bar_index + 1,
y2 = math.min(openVolume, lastVolume),
xloc = xloc.bar_index,
extend = extend.none,
color = bearLineColor,
width = sessionLineWidth)
if isIndiaSession and not na(sessionCVDLine)
line.set_x2(sessionCVDLine, bar_index + 1)
// Extend the current session's line through the end of the session.
if isIndiaSession and not na(sessionCVDLine)
line.set_x2(sessionCVDLine, bar_index + 1)
// ==================== RUNTIME CHECKS ====================
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.")
// ==================== VOWI SECTION ====================
showVOWI = input.bool(false, "Show Vol-OWI", group = "VOWI Settings")
smaLength = input.int(20, "SMA Length", group = "VOWI Settings")
// Vol-OWI MA
vowiMALength = input.int(20, "Vol-OWI Moving Average Length", minval = 1, group = "VOWI Settings")
// ==================== VOLUME CALCULATIONS ====================
buyVolume = high == low ? 0.0 : volume * (close - low) / (high - low)
sellVolume = high == low ? 0.0 : volume * (high - close) / (high - low)
// ==================== VOWI VOLUME COLUMNS ====================
plot(
showVOWI ? volume : na,
"SELL V",
color = color.rgb(252, 2, 2),
style = plot.style_columns)
plot(
showVOWI ? buyVolume : na,
"BUY V",
color = color.rgb(5, 247, 13),
style = plot.style_columns)
// ==================== VOL-OWI MOVING AVERAGE ====================
vowiMA = ta.sma(volume, vowiMALength)
plot(
showVOWI ? vowiMA : na,
"Vol-OWI Moving Average",
color = color.rgb(255, 255, 255),
linewidth = 1)