Capital Trade | Volume Pulse

D-HESON · study · 270 行 · 点赞 68 · TradingView 原页

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

Pine Script

// This Source Code Form is subject to the terms of the Mozilla Public License 2.0
// at https://mozilla.org/MPL/2.0/
// Original Williams Fractals template: Mit Nayi.
// Modified for Capital Trade: body-only fractals, adaptive spacing and alerts.
// Combined with Volume Pulse candle highlighting.
 
//@version=6
indicator("Capital Trade | Volume Pulse", shorttitle="CT Volume Pulse", overlay=true)
 
// ── Volume ──
 
gVolume = "Volume"
 
volLength = input.int(20, "EMA / StDev length", minval=2, group=gVolume)
growthPct = input.float(20.0, "L1 · Growth (%)", minval=0, step=1, group=gVolume,
     tooltip="Volume must increase by more than this percentage versus the previous bar. Higher volume levels take priority.")
mult1 = input.float(0.7, "L2 · StDev multiplier", minval=0.1, step=0.1, group=gVolume)
mult2 = input.float(1.8, "L3 · StDev multiplier", minval=0.1, step=0.1, group=gVolume)
mult3 = input.float(3.0, "L4 · StDev multiplier", minval=0.1, step=0.1, group=gVolume)
 
// ── Candle appearance ──
 
gCandles = "Candles"
 
paintCandles = input.bool(true, "Color candles", group=gCandles)
colorMode = input.string("Direction", "Color mode",
     options=["Direction", "Level"], group=gCandles)
fadeNormal = input.bool(true, "Fade normal candles", group=gCandles,
     tooltip="Apply normal-bar transparency to candles without a volume event. Display only; does not affect alerts.")
 
