CVD Pro

Zomzi · study · 169 行 · 点赞 60 · TradingView 原页

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

Pine Script

//@version=6
indicator(title = 'CVD Pro', shorttitle = 'CVD Pro', precision = 0)
 
// INPUTS & TOOLTIPS
var string G_MA     = "1. MA Settings"
plotMa              = input.bool(title = 'Plot MA?', defval = true, group = G_MA, tooltip = 'Toggle the visibility of the CVD Moving Average line on the chart.')
periodMa            = input.int(title = 'MA Length', minval = 1, defval = 50, group = G_MA, tooltip = 'Lookback period for the core ALMA smoothing and volatility calculations.')
maOffset            = input.float(title = 'MA Offset', minval = 0, defval = 0.85, step = 0.025, maxval = 1, group = G_MA, tooltip = 'ALMA offset parameter for the main MA line. Higher values increase responsiveness.')
maSigma             = input.float(title = 'MA Sigma', minval = 0, defval = 6.0, step = 0.25, maxval = 7, group = G_MA, tooltip = 'ALMA sigma parameter for the main MA line. Controls the smoothness and focus of the window.')
 
var string G_SMOOTH = "2. CVD Smooth Settings"
smoothLen           = input.int(title = 'Smooth Length', minval = 1, defval = 2, group = G_SMOOTH, tooltip = 'Lookback period used for initial raw CVD pre-smoothing.')
smoothOffset        = input.float(title = 'Smooth Offset', minval = 0, defval = 0.95, step = 0.025, maxval = 1, group = G_SMOOTH, tooltip = 'ALMA offset parameter for the pre-smoothed CVD line.')
smoothSigma         = input.float(title = 'Smooth Sigma', minval = 0, defval = 2.0, step = 0.25, maxval = 7, group = G_SMOOTH, tooltip = 'ALMA sigma parameter for the pre-smoothed CVD line.')
 
var string G_ANOM   = "3. Wyckoff & Mahalanobis"
showAnom            = input.bool(title = 'Include Anomalies', defval = true, group = G_ANOM, tooltip = 'Enable or disable Wyckoff anomaly detection using Mahalanobis distance to adjust the volume profile.')
 
var string G_EVASION = "4. Evasion Zone"
multBand            = input.float(title = 'Evasion Zone Multiplier', defval = 0.02, minval = 0.000, step = 0.005, group = G_EVASION, tooltip = 'Standard deviation multiplier that determines the width of the noise-filtering Evasion Zone.')
 
var string G_DISPLAY = "5. Crossover Dots"
showCross           = input.bool(title = 'Crossover Dots', defval = true, group = G_DISPLAY, tooltip = 'Enable or disable bullish/bearish dot signals generated upon leaving the Evasion Zone extremes.')
 
// ENGINE CORE (ERF, NORMCDF, TANH)
f_tanh(float x) =>
    float ax = math.abs(x)
    float t = math.exp(-2.0 * ax)
    float y = (1.0 - t) / (1.0 + t)
    math.sign(x) * y
 
erf(float x) =>  
    float clampedX = math.max(-4.0, math.min(4.0, x))
    float t = 1.0 / (1.0 + 0.5 * math.abs(clampedX))
    float ans = 1.0 - t * math.exp(-clampedX * clampedX - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 + t * (-0.01862880 + t * (0.02788680 + t * (-1.13520398 + t * (1.48851587 + t * (-0.82215223 + t * 0.17087277)))))))))
    clampedX >= 0.0 ? ans : -ans
 
normCDF(float z) =>
    0.5 * (1.0 + erf(z / math.sqrt(2.0)))
 
// GKYZ HYBRID VOLATILITY CORE
float prev_close = nz(close[1], open)
float effHigh = math.max(high, prev_close)
float effLow = math.max(math.min(low, prev_close), 0.00000001)
 
