[CT] Advanced CVD Histogram with Entry Signals

ChaosTrader63 · study · 342 行 · 点赞 167 · TradingView 原页

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

Pine Script

//@version=6
indicator(
     "[CT] Advanced CVD Histogram with Entry Signals",
     shorttitle="[CT] Advanced CVD",
     overlay=false,
     max_labels_count=500)
 
// =====================================================
// INPUT GROUPS
// =====================================================
 
string G1 = "01 — CVD Settings"
string G2 = "02 — RSI Filter"
string G3 = "03 — Histogram Colors"
string G4 = "04 — Entry Signals"
string G5 = "05 — Price Candle Coloring"
 
// =====================================================
// CVD SETTINGS
// =====================================================
 
lookback = input.int(
     20, "Lookback Period",
     minval=1, group=G1)
 
smooth = input.int(
     3, "Smoothing Period",
     minval=1, group=G1)
 
threshold = input.float(
     1.0, "Signal Threshold",
     minval=0.1, step=0.1, group=G1)
 
// =====================================================
// RSI FILTER
// =====================================================
 
// OFF by default to allow CVD-based signals.
 
useRSI = input.bool(
     false, "Use RSI Filter",
     group=G2)
 
rsiPeriod = input.int(
     14, "RSI Period",
     minval=1, group=G2)
 
rsiThresholdLow = input.int(
     30, "RSI Oversold",
     minval=0, maxval=100, group=G2)
 
rsiThresholdHigh = input.int(
     70, "RSI Overbought",
     minval=0, maxval=100, group=G2)
 
// =====================================================
// HISTOGRAM COLORS
// =====================================================
 
bullColor = input.color(
     #26A69A, "Bullish Histogram",
     group=G3)
 
bearColor = input.color(
     #EF5350, "Bearish Histogram",
     group=G3)
 
// =====================================================
// ENTRY SIGNAL SETTINGS
// =====================================================
 
showSignals = input.bool(
     true, "Show Entry Signals",
     group=G4)
 
showPriceSignals = input.bool(
     true, "Show Signals on Price Chart",
     group=G4)
 
showPanelSignals = input.bool(
     false, "Show Signals in Histogram Panel",
     group=G4)
 
confirmedSignalsOnly = input.bool(
     true, "Confirm Signals on Candle Close",
     group=G4)
 
signalMode = input.string(
     "First Threshold Cross",
     "Entry Signal Mode",
     options=[
         "First Threshold Cross",
         "Every Qualifying Bar"],
     group=G4)
 
buySignalColor = input.color(
     color.lime, "BUY Signal Color",
     group=G4)
 
sellSignalColor = input.color(
     color.red, "SELL Signal Color",
     group=G4)
 
// =====================================================
// PRICE CANDLE COLORING
// =====================================================
 
colorCandles = input.bool(
     false, "Color Price Candles",
     group=G5)
 
confirmedCandleColors = input.bool(
     true, "Color Confirmed Candles Only",
     group=G5)
 
// =====================================================
// ORIGINAL CVD CALCULATION
// =====================================================
 
// Positive volume on bullish candles.
// Negative volume on bearish or doji candles.
 
float delta = close > open ? volume : -volume
 
// Cumulative volume delta.
 
float cvd = ta.cum(delta)
 
// Smooth cumulative volume delta.
 
float smoothedCVD = ta.sma(cvd, smooth)
 
// CVD momentum.
 
float cvdMomentum = ta.change(smoothedCVD)
 
// Normalize using standard deviation.
 
float cvdStdDev = ta.stdev(cvdMomentum, lookback)
 
float normalizedCVDMomentum =
     cvdStdDev > 0 ?
     cvdMomentum / cvdStdDev : na
 
// =====================================================
// RSI CALCULATION
// =====================================================
 
float rsi = ta.rsi(close, rsiPeriod)
 
// =====================================================
// HISTOGRAM COLOR
// =====================================================
 
bool bullishMomentum = normalizedCVDMomentum > 0
 
color histColor = bullishMomentum ?
     bullColor : bearColor
 
// =====================================================
// HISTOGRAM
// =====================================================
 
plot(
     normalizedCVDMomentum,
     title="Normalized CVD Momentum",
     style=plot.style_columns,
     color=histColor,
     linewidth=2)
 
// =====================================================
// ZERO LINE
// =====================================================
 
hline(
     0, "Zero Line",
     color=color.new(color.gray, 50),
     linestyle=hline.style_dotted)
 