bullColor = input.color(#089981, "Bullish", inline="direction", group=gCandles)
bearColor = input.color(#000000, "Bearish", inline="direction", group=gCandles)
 
normalColor = input.color(#000000, "Normal", inline="levels0", group=gCandles,
     tooltip="Used in Level color mode only.")
level1Color = input.color(#2B2B2B, "L1", inline="levels1", group=gCandles)
level2Color = input.color(#2B2B2B, "L2", inline="levels1", group=gCandles)
level3Color = input.color(#2B2B2B, "L3", inline="levels2", group=gCandles)
level4Color = input.color(#2B2B2B, "L4", inline="levels2", group=gCandles)
 
// ── Candle transparency ──
 
gOpacity = "Candle Transparency"
 
normalAlpha = input.int(45, "Normal", minval=0, maxval=100, group=gOpacity,
     tooltip="0 = opaque. 100 = invisible.")
level1Alpha = input.int(0, "L1", minval=0, maxval=100, inline="alpha12", group=gOpacity)
level2Alpha = input.int(0, "L2", minval=0, maxval=100, inline="alpha12", group=gOpacity)
level3Alpha = input.int(0, "L3", minval=0, maxval=100, inline="alpha34", group=gOpacity)
level4Alpha = input.int(0, "L4", minval=0, maxval=100, inline="alpha34", group=gOpacity)
 
// ── Body fractals ──
 
gFractals = "Body Fractals"
 
fractalBars = input.string("5", "Length", options=["3", "5"], group=gFractals)
equalMode = input.string("Last bar", "Equal levels",
     options=["Last bar", "Strict"], group=gFractals,
     tooltip="Last bar: equal body levels are allowed on the left, but not on the right. Strict: equal levels on either side invalidate the fractal.")
 
showUpper = input.bool(true, "Upper", inline="fractalVisibility", group=gFractals,
     tooltip="Display only. Does not affect alerts.")
showLower = input.bool(true, "Lower", inline="fractalVisibility", group=gFractals,
     tooltip="Display only. Does not affect alerts.")
 
// ── Fractal appearance ──
 
gMarkers = "Fractal Markers"
 
markerType = input.string("Dots", "Shape",
     options=["Dots", "Crosses", "Triangles"], group=gMarkers)
markerColor = input.color(color.rgb(160, 160, 160), "Color", group=gMarkers)
markerAlpha = input.int(60, "Transparency", minval=0, maxval=100, group=gMarkers)
pointSize = input.int(1, "Dot / cross size", minval=1, maxval=5, group=gMarkers)
triangleSize = input.string("Classic", "Triangle size",
     options=["Classic", "Tiny", "Small", "Normal"], group=gMarkers,
     tooltip="Classic uses automatic sizing from the original Williams Fractals template.")
 
anchor = input.string("Wick", "Anchor", options=["Wick", "Body"], group=gMarkers,
     tooltip="Visual anchor only. Fractals are always calculated from candle bodies.")
upperGapPct = input.float(10.0, "Upper gap (% ATR)", minval=0, step=1, group=gMarkers)
lowerGapPct = input.float(10.0, "Lower gap (% ATR)", minval=0, step=1, group=gMarkers)
 
// ── Alerts ──
 
gAlerts = "Alerts"
 
volumeAlerts = input.bool(true, "Volume alerts", group=gAlerts,
     tooltip="Create one TradingView alert using Any alert() function call. All enabled events are evaluated at bar close on the chart timeframe.")
alertL1 = input.bool(true, "L1", inline="volumeAlerts", group=gAlerts)
alertL2 = input.bool(true, "L2", inline="volumeAlerts", group=gAlerts)
alertL3 = input.bool(true, "L3", inline="volumeAlerts", group=gAlerts)
alertL4 = input.bool(true, "L4", inline="volumeAlerts", group=gAlerts)
 
fractalAlerts = input.bool(true, "Fractal alerts", group=gAlerts)
alertLower = input.bool(true, "Lower · Long", inline="fractalAlerts", group=gAlerts)
alertUpper = input.bool(true, "Upper · Short", inline="fractalAlerts", group=gAlerts)
 
// ── Validate volume settings ──
 
if barstate.isfirst and not (mult1 < mult2 and mult2 < mult3)
    runtime.error("StDev multipliers must increase: L2 < L3 < L4.")
 
// ── Volume calculation ──
// Thresholds include the current bar, as in the original Volume Pulse.
 
emaVol = ta.ema(volume, volLength)
stdVol = ta.stdev(volume, volLength)
 
threshold1 = emaVol + stdVol * mult1
threshold2 = emaVol + stdVol * mult2
threshold3 = emaVol + stdVol * mult3
 
validVolume = not na(volume) and volume > 0
volumeGrowth = not na(volume[1]) and volume[1] > 0 ? (volume - volume[1]) / volume[1] * 100.0 : na
 
growthEvent = validVolume and not na(volumeGrowth) and volumeGrowth > growthPct
thresholdsReady = validVolume and not na(stdVol) and stdVol > 0
 
int volumeLevel = 0
 
if thresholdsReady and volume >= threshold3
    volumeLevel := 4
else if thresholdsReady and volume >= threshold2
    volumeLevel := 3
else if thresholdsReady and volume >= threshold1
    volumeLevel := 2
else if growthEvent
    volumeLevel := 1
 
// ── Candle coloring ──
 
volumeColor = switch volumeLevel
    4 => level4Color
    3 => level3Color
    2 => level2Color
    1 => level1Color
    => normalColor
 
candleAlpha = switch volumeLevel
    4 => level4Alpha
    3 => level3Alpha
    2 => level2Alpha
    1 => level1Alpha
    => normalAlpha
 
directionColor = close >= open ? bullColor : bearColor
candleColor = colorMode == "Direction" ? directionColor : volumeColor
 
shouldPaint = paintCandles and barstate.isconfirmed and not na(volume) and (volumeLevel > 0 or fadeNormal)
 
barcolor(shouldPaint ? color.new(candleColor, candleAlpha) : na,
     title="Volume candles")
 
// ── Body fractal calculation ──
// Fractals are independent of volume conditions.
 
bodyHigh = math.max(open, close)
bodyLow = math.min(open, close)
 
int rightBars = fractalBars == "5" ? 2 : 1
bool allowEqualLeft = equalMode == "Last bar"
 
bool upperFractal = bar_index >= 2 * rightBars and barstate.isconfirmed
bool lowerFractal = bar_index >= 2 * rightBars and barstate.isconfirmed
 
for i = 1 to rightBars
    bool upperLeft = allowEqualLeft ? bodyHigh[rightBars] >= bodyHigh[rightBars + i] : bodyHigh[rightBars] > bodyHigh[rightBars + i]
    bool lowerLeft = allowEqualLeft ? bodyLow[rightBars] <= bodyLow[rightBars + i] : bodyLow[rightBars] < bodyLow[rightBars + i]
 
    upperFractal := upperFractal and upperLeft and bodyHigh[rightBars] > bodyHigh[rightBars - i]
    lowerFractal := lowerFractal and lowerLeft and bodyLow[rightBars] < bodyLow[rightBars - i]
 
// ── Fractal spacing ──
 
atrValue = ta.atr(14)
trValue = ta.tr(true)
 
float spacingRange = nz(atrValue[rightBars], trValue[rightBars])
float upperDistance = spacingRange * upperGapPct / 100.0
float lowerDistance = spacingRange * lowerGapPct / 100.0
 
float upperBase = anchor == "Wick" ? high[rightBars] : bodyHigh[rightBars]
float lowerBase = anchor == "Wick" ? low[rightBars] : bodyLow[rightBars]
 
float upperY = showUpper and upperFractal ? upperBase + upperDistance : na
float lowerY = showLower and lowerFractal ? lowerBase - lowerDistance : na
 
// ── Fractal display ──
 
drawColor = color.new(markerColor, markerAlpha)
 
bool usePoints = markerType != "Triangles"
pointStyle = markerType == "Crosses" ? plot.style_cross : plot.style_circles
 
bool triAuto = markerType == "Triangles" and triangleSize == "Classic"
bool triTiny = markerType == "Triangles" and triangleSize == "Tiny"
bool triSmall = markerType == "Triangles" and triangleSize == "Small"
bool triNormal = markerType == "Triangles" and triangleSize == "Normal"
 
plot(usePoints ? upperY : na, title="Upper points", color=drawColor, linewidth=pointSize, style=pointStyle, offset=-rightBars, join=false, editable=true, display=display.pane)
plot(usePoints ? lowerY : na, title="Lower points", color=drawColor, linewidth=pointSize, style=pointStyle, offset=-rightBars, join=false, editable=true, display=display.pane)
 
plotshape(triAuto ? upperY : na, title="Upper triangles · Classic", style=shape.triangledown, location=location.absolute, color=drawColor, offset=-rightBars, size=size.auto, editable=true, display=display.pane)
plotshape(triAuto ? lowerY : na, title="Lower triangles · Classic", style=shape.triangleup, location=location.absolute, color=drawColor, offset=-rightBars, size=size.auto, editable=true, display=display.pane)
 
plotshape(triTiny ? upperY : na, title="Upper triangles · Tiny", style=shape.triangledown, location=location.absolute, color=drawColor, offset=-rightBars, size=size.tiny, editable=true, display=display.pane)
plotshape(triTiny ? lowerY : na, title="Lower triangles · Tiny", style=shape.triangleup, location=location.absolute, color=drawColor, offset=-rightBars, size=size.tiny, editable=true, display=display.pane)
 
plotshape(triSmall ? upperY : na, title="Upper triangles · Small", style=shape.triangledown, location=location.absolute, color=drawColor, offset=-rightBars, size=size.small, editable=true, display=display.pane)
plotshape(triSmall ? lowerY : na, title="Lower triangles · Small", style=shape.triangleup, location=location.absolute, color=drawColor, offset=-rightBars, size=size.small, editable=true, display=display.pane)
 
plotshape(triNormal ? upperY : na, title="Upper triangles · Normal", style=shape.triangledown, location=location.absolute, color=drawColor, offset=-rightBars, size=size.normal, editable=true, display=display.pane)
plotshape(triNormal ? lowerY : na, title="Lower triangles · Normal", style=shape.triangleup, location=location.absolute, color=drawColor, offset=-rightBars, size=size.normal, editable=true, display=display.pane)
 
// ── Alert helpers ──
 
f_timeframeLabel() =>
    string result = timeframe.period
 
    if timeframe.isseconds
        result := str.tostring(timeframe.multiplier) + "S"
    else if timeframe.isminutes
        result := timeframe.multiplier % 60 == 0 ? str.tostring(timeframe.multiplier / 60, "#") + "H" : str.tostring(timeframe.multiplier) + "m"
    else if timeframe.isdaily
        result := str.tostring(timeframe.multiplier) + "D"
    else if timeframe.isweekly
        result := str.tostring(timeframe.multiplier) + "W"
    else if timeframe.ismonthly
        result := str.tostring(timeframe.multiplier) + "M"
 
    result
 
f_append(string existing, string eventText) =>
    existing + (str.length(existing) > 0 ? "\n" : "") + eventText
 
// ── Unified alert engine ──
// One message per closing bar, containing every enabled event.
// A disabled higher volume level does not fall back to a lower level.
 
bool volumeLevelEnabled = switch volumeLevel
    4 => alertL4
    3 => alertL3
    2 => alertL2
    1 => alertL1
    => false
 
string volumeMessage = switch volumeLevel
    4 => "▂▄▆█ volume"
    3 => "▂▄▆ volume"
    2 => "▂▄ volume"
    1 => "▂ volume"
    => ""
 
string tfLabel = f_timeframeLabel()
string fractalContext = " · " + syminfo.ticker + " · " + tfLabel
string alertMessage = ""
 
if barstate.isconfirmed
    if volumeAlerts and volumeLevelEnabled
        alertMessage := f_append(alertMessage, volumeMessage)
 
    if fractalAlerts and alertLower and lowerFractal
        alertMessage := f_append(alertMessage, "▲ LONG · FRACTAL CONFIRM" + fractalContext)
 
    if fractalAlerts and alertUpper and upperFractal
        alertMessage := f_append(alertMessage, "▼ SHORT · FRACTAL CONFIRM" + fractalContext)
 
    if str.length(alertMessage) > 0
        alert(alertMessage, alert.freq_once_per_bar_close)

← 返回列表