float GK_COEF = 2.0 * math.log(2.0) - 1.0
float overnightReturn = open > 0.0 and prev_close > 0.0 ? 100.0 * math.log(open / prev_close) : 0.0
float overnightVariance = math.pow(overnightReturn, 2.0)
 
float pct_h_l = effHigh > 0.0 and effLow > 0.0 ? 100.0 * math.log(effHigh / effLow) : 0.0
float pct_c_o = open > 0.0 and close > 0.0 ? 100.0 * math.log(close / open) : 0.0
float gk_core = 0.5 * math.pow(pct_h_l, 2.0) - GK_COEF * math.pow(pct_c_o, 2.0)
 
float pct_h_c = effHigh > 0.0 and close > 0.0 ? 100.0 * math.log(effHigh / close) : 0.0
float pct_h_o = effHigh > 0.0 and open > 0.0 ? 100.0 * math.log(effHigh / open) : 0.0
float pct_l_c = effLow > 0.0 and close > 0.0 ? 100.0 * math.log(effLow / close) : 0.0
float pct_l_o = effLow > 0.0 and open > 0.0 ? 100.0 * math.log(effLow / open) : 0.0
float rs_core = pct_h_c * pct_h_o + pct_l_c * pct_l_o
 
float intradayVariance = (math.max(gk_core, 0.0) + math.max(rs_core, 0.0)) / 2.0
float hybrid_element = (0.5 * overnightVariance) + (0.5 * intradayVariance)
float gkyzMeanVariance = ta.alma(hybrid_element, periodMa, 0.85, 1)
float finalVolatilityPct = na(gkyzMeanVariance) or gkyzMeanVariance < 0.0 ? 0.01 : math.max(math.sqrt(gkyzMeanVariance), 0.01)
 
// PRICE IMBALANCE & VOLUME FILTERING
float rawVolume = nz(volume)
float periodAvgVol = ta.alma(rawVolume, periodMa, 0.85, 1)
float volStDev = math.max(ta.stdev(rawVolume, periodMa), 0.00000001)
float maxAllowedVol = periodAvgVol > 0.0 ? periodAvgVol + (4.0 * volStDev) : 0.0
float processedVolume = (rawVolume > maxAllowedVol and periodAvgVol > 0.0) ? maxAllowedVol : rawVolume
 
float midPoint = (open + prev_close) / 2.0
float interBarImbalancePct = close > 0.0 and midPoint > 0.0 ? 100.0 * math.log(close / midPoint) : 0.0
float intraLogImbalancePct = pct_c_o
 
float w_log = 50.0 / 100.0
float w_inter = 1.0 - w_log
float priceImbalancePct = (interBarImbalancePct * w_inter) + (intraLogImbalancePct * w_log)
 
// WYCKOFF ANOMALY DETECTION (Mahalanobis Distance)
 
float candleSpread = math.abs(close - open)
float spreadStDev = math.max(ta.stdev(candleSpread, periodMa), 0.00000001)
float spreadMa = ta.alma(candleSpread, periodMa, 0.85, 1)
 
float volZScore = (processedVolume - periodAvgVol) / volStDev
float spreadZScore = (candleSpread - spreadMa) / spreadStDev
 
float expected_XY = ta.alma(processedVolume * candleSpread, periodMa, 0.85, 1)
float cov_vol_spread = expected_XY - (periodAvgVol * spreadMa)
float r_corr = cov_vol_spread / math.max(volStDev * spreadStDev, 0.00000001)
float r_clamped = math.max(-0.99, math.min(0.99, r_corr))
 
float mahalanobis_sq = (1.0 / (1.0 - math.pow(r_clamped, 2.0))) * (math.pow(volZScore, 2.0) + math.pow(spreadZScore, 2.0) - 2.0 * r_clamped * volZScore * spreadZScore)
float mahalanobis_dist = math.sqrt(math.max(mahalanobis_sq, 0.0))
float rawEffortScore = (volZScore > 0.0 and spreadZScore < volZScore) ? mahalanobis_dist : 0.0
float continuousAnomalyWeight = f_tanh(rawEffortScore * 0.5)
 
