Astounding VSA Volume Clouds with Alerts

philipovitz · study · 274 行 · 点赞 17 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © philipovitz
 
//@version=6
// VSA Volume Clouds + Alerts
//
// Classifies every bar's volume against its 30-bar SMA and combines it with candle spread, structure and RSI
// into a set of alerts for different market situations.
//
// Volume classes (multiples of SMA(30) volume):
//   RED     >= 3.0x   ultra high volume
//   YELLOW  >= 1.5x   high volume
//   GREEN   0.5x-1.5x average volume
//   BLUE    <  0.5x   low volume
//
// Idea credit: the volume-vs-SMA cloud concept follows the publicly described logic of "Volume Spread for VSA"
// by HariprasathGopal (protected source). This script is an independent implementation, not a copy of its code.
//
// Chart labels (price chart; bullish events sit below the candle, bearish above it):
//   ▲ ULTRA / ▼ ULTRA     red (ultra high) volume bar on a bullish / bearish candle
//   ▲ BREAK / ▼ BREAK     breakout of the N-bar high / low on high volume and a wide spread
//   ABSORB                absorption: high volume + narrow spread near the N-bar high (above) or low (below)
//   EXHAUST               possible exhaustion: ultra volume after a run of same-direction candles
//   WAKE-UP               high volume after a quiet period
//   ECHO                  high-volume bar, 2-4 average-volume bars, then high volume again (context only)
//   ▲ HIGH / ▼ HIGH      (optional) yellow bar on a bullish / bearish candle
//   ▲ BRK+RSI / ▼ BRK+RSI (optional) plain N-bar breakout with RSI filter
indicator("VSA Volume Clouds + Alerts", shorttitle="VSA Clouds", format=format.volume, overlay=false, max_labels_count=500, explicit_plot_zorder=true)
 
// ===================== INPUTS =====================
grpV = "Volume classification"
len    = input.int(30, "SMA length (volume)", minval=2, group=grpV)
mUltra = input.float(3.0, "Red (ultra high): >= x SMA", step=0.1, group=grpV)
mHigh  = input.float(1.5, "Yellow (high): >= x SMA", step=0.1, group=grpV)
mLow   = input.float(0.5, "Blue (low): < x SMA", step=0.1, group=grpV)
 
grpA = "Spread and structure"
atrLen  = input.int(14, "ATR length", minval=1, group=grpA)
narrowK = input.float(0.7, "Narrow spread: range < K x ATR", step=0.05, group=grpA)
wideK   = input.float(1.3, "Wide spread: range > K x ATR", step=0.05, group=grpA)
brkN    = input.int(48, "Breakout lookback (bars)", minval=2, group=grpA)
rsiLen  = input.int(50, "RSI length (trend filter)", minval=2, group=grpA)
dryN    = input.int(6, "Quiet period: consecutive low-volume bars", minval=2, group=grpA)
exhN    = input.int(3, "Exhaustion: same-direction bars before", minval=2, group=grpA)
 
grpS = "Alerts"
useAgg = input.bool(true, "Combined alert: use 'Any alert() function call' (one alert per chart)", group=grpS, tooltip="Create one alert on this indicator with the condition 'Any alert() function call'. The message contains the symbol, timeframe and the situation.")
 
useLive = input.bool(false, "Intrabar alerts: notify while a yellow / red bar is forming", group=grpS, tooltip="Fires as soon as the running volume of the current bar crosses the yellow or red threshold, without waiting for the bar to close. Needs the 'Any alert() function call' alert. Candle colour at that moment may still change before the close.")
 
grpD = "Appearance"
showClouds  = input.bool(true, "Filled clouds", group=grpD)
borderCol   = input.color(color.black, "Volume bar outline", group=grpD)
showMarkers = input.bool(true, "Markers above volume bars", group=grpD)
showFreq    = input.bool(false, "Markers for frequent breakouts (09/10)", group=grpD)
 
