本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("[CT] Volume Flow Candles", shorttitle="[CT] Volume Candles", overlay=false, precision=0)
// =====================================================
// INPUT GROUPS
// =====================================================
string G1 = "01 — Candle Construction"
string G2 = "02 — Smoothing"
string G3 = "03 — Volume Candle Colors & Momentum"
string G4 = "04 — Price Candle Coloring"
string G5 = "05 — Enhanced Candle Visibility"
string G6 = "06 — Moving Averages"
string G7 = "07 — Signals & Alerts"
// =====================================================
// CANDLE CONSTRUCTION
// =====================================================
showCandles = input.bool(true, "Show Volume Candles", group=G1)
calcMode = input.string(
"Smoothed OBV First",
"Calculation Mode",
options=["Smoothed OBV First", "Individual OHLC Smoothing", "Raw OBV Candles"],
group=G1)
wickMode = input.string(
"Price-Proportional Wicks",
"Wick Construction",
options=["Body Only", "Previous OBV Range", "Price-Proportional Wicks"],
group=G1)
// =====================================================
// SMOOTHING
// =====================================================
enableSmoothing = input.bool(true, "Enable Smoothing", group=G2)
smoothLength = input.int(8, "Smoother Length", minval=1, group=G2)
smoothType = input.string(
"DEMA",
"Smoothing Method",
options=["EMA", "DEMA", "TEMA", "SMA", "RMA", "WMA"],
group=G2)
// =====================================================
// VOLUME CANDLE COLORS & MOMENTUM
// =====================================================
colorMode = input.string(
"Hybrid OBV Direction",
"Candle Color Logic",
options=["Smoothed OBV Direction", "Raw OBV Direction", "Hybrid OBV Direction"],
group=G3)
hybridSmoothWeight = input.float(
70.0, "Hybrid Smoothed Weight %",
minval=0.0, maxval=100.0, step=5.0, group=G3)
forceBodyColors = input.bool(
false, "Force Colors To Match Candle Bodies", group=G3)
bullColor = input.color(#00F000, "Bullish Volume Candle", group=G3)
bearColor = input.color(#B52535, "Bearish Volume Candle", group=G3)
wickColor = input.color(color.gray, "Volume Candle Wick", group=G3)
borderColor = input.color(color.black, "Volume Candle Border", group=G3)
// =====================================================
// MAIN PRICE CANDLE COLORING
// =====================================================
enablePriceColors = input.bool(
true, "Enable Price Candle Coloring", group=G4)
priceBullColor = input.color(
#00C853, "Bullish Price Candle", group=G4)
priceBearColor = input.color(
#E53935, "Bearish Price Candle", group=G4)
confirmPriceColors = input.bool(
false, "Confirm Price Colors On Candle Close", group=G4)
// =====================================================
// ENHANCED CANDLE VISIBILITY
// =====================================================
bodySize = input.float(
1.75,
"Visual Candle Body Size",
minval=1.0,
maxval=4.0,
step=0.05,
group=G5)
minBodyPercent = input.float(
50.0,
"Minimum Body Strength %",
minval=0.0,
maxval=100.0,
step=5.0,
group=G5)
bodyAverageLength = input.int(
20, "Average Body Lookback",
minval=2, group=G5)
// =====================================================
// MOVING AVERAGES
// =====================================================
enableMA = input.bool(false, "Show Moving Averages", group=G6)
maType = input.string(
"DEMA",
"Moving Average Type",
options=["EMA", "DEMA", "TEMA", "SMA", "WMA", "RMA", "HMA"],
group=G6)
showMA1 = input.bool(true, "Show MA 1", group=G6)
ma1Length = input.int(8, "MA 1 Length", minval=1, group=G6)
ma1Color = input.color(color.aqua, "MA 1 Color", group=G6)
showMA2 = input.bool(true, "Show MA 2", group=G6)
ma2Length = input.int(21, "MA 2 Length", minval=1, group=G6)
ma2Color = input.color(color.fuchsia, "MA 2 Color", group=G6)
showMA3 = input.bool(false, "Show MA 3", group=G6)
ma3Length = input.int(1, "MA 3 Length", minval=1, group=G6)
ma3Color = input.color(color.orange, "MA 3 Color", group=G6)
maWidth = input.int(
2, "MA Line Width", minval=1, maxval=5, group=G6)
// =====================================================
// SIGNALS & ALERTS
// =====================================================
showCross = input.bool(false, "Show MA Cross Shapes", group=G7)
confirmedOnly = input.bool(
true, "Confirm Signals On Candle Close", group=G7)
bullSignalColor = input.color(
color.lime, "Bullish Signal", group=G7)
bearSignalColor = input.color(
color.red, "Bearish Signal", group=G7)
// =====================================================
// SAFE MOVING AVERAGE FUNCTION
// =====================================================
getMA(float src, int length, string method) =>
int len = math.max(1, length)
float e1 = ta.ema(src, len)
float e2 = ta.ema(e1, len)
float e3 = ta.ema(e2, len)
float demaValue = 2.0 * e1 - e2
float temaValue = 3.0 * e1 - 3.0 * e2 + e3
float smaValue = ta.sma(src, len)
float wmaValue = ta.wma(src, len)
float rmaValue = ta.rma(src, len)
int halfLen = math.max(1, int(math.round(len / 2.0)))
int rootLen = math.max(1, int(math.round(math.sqrt(len))))
float halfWMA = ta.wma(src, halfLen)
float fullWMA = ta.wma(src, len)
float hmaSource = 2.0 * halfWMA - fullWMA
float hmaValue = ta.wma(hmaSource, rootLen)
switch method
"EMA" => e1
"DEMA" => demaValue
"TEMA" => temaValue
"SMA" => smaValue
"WMA" => wmaValue
"RMA" => rmaValue
"HMA" => hmaValue
=> e1
// =====================================================
// OBV ENGINE
// =====================================================
float vol = nz(volume, 0.0)
float priceChange = nz(ta.change(close), 0.0)
float direction = math.sign(priceChange)
float obv = ta.cum(direction * vol)
float rawOpen = nz(obv[1], obv)
float rawClose = obv
// =====================================================
// RAW CANDLE CONSTRUCTION
// =====================================================
float bodyHigh = math.max(rawOpen, rawClose)
float bodyLow = math.min(rawOpen, rawClose)
float previousObv = nz(obv[2], rawOpen)
float previousHigh = math.max(bodyHigh, previousObv)
float previousLow = math.min(bodyLow, previousObv)
float priceRange = high - low
float upperPriceWick = high - math.max(open, close)
float lowerPriceWick = math.min(open, close) - low
float upperShare = priceRange > 0 ? upperPriceWick / priceRange : 0.0
float lowerShare = priceRange > 0 ? lowerPriceWick / priceRange : 0.0
float proportionalHigh = bodyHigh + vol * upperShare
float proportionalLow = bodyLow - vol * lowerShare
float rawHigh = switch wickMode
"Previous OBV Range" => previousHigh
"Price-Proportional Wicks" => proportionalHigh
=> bodyHigh
float rawLow = switch wickMode
"Previous OBV Range" => previousLow
"Price-Proportional Wicks" => proportionalLow
=> bodyLow
// =====================================================
// SMOOTHING CALCULATIONS
// =====================================================
float smoothedObv = getMA(obv, smoothLength, smoothType)
float firstOpen = nz(smoothedObv[1], smoothedObv)
float firstClose = smoothedObv
float firstHigh = math.max(firstOpen, firstClose)
float firstLow = math.min(firstOpen, firstClose)
float smoothOpen = getMA(rawOpen, smoothLength, smoothType)
float smoothHigh = getMA(rawHigh, smoothLength, smoothType)
float smoothLow = getMA(rawLow, smoothLength, smoothType)
float smoothClose = getMA(rawClose, smoothLength, smoothType)
// =====================================================
// SELECT CANDLE CONSTRUCTION
// =====================================================
bool useRaw = calcMode == "Raw OBV Candles" or not enableSmoothing
bool useFirst = calcMode == "Smoothed OBV First" and enableSmoothing
float candleOpen = useRaw ? rawOpen : useFirst ? firstOpen : smoothOpen
float candleClose = useRaw ? rawClose : useFirst ? firstClose : smoothClose
float selectedHigh = useRaw ? rawHigh : useFirst ? firstHigh : smoothHigh
float selectedLow = useRaw ? rawLow : useFirst ? firstLow : smoothLow
float candleHigh = math.max(selectedHigh, candleOpen, candleClose)
float candleLow = math.min(selectedLow, candleOpen, candleClose)
// =====================================================
// HYBRID VOLUME MOMENTUM COLOR ENGINE
// =====================================================
float smoothChange = smoothedObv - nz(smoothedObv[1], smoothedObv)
float rawChange = obv - nz(obv[1], obv)
float smoothWeight = hybridSmoothWeight / 100.0
float rawWeight = 1.0 - smoothWeight
float hybridChange = smoothChange * smoothWeight + rawChange * rawWeight
float selectedChange = switch colorMode
"Raw OBV Direction" => rawChange
"Hybrid OBV Direction" => hybridChange
=> smoothChange
bool bodyBull = candleClose >= candleOpen
var bool momentumBull = true
if selectedChange > 0
momentumBull := true
else if selectedChange < 0
momentumBull := false
bool isBull = forceBodyColors ? bodyBull : momentumBull
color candleColor = isBull ? bullColor : bearColor
// =====================================================
// MAIN PRICE CANDLE COLORING — CORRECTED
// =====================================================
// Pine Script v6 does not support nz() on bool.
// Use the previous bar's state directly.
bool confirmedBull = barstate.isconfirmed ? isBull : isBull[1]
bool priceBull = confirmPriceColors ? confirmedBull : isBull
color priceBarColor = na
if enablePriceColors
if priceBull
priceBarColor := priceBullColor
else
priceBarColor := priceBearColor
// This colors main-chart candles while the
// indicator remains in its lower panel.
barcolor(priceBarColor, title="CT Volume Momentum Price Candle Colors")
// =====================================================
// ENHANCED VISUAL CANDLE BODY SIZE
// =====================================================
float actualBody = math.abs(candleClose - candleOpen)
float averageBody = ta.sma(actualBody, bodyAverageLength)
float enlargedBody = actualBody * bodySize
float minimumBody = nz(averageBody, actualBody) * minBodyPercent / 100.0
float finalBody = actualBody > 0 ?
math.max(enlargedBody, minimumBody) : 0.0
// Preserve candle-body direction.
float bodyDirection = candleClose >= candleOpen ? 1.0 : -1.0
float bodyMid = (candleOpen + candleClose) / 2.0
float visualOpen = bodyMid - bodyDirection * finalBody / 2.0
float visualClose = bodyMid + bodyDirection * finalBody / 2.0
// Keep candle OHLC valid after visual enlargement.
float visualHigh = math.max(candleHigh, visualOpen, visualClose)
float visualLow = math.min(candleLow, visualOpen, visualClose)
// =====================================================
// PLOT LOWER VOLUME CANDLES
// =====================================================
plotcandle(
showCandles ? visualOpen : na,
showCandles ? visualHigh : na,
showCandles ? visualLow : na,
showCandles ? visualClose : na,
title="CT Volume Flow Candles",
color=candleColor,
wickcolor=wickColor,
bordercolor=borderColor)
// =====================================================
// MOVING AVERAGES
// =====================================================
float ma1 = getMA(candleClose, ma1Length, maType)
float ma2 = getMA(candleClose, ma2Length, maType)
float ma3 = getMA(candleClose, ma3Length, maType)
plot(enableMA and showMA1 ? ma1 : na,
title="MA 1", color=ma1Color, linewidth=maWidth)
plot(enableMA and showMA2 ? ma2 : na,
title="MA 2", color=ma2Color, linewidth=maWidth)
plot(enableMA and showMA3 ? ma3 : na,
title="MA 3", color=ma3Color, linewidth=maWidth)
// =====================================================
// MA CROSSOVER SIGNALS
// =====================================================
bool bullCross = ta.crossover(ma1, ma2)
bool bearCross = ta.crossunder(ma1, ma2)
bool signalReady = not confirmedOnly or barstate.isconfirmed
plotshape(
showCross and bullCross and signalReady ? candleLow : na,
title="Bullish MA Cross",
style=shape.circle,
location=location.absolute,
color=bullSignalColor,
size=size.tiny)
plotshape(
showCross and bearCross and signalReady ? candleHigh : na,
title="Bearish MA Cross",
style=shape.circle,
location=location.absolute,
color=bearSignalColor,
size=size.tiny)
// =====================================================
// ALERTS
// =====================================================
bool bullishFlip = isBull and not isBull[1]
bool bearishFlip = not isBull and isBull[1]
alertcondition(
bullishFlip and signalReady,
title="Bullish Volume Flow",
message="CT Volume Flow Candles changed to green")
alertcondition(
bearishFlip and signalReady,
title="Bearish Volume Flow",
message="CT Volume Flow Candles changed to red")
alertcondition(
bullCross and signalReady,
title="Bullish MA Crossover",
message="CT Volume Flow bullish MA crossover")
alertcondition(
bearCross and signalReady,
title="Bearish MA Crossunder",
message="CT Volume Flow bearish MA crossunder")