// =====================================================
// THRESHOLD LINES
// =====================================================
 
upperThreshold = hline(
     threshold,
     "Upper Threshold",
     color=color.new(color.green, 50))
 
lowerThreshold = hline(
     -threshold,
     "Lower Threshold",
     color=color.new(color.red, 50))
 
fill(
     upperThreshold,
     lowerThreshold,
     color=color.new(color.gray, 90))
 
// =====================================================
// ORIGINAL ENTRY CONDITIONS
// =====================================================
 
// LONG:
// Positive CVD momentum exceeds threshold.
// Optional RSI filter requires oversold RSI.
 
bool longQualified =
     normalizedCVDMomentum > threshold and
     (not useRSI or rsi < rsiThresholdLow)
 
// SHORT:
// Negative CVD momentum exceeds threshold.
// Optional RSI filter requires overbought RSI.
 
bool shortQualified =
     normalizedCVDMomentum < -threshold and
     (not useRSI or rsi > rsiThresholdHigh)
 
// =====================================================
// SIGNAL TRIGGER MODE
// =====================================================
 
// First Threshold Cross:
// Signals only when the full qualifying condition
// changes from false to true.
//
// Every Qualifying Bar:
// Repeats signals while conditions remain true.
 
bool firstLong = longQualified and not longQualified[1]
bool firstShort = shortQualified and not shortQualified[1]
 
bool rawLongSignal = signalMode == "First Threshold Cross" ?
     firstLong : longQualified
 
bool rawShortSignal = signalMode == "First Threshold Cross" ?
     firstShort : shortQualified
 
bool signalConfirmed =
     not confirmedSignalsOnly or barstate.isconfirmed
 
bool longSignal =
     showSignals and rawLongSignal and signalConfirmed
 
bool shortSignal =
     showSignals and rawShortSignal and signalConfirmed
 
// =====================================================
// MAIN PRICE CHART ENTRY SIGNALS
// =====================================================
 
// These plot directly on price despite the
// indicator occupying a separate lower panel.
 
plotshape(
     showPriceSignals and longSignal,
     title="BUY Entry on Price Chart",
     style=shape.triangleup,
     location=location.belowbar,
     color=buySignalColor,
     size=size.small,
     force_overlay=true)
 
plotshape(
     showPriceSignals and shortSignal,
     title="SELL Entry on Price Chart",
     style=shape.triangledown,
     location=location.abovebar,
     color=sellSignalColor,
     size=size.small,
     force_overlay=true)
 
// =====================================================
// OPTIONAL LOWER-PANEL SIGNALS
// =====================================================
 
plotshape(
     showPanelSignals and longSignal,
     title="BUY Entry in Histogram",
     style=shape.triangleup,
     location=location.bottom,
     color=buySignalColor,
     size=size.small)
 
plotshape(
     showPanelSignals and shortSignal,
     title="SELL Entry in Histogram",
     style=shape.triangledown,
     location=location.top,
     color=sellSignalColor,
     size=size.small)
 
// =====================================================
// OPTIONAL PRICE CANDLE COLORING
// =====================================================
 
// Uses the same bullish/bearish classification
// as the histogram.
//
// Momentum > 0 = Green
// Momentum <= 0 = Red
 
color priceBarColor = na
 
if colorCandles and not na(normalizedCVDMomentum)
    if not confirmedCandleColors or barstate.isconfirmed
        priceBarColor := histColor
 
barcolor(
     priceBarColor,
     title="Price Candles Match Histogram")
 
// =====================================================
// CURRENT CVD VALUE
// =====================================================
 
// Display in the TradingView Data Window
// without accumulating new labels.
 
plot(
     normalizedCVDMomentum,
     title="Current Normalized CVD Momentum",
     display=display.data_window)
 
// =====================================================
// ALERTS
// =====================================================
 
alertcondition(
     longSignal,
     title="Advanced CVD BUY Entry",
     message="[CT] Advanced CVD BUY | {{ticker}} | {{interval}}")
 
alertcondition(
     shortSignal,
     title="Advanced CVD SELL Entry",
     message="[CT] Advanced CVD SELL | {{ticker}} | {{interval}}")
 
alertcondition(
     longSignal or shortSignal,
     title="Advanced CVD Any Entry",
     message="[CT] Advanced CVD Entry | {{ticker}} | {{interval}}")

← 返回列表