if showAnom
    float candleRange = high - low
    float closePosition = candleRange > 0.0 ? (close - low) / candleRange : 0.5
    float volumeFlowDirection = (closePosition - 0.5) * 2.0
    float anomalyAdjustmentPct = volumeFlowDirection * continuousAnomalyWeight * finalVolatilityPct * 1.0
    priceImbalancePct := priceImbalancePct + anomalyAdjustmentPct
 
// BVC PROBABILISTIC DECOMPOSITION & CVD GENERATION
float z_score = priceImbalancePct / finalVolatilityPct
float directionWeight = normCDF(z_score)
 
float sumVolume = math.sum(processedVolume, periodMa)
bool hasVolume = nz(sumVolume) > 0.0
 
float buyPowerSeries = hasVolume ? (processedVolume * directionWeight) : directionWeight
float sellPowerSeries = hasVolume ? (processedVolume * (1.0 - directionWeight)) : (1.0 - directionWeight)
 
delta = buyPowerSeries - sellPowerSeries
rawCvd = ta.cum(delta)
 
cvdRaw = ta.alma(rawCvd, smoothLen, smoothOffset, smoothSigma)
cvdMa  = ta.alma(cvdRaw, periodMa, maOffset, maSigma)
 
// EVASION ZONE & PLOTTING
cvdStDev  = ta.stdev(cvdRaw, periodMa * 2)
upperBand = cvdMa + (cvdStDev * multBand)
lowerBand = cvdMa - (cvdStDev * multBand)
 
inZone    = (cvdRaw <= upperBand) and (cvdRaw >= lowerBand)
cvd       = inZone ? cvdMa : cvdRaw
 
crossedAbove = ta.crossover(cvdRaw, upperBand)
crossedBelow = ta.crossunder(cvdRaw, lowerBand)
 
var int entryDirection = 0
if ta.crossunder(cvdRaw, upperBand)
    entryDirection := 1
if ta.crossover(cvdRaw, lowerBand)
    entryDirection := -1
 
var int lastDotType = 0
bool drawPlotUp   = false
bool drawPlotDown = false
 
if crossedAbove and entryDirection == -1 and lastDotType != 1
    drawPlotUp   := true
    lastDotType  := 1
 
if crossedBelow and entryDirection == 1 and lastDotType != -1
    drawPlotDown := true
    lastDotType  := -1
 
var color persistentColor = color.new(color.teal, 50)
if barstate.isfirst
    persistentColor := cvdRaw >= cvdMa ? color.new(color.teal, 50) : color.new(color.red, 50)
 
if cvdRaw > upperBand
    persistentColor := color.new(color.teal, 50)
else if cvdRaw < lowerBand
    persistentColor := color.new(color.red, 50)
 
plotRef1  = plot(cvd, style = plot.style_line, linewidth = 2, color = color.new(color.black, 0), title = 'CVD Line')
plotRef2  = plot(plotMa ? cvdMa : na, style = plot.style_line, linewidth = 1, color = color.new(color.blue, 30), title = 'CVD MA')
plotBand1 = plot(upperBand, color = color.new(color.gray, 60), style = plot.style_line, title = 'Evasion Upper Band')
plotBand2 = plot(lowerBand, color = color.new(color.gray, 60), style = plot.style_line, title = 'Evasion Lower Band')
 
fill(plotRef1, plotRef2, color = persistentColor, title = "CVD vs MA Fill")
fill(plotBand1, plotBand2, color = color.new(color.gray, 85), title = "Evasion Zone Fill")
 
plot(showCross and drawPlotUp ? cvd : na, title = 'Filtered Bullish Dot', style = plot.style_circles, linewidth = 5, color = color.teal)
plot(showCross and drawPlotDown ? cvd : na, title = 'Filtered Bearish Dot', style = plot.style_circles, linewidth = 5, color = color.red)

← 返回列表