grpC = "Labels on price chart"
showChart      = input.bool(true, "Show concise labels on the price chart", group=grpC, tooltip="Labels are drawn on the main chart pane and only on closed bars. Hover a label for details.")
showChartEcho  = input.bool(true, "Include volume echo labels", group=grpC, tooltip="High-volume bar, then 2 to 4 average-volume (green) bars, then high volume again. Context only: not a directional signal.")
showChartMinor = input.bool(false, "Include yellow-bar labels (frequent)", group=grpC)
showChartBrk   = input.bool(false, "Include plain breakout labels (frequent)", group=grpC)
lblSizeIn      = input.string("Large", "Label text size", options=["Small", "Normal", "Large", "Huge"], group=grpC)
lblSize = switch lblSizeIn
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    "Huge"   => size.huge
 
// ===================== VOLUME CLASSIFICATION =====================
base  = ta.sma(volume, len)
ratio = base > 0 ? volume / base : na
 
isUltra = ratio >= mUltra
isHigh  = ratio >= mHigh and not isUltra
isLow   = ratio < mLow
isAvg   = not isUltra and not isHigh and not isLow
 
colV = isUltra ? color.red : isHigh ? color.yellow : isAvg ? color.green : color.blue
 
// Clouds are drawn first so that the bars stay on top.
pZero  = plot(0, "Zero", color=na)
pLow   = plot(base * mLow, "Low x SMA", color=color.new(color.blue, 30))
pHigh  = plot(base * mHigh, "High x SMA", color=color.new(color.orange, 30))
pUltra = plot(base * mUltra, "Ultra x SMA", color=color.new(color.red, 20))
fill(pHigh, pUltra, color=showClouds ? color.new(color.red, 65) : na, title="Cloud 3 (high to ultra)")
fill(pLow, pHigh, color=showClouds ? color.new(color.yellow, 82) : na, title="Cloud 2 (average)")
fill(pZero, pLow, color=showClouds ? color.new(color.blue, 90) : na, title="Cloud 1 (low)")
plot(base, "Volume SMA", color=color.gray, linewidth=2)
 
// Volume bars with an outline: a candle drawn from zero to the volume value.
plotcandle(0, volume, 0, volume, "Volume", color=colV, wickcolor=borderCol, bordercolor=borderCol)
 
// ===================== CANDLE AND STRUCTURE =====================
bull = close >= open          // same as the default volume colouring on TradingView
bear = close < open
atr  = ta.atr(atrLen)
rng  = high - low
narrow = rng < narrowK * atr
wide   = rng > wideK * atr
 
hh = ta.highest(high, brkN)[1]
ll = ta.lowest(low, brkN)[1]
brkUp = close > hh
brkDn = close < ll
nearTop = close > hh - 0.5 * atr
nearBot = close < ll + 0.5 * atr
rsiV = ta.rsi(close, rsiLen)
highVol = ratio >= mHigh
 
// ===================== CONDITIONS =====================
c1  = isHigh and bull                                             // yellow bar, bullish candle
c2  = isHigh and bear                                             // yellow bar, bearish candle
c3  = isUltra and bull                                            // red bar, bullish candle
c4  = isUltra and bear                                            // red bar, bearish candle
c5  = highVol and narrow and nearTop                              // absorption near local high
c6  = highVol and narrow and nearBot                              // absorption near local low
c7  = highVol and wide and bull and brkUp                         // breakout up on high volume
c8  = highVol and wide and bear and brkDn                         // breakout down on high volume
c9  = brkUp and bull and rsiV > 50                                // plain breakout up, RSI > 50
c10 = brkDn and bear and rsiV < 50                                // plain breakout down, RSI < 50
c11 = highVol and ta.highest(ratio, dryN)[1] < 1.0                // wake-up after a quiet period
c12 = isUltra and bull and ta.lowest(close - open, exhN)[1] > 0   // possible exhaustion of an up-move
c13 = isUltra and bear and ta.highest(close - open, exhN)[1] < 0  // possible exhaustion of a down-move
c14 = ta.highest(ratio, dryN) < mLow                              // quiet: volume dried up
avgB = ratio >= mLow and ratio < mHigh                            // average-volume (green) bar
c15 = highVol and ((ratio[3] >= mHigh and avgB[1] and avgB[2]) or (ratio[4] >= mHigh and avgB[1] and avgB[2] and avgB[3]) or (ratio[5] >= mHigh and avgB[1] and avgB[2] and avgB[3] and avgB[4]))  // volume echo
 
// ===================== MARKERS IN THE INDICATOR PANE =====================
// Markers sit above the bar tops (three rows), so they never cover the volume bars.
y1 = volume * 1.10
y2 = volume * 1.22
y3 = volume * 1.34
plotshape(showMarkers and c3  ? y1 : na, "03 Red bar, bullish",    shape.triangleup,   location.absolute, color.red,     size=size.tiny)
plotshape(showMarkers and c4  ? y1 : na, "04 Red bar, bearish",    shape.triangledown, location.absolute, color.red,     size=size.tiny)
plotshape(showMarkers and c12 ? y1 : na, "12 Exhaustion up",       shape.xcross,       location.absolute, color.purple,  size=size.tiny, text="X", textcolor=color.purple)
plotshape(showMarkers and c13 ? y1 : na, "13 Exhaustion down",     shape.xcross,       location.absolute, color.fuchsia, size=size.tiny, text="X", textcolor=color.fuchsia)
plotshape(showMarkers and c5  ? y2 : na, "05 Absorption at high",  shape.diamond,      location.absolute, color.orange,  size=size.tiny, text="A", textcolor=color.orange)
plotshape(showMarkers and c6  ? y2 : na, "06 Absorption at low",   shape.diamond,      location.absolute, color.aqua,    size=size.tiny, text="A", textcolor=color.aqua)
plotshape(showMarkers and c7  ? y2 : na, "07 Breakout up + volume",   shape.arrowup,   location.absolute, color.lime,    size=size.small)
plotshape(showMarkers and c8  ? y2 : na, "08 Breakout down + volume", shape.arrowdown, location.absolute, color.fuchsia, size=size.small)
plotshape(showMarkers and c11 ? y3 : na, "11 Wake-up",             shape.circle,       location.absolute, color.teal,    size=size.tiny, text="W", textcolor=color.teal)
plotshape(showMarkers and c15 ? y3 : na, "15 Volume echo",        shape.square,       location.absolute, color.new(color.yellow, 0), size=size.tiny, text="E", textcolor=color.olive)
plotshape(showMarkers and showFreq and c9  ? y3 : na, "09 Breakout up (RSI)",   shape.circle, location.absolute, color.new(color.lime, 30),    size=size.tiny)
plotshape(showMarkers and showFreq and c10 ? y3 : na, "10 Breakout down (RSI)", shape.circle, location.absolute, color.new(color.fuchsia, 30), size=size.tiny)
 
