Delta Volume Candles

Ama7Choudhary · study · 75 行 · 点赞 32 · TradingView 原页

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

Pine Script

//@version=6
indicator("Delta Volume Candles", "Delta Candles", overlay = true, linktoseries = true)
 
// ───────── Inputs ─────────
tfMode      = input.string("Auto", "Intrabar timeframe mode", options = ["Auto", "Manual"], tooltip = "Auto picks an intrabar timeframe (minimum 1 minute) based on your chart timeframe so history is covered.")
targetBars  = input.int(100, "Auto: target intrabars per candle", minval = 10, maxval = 1000, tooltip = "Higher = more precise but less history. Lower = more history.")
manualTf    = input.timeframe("60", "Manual intrabar timeframe")
colorMode   = input.string("Delta sign", "Color mode", options = ["Delta sign", "Delta strength (gradient)"])
wickMode    = input.string("Same as body", "Wick & border color", options = ["Same as body", "Candle polarity"])
noDataMode  = input.string("Gray", "Bars with no intrabar data", options = ["Gray", "Normal polarity"])
 
upColor     = input.color(#26a69a, "Positive delta color")
dnColor     = input.color(#ef5350, "Negative delta color")
neutColor   = input.color(color.gray, "Zero / no data color")
 
showDiv     = input.bool(false, "Highlight divergences", inline = "div")
divColor    = input.color(color.yellow, "", inline = "div")
 
// ───────── Intrabar timeframe ─────────
float wantedSec = timeframe.in_seconds(timeframe.period) / targetBars
string autoTf = wantedSec >= 14400 ? "240" :
     wantedSec >= 7200 ? "120" :
     wantedSec >= 3600 ? "60" :
     wantedSec >= 1800 ? "30" :
     wantedSec >= 900  ? "15" :
     wantedSec >= 300  ? "5"  :
     wantedSec >= 180  ? "3"  :
     wantedSec >= 120  ? "2"  : "1"
 
string intrabarTf = tfMode == "Auto" ? autoTf : manualTf
 
if timeframe.in_seconds(intrabarTf) >= timeframe.in_seconds(timeframe.period)
    runtime.error("Intrabar timeframe must be lower than the chart timeframe. Use a chart of 2 minutes or higher.")
 
// ───────── Intrabar up/down volume ─────────
upDnVolume() =>
    bool isUp = close > open or (close == open and close >= nz(close[1], close))
    [isUp ? volume : 0.0, isUp ? 0.0 : volume]
 
[upArr, dnArr] = request.security_lower_tf(syminfo.tickerid, intrabarTf, upDnVolume())
 
float upVol    = nz(array.sum(upArr))
float dnVol    = nz(array.sum(dnArr))
float totalVol = upVol + dnVol
float delta    = upVol - dnVol
float deltaPct = totalVol > 0 ? 100 * delta / totalVol : 0.0
bool  hasData  = totalVol > 0
 
// ───────── Colors ─────────
bool candleUp = close >= open
color noDataCol = noDataMode == "Gray" ? neutColor : (candleUp ? upColor : dnColor)
color baseCol   = not hasData ? noDataCol : delta > 0 ? upColor : delta < 0 ? dnColor : neutColor
 
float strength = math.min(math.abs(deltaPct), 100)
color bodyCol  = colorMode == "Delta sign" or not hasData ? baseCol : color.new(baseCol, int(math.round(80 - strength * 0.8)))
color edgeCol  = not hasData ? noDataCol : wickMode == "Same as body" ? baseCol : (candleUp ? upColor : dnColor)
 
bool diverge = hasData and ((close > open and delta < 0) or (close < open and delta > 0))
if showDiv and diverge
    bodyCol := divColor
    edgeCol := divColor
 
// ───────── Plot candles ─────────
plotcandle(open, high, low, close, "Delta Candles", color = bodyCol, wickcolor = edgeCol, bordercolor = edgeCol)
 
// ───────── Data Window values ─────────
plot(delta,    "Volume Delta",    display = display.data_window)
plot(deltaPct, "Delta %",         display = display.data_window)
plot(upVol,    "Up Volume",       display = display.data_window)
plot(dnVol,    "Down Volume",     display = display.data_window)
plot(totalVol, "Intrabar Volume", display = display.data_window)
 
// ───────── Info label (last bar) ─────────
if barstate.islast
    label.new(bar_index, high, "Intrabars: " + intrabarTf + (str.tonumber(intrabarTf) >= 60 ? " min" : " min"), style = label.style_label_down, color = color.new(color.gray, 70), textcolor = color.gray, size = size.small)

← 返回列表