// ===================== CONCISE LABELS ON THE PRICE CHART =====================
// Bullish-type events are labelled below the candle, bearish-type above it; one label per bar at most per side.
// force_overlay draws them on the main chart pane even though the indicator lives in its own pane.
string lblBelow = ""
string lblAbove = ""
string tipBelow = ""
string tipAbove = ""
if showChart and barstate.isconfirmed
    if c3
        lblBelow := lblBelow + "▲ ULTRA "
        tipBelow := tipBelow + "▲ ULTRA ultra volume, bullish candle\n"
    if c4
        lblAbove := lblAbove + "▼ ULTRA "
        tipAbove := tipAbove + "▼ ULTRA ultra volume, bearish candle\n"
    if c7
        lblBelow := lblBelow + "▲ BREAK "
        tipBelow := tipBelow + "▲ BREAK breakout up on high volume, wide spread\n"
    if c8
        lblAbove := lblAbove + "▼ BREAK "
        tipAbove := tipAbove + "▼ BREAK breakout down on high volume, wide spread\n"
    if c6
        lblBelow := lblBelow + "ABSORB "
        tipBelow := tipBelow + "ABSORB absorption near the local low (high volume, narrow spread)\n"
    if c5
        lblAbove := lblAbove + "ABSORB "
        tipAbove := tipAbove + "ABSORB absorption near the local high (high volume, narrow spread)\n"
    if c13
        lblBelow := lblBelow + "EXHAUST "
        tipBelow := tipBelow + "EXHAUST possible exhaustion of a down-move\n"
    if c12
        lblAbove := lblAbove + "EXHAUST "
        tipAbove := tipAbove + "EXHAUST possible exhaustion of an up-move\n"
    if c11
        if bull
            lblBelow := lblBelow + "WAKE-UP "
            tipBelow := tipBelow + "WAKE-UP wake-up: high volume after a quiet period\n"
        else
            lblAbove := lblAbove + "WAKE-UP "
            tipAbove := tipAbove + "WAKE-UP wake-up: high volume after a quiet period\n"
    if showChartEcho and c15
        if bull
            lblBelow := lblBelow + "ECHO "
            tipBelow := tipBelow + "ECHO volume echo: high volume, 2-4 average bars, high volume again (context only)\n"
        else
            lblAbove := lblAbove + "ECHO "
            tipAbove := tipAbove + "ECHO volume echo: high volume, 2-4 average bars, high volume again (context only)\n"
    if showChartMinor
        if c1
            lblBelow := lblBelow + "▲ HIGH "
            tipBelow := tipBelow + "▲ HIGH yellow bar, bullish candle\n"
        if c2
            lblAbove := lblAbove + "▼ HIGH "
            tipAbove := tipAbove + "▼ HIGH yellow bar, bearish candle\n"
    if showChartBrk
        if c9
            lblBelow := lblBelow + "▲ BRK+RSI "
            tipBelow := tipBelow + "▲ BRK+RSI breakout of the N-bar high, RSI above 50\n"
        if c10
            lblAbove := lblAbove + "▼ BRK+RSI "
            tipAbove := tipAbove + "▼ BRK+RSI breakout of the N-bar low, RSI below 50\n"
 
detail = "Volume " + str.tostring(ratio, "#.0") + "x SMA(" + str.tostring(len) + ")  |  range " + str.tostring(atr > 0 ? rng / atr : 0.0, "#.0") + " ATR  |  RSI " + str.tostring(rsiV, "#")
if lblBelow != ""
    label.new(bar_index, na, str.trim(lblBelow), yloc=yloc.belowbar, style=label.style_label_up, color=color.new(color.green, 15), textcolor=color.white, size=lblSize, tooltip=tipBelow + detail, force_overlay=true)
if lblAbove != ""
    label.new(bar_index, na, str.trim(lblAbove), yloc=yloc.abovebar, style=label.style_label_down, color=color.new(color.red, 15), textcolor=color.white, size=lblSize, tooltip=tipAbove + detail, force_overlay=true)
 
// ===================== SINGLE ALERT CONDITIONS =====================
// In the alert dialog pick the condition and set the trigger to "Once Per Bar Close".
alertcondition(c1,  "01 VSA yellow bar + bullish candle", "{{ticker}} {{interval}}: YELLOW bar (high volume), bullish candle")
alertcondition(c2,  "02 VSA yellow bar + bearish candle", "{{ticker}} {{interval}}: YELLOW bar (high volume), bearish candle")
alertcondition(c3,  "03 VSA red bar + bullish candle",    "{{ticker}} {{interval}}: RED bar (ultra volume), bullish candle")
alertcondition(c4,  "04 VSA red bar + bearish candle",    "{{ticker}} {{interval}}: RED bar (ultra volume), bearish candle")
alertcondition(c5,  "05 Absorption near local high",      "{{ticker}} {{interval}}: high volume, narrow spread near the local high (distribution / reversal risk)")
alertcondition(c6,  "06 Absorption near local low",       "{{ticker}} {{interval}}: high volume, narrow spread near the local low (demand absorbing supply)")
alertcondition(c7,  "07 Breakout up + high volume",       "{{ticker}} {{interval}}: breakout UP on high volume, wide spread")
alertcondition(c8,  "08 Breakout down + high volume",     "{{ticker}} {{interval}}: breakout DOWN on high volume, wide spread")
alertcondition(c9,  "09 Breakout up (RSI above 50)",      "{{ticker}} {{interval}}: breakout of the N-bar high, RSI above 50")
alertcondition(c10, "10 Breakout down (RSI below 50)",    "{{ticker}} {{interval}}: breakout of the N-bar low, RSI below 50")
alertcondition(c11, "11 Wake-up after quiet period",      "{{ticker}} {{interval}}: high volume after a period of low volume")
alertcondition(c12, "12 Possible exhaustion of up-move",  "{{ticker}} {{interval}}: ultra volume after a run of bullish candles (possible exhaustion)")
alertcondition(c13, "13 Possible exhaustion of down-move","{{ticker}} {{interval}}: ultra volume after a run of bearish candles (possible exhaustion)")
alertcondition(c14, "14 Quiet: volume dried up",          "{{ticker}} {{interval}}: volume below the low threshold for several bars (compression)")
alertcondition(c15, "15 Volume echo",                     "{{ticker}} {{interval}}: high volume, 2-4 average bars, high volume again (context, not a direction)")
alertcondition(highVol, "16 Yellow or red bar forming (intrabar)", "{{ticker}} {{interval}}: volume of the current bar reached the YELLOW threshold or higher")
alertcondition(isUltra, "17 Red bar forming (intrabar)", "{{ticker}} {{interval}}: volume of the current bar reached the RED threshold")
 
// ===================== INTRABAR ALERTS =====================
// Volume only grows during a bar, and the ratio rises with it, so once a threshold is crossed it stays crossed.
// Each message fires at most once per bar. A bar that turns yellow and later red sends two notifications.
liveDir = bull ? "candle bullish so far" : "candle bearish so far"
if useLive and barstate.isrealtime and isUltra
    alert(syminfo.ticker + " " + timeframe.period + ": RED bar forming (" + liveDir + ")", alert.freq_once_per_bar)
if useLive and barstate.isrealtime and isHigh
    alert(syminfo.ticker + " " + timeframe.period + ": YELLOW bar forming (" + liveDir + ")", alert.freq_once_per_bar)
 
// ===================== COMBINED ALERT =====================
// One alert per chart: condition "VSA Clouds" -> "Any alert() function call".
pre = syminfo.ticker + " " + timeframe.period + ": "
if useAgg and c1
    alert(pre + "YELLOW bullish", alert.freq_once_per_bar_close)
if useAgg and c2
    alert(pre + "YELLOW bearish", alert.freq_once_per_bar_close)
if useAgg and c3
    alert(pre + "RED bullish", alert.freq_once_per_bar_close)
if useAgg and c4
    alert(pre + "RED bearish", alert.freq_once_per_bar_close)
if useAgg and c5
    alert(pre + "Absorption near high", alert.freq_once_per_bar_close)
if useAgg and c6
    alert(pre + "Absorption near low", alert.freq_once_per_bar_close)
if useAgg and c7
    alert(pre + "Breakout UP + high volume", alert.freq_once_per_bar_close)
if useAgg and c8
    alert(pre + "Breakout DOWN + high volume", alert.freq_once_per_bar_close)
if useAgg and c9
    alert(pre + "Breakout up (RSI above 50)", alert.freq_once_per_bar_close)
if useAgg and c10
    alert(pre + "Breakout down (RSI below 50)", alert.freq_once_per_bar_close)
if useAgg and c11
    alert(pre + "Wake-up after quiet period", alert.freq_once_per_bar_close)
if useAgg and c12
    alert(pre + "Possible exhaustion of up-move", alert.freq_once_per_bar_close)
if useAgg and c13
    alert(pre + "Possible exhaustion of down-move", alert.freq_once_per_bar_close)
if useAgg and c14
    alert(pre + "Quiet: volume dried up", alert.freq_once_per_bar_close)
if useAgg and c15
    alert(pre + "Volume echo (high, 2-4 average bars, high)", alert.freq_once_per_bar_close)

← 返回列表