本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// author © OnnoP
// Volume-confirmed breakout detector, v3: combines the "How to confirm
// breakouts" rule (a breakout is only credible on heavy volume) with the
// HawkEye Volume Indicator's bar classification (green = buying volume,
// red = selling volume, gray = neutral/noise). The volume columns are colored
// HawkEye style; a breakout is CONFIRMED only when price clears the level by an
// ATR buffer, relative volume is high, HawkEye agrees with the direction and the
// bar closed with conviction (near its own high/low). Optional filters: a tight
// range before the breakout and higher-timeframe trend alignment.
indicator("Volume breakout", overlay=false, format=format.volume)
// ══════════════════════════════════════════════════════════════════════
// INPUTS
// ══════════════════════════════════════════════════════════════════════
GRPBRK = "Breakout detection"
iLookback = input.int(20, "Breakout lookback (bars)", minval=5, maxval=200, group=GRPBRK,
tooltip="A bullish breakout fires when the bar CLOSES above the highest high of this many prior bars (the current bar excluded) plus the buffer below; a bearish breakdown when it closes below the lowest low minus the buffer. 20 on a daily chart ≈ a one-month range.")
iBufAtr = input.float(0.1, "Breakout buffer (× ATR)", minval=0.0, maxval=2.0, step=0.05, group=GRPBRK,
tooltip="The close must clear the broken level by at least this fraction of the 14-bar ATR, measured before the breakout bar. Filters one-tick pokes through the level. 0 disables the buffer.")
iMaxBaseAtr = input.float(0.0, "Max range width (× ATR)", minval=0.0, maxval=30.0, step=0.5, group=GRPBRK,
tooltip="Only confirm breakouts out of a range (highest high to lowest low over the lookback) no wider than this many ATRs. Breakouts from tight consolidations follow through more often than breakouts from wide, choppy ranges. With a 20-bar lookback, 4-6 is a tight base. 0 disables the filter.")
iMinClv = input.float(0.6, "Min close location on breakout bar", minval=0.5, maxval=1.0, step=0.05, group=GRPBRK,
tooltip="Where the breakout bar must close within its own range to count as conviction. 0.6 = a bullish breakout bar must close in the top 40% of its range (a bearish one in the bottom 40%). Heavy volume with a mid-range close is absorption, not a breakout, and stays 'suspect'. 0.5 disables the check.")
GRPVOL = "Volume confirmation"
iVolLen = input.int(20, "Volume average length", minval=5, maxval=200, group=GRPVOL,
tooltip="Length of the volume baseline that defines 'normal' volume: bars for Median and SMA, sessions for Time of day. Relative volume = bar volume / this baseline.")
iVolBase = input.string("Time of day", "Volume baseline", options=["Time of day", "Median", "SMA"], group=GRPVOL,
tooltip="Time of day (default): on intraday charts each bar is compared with the median volume of the same time slot over the last sessions, so the opening bars are not always read as huge volume. Until a slot has 5 sessions of history, and on daily and higher charts, it uses the Median. Median: one news spike does not inflate the baseline for the next 20 bars. SMA: the classic simple average, kept for comparison. Every baseline excludes the current bar, so a heavy breakout bar does not raise its own yardstick.")
iVolMult = input.float(1.5, "Confirmation multiple (× baseline)", minval=1.0, maxval=5.0, step=0.1, group=GRPVOL,
tooltip="The breakout bar's volume must be at least this many times the baseline to count as CONFIRMED.")
GRPHTF = "Higher-timeframe trend"
iHtfOn = input.bool(false, "Require higher-timeframe trend alignment", group=GRPHTF,
tooltip="Only confirm bullish breakouts while the higher timeframe's last completed bar closed above its EMA, and bearish ones while it closed below. Uses completed bars only, so it does not repaint. Ignored when the chosen timeframe is not higher than the chart's.")
iHtfTf = input.timeframe("W", "Higher timeframe", group=GRPHTF)
iHtfLen = input.int(20, "Higher-timeframe EMA length", minval=2, maxval=500, group=GRPHTF)
// HawkEye constants (the classic indicator's defaults).
HAWK_LEN = 200 // length of the range and volume averages HawkEye compares each bar against
HAWK_DIV = 3.6 // width of the neutral band around the previous bar's midpoint: mid ± range/divisor
ATR_LEN = 14 // ATR used for the breakout buffer and the range width filter
// ══════════════════════════════════════════════════════════════════════
// HAWKEYE CLASSIFICATION (port of the classic HawkEye Volume Indicator)
// ══════════════════════════════════════════════════════════════════════
rng = high - low
// Until 200 bars exist the classic averages are na and nothing could confirm,
// so fall back to the shorter volume-average length.
rngAvg = nz(ta.sma(rng, HAWK_LEN), ta.sma(rng, iVolLen))
hawkVolA = nz(ta.sma(nz(volume), HAWK_LEN), ta.sma(nz(volume), iVolLen))
high1 = high[1]
low1 = low[1]
mid1 = hl2[1]
u1 = mid1 + (high1 - low1) / HAWK_DIV
d1 = mid1 - (high1 - low1) / HAWK_DIV
rEnabled = (rng > rngAvg and close < d1 and volume > hawkVolA) or close < mid1
gEnabled = close > mid1
or (rng > rngAvg and close > u1 and volume > hawkVolA)
or (high > high1 and rng < rngAvg / 1.5 and volume < hawkVolA)
or (low < low1 and rng < rngAvg / 1.5 and volume > hawkVolA)
grEnabled = (rng > rngAvg and close > d1 and close < u1 and volume > hawkVolA and volume < hawkVolA * 1.5 and volume > volume[1])
or (rng < rngAvg / 1.5 and volume < hawkVolA / 1.5)
or (close > d1 and close < u1)
// Same precedence as the original: gray first, then green, then red, else blue.
hawkGray = grEnabled
hawkGreen = not hawkGray and gEnabled
hawkRed = not hawkGray and not hawkGreen and rEnabled
hawkColor = hawkGray ? color.gray : hawkGreen ? color.green : hawkRed ? color.red : color.blue
// ══════════════════════════════════════════════════════════════════════
// VOLUME BASELINE
// ══════════════════════════════════════════════════════════════════════
hasVol = not na(volume) and volume > 0
// Both bar baselines are computed every bar (Pine needs that for consistency) and
// lagged one bar so the breakout bar is not part of its own baseline.
volSma = ta.sma(nz(volume), iVolLen)[1]
volMed = ta.percentile_nearest_rank(nz(volume), iVolLen, 50)[1]
// Time of day: per time slot, the volumes of the last iVolLen sessions.
type VolSlot
array<float> vols
var slots = map.new<int, VolSlot>()
float volTod = na
if iVolBase == "Time of day" and timeframe.isintraday
key = hour(time) * 3600 + minute(time) * 60 + second(time)
slot = slots.get(key)
if na(slot)
slot := VolSlot.new(array.new<float>())
slots.put(key, slot)
if slot.vols.size() >= 5
volTod := slot.vols.median() // read before this bar is added
if hasVol and barstate.isconfirmed // committed bars only, so a live bar is never pushed twice
slot.vols.push(volume)
if slot.vols.size() > iVolLen
slot.vols.shift()
volAvg = iVolBase == "SMA" ? volSma : nz(volTod, volMed)
relVol = hasVol and volAvg > 0 ? volume / volAvg : 0.0
volOk = relVol >= iVolMult
// ══════════════════════════════════════════════════════════════════════
// BREAKOUT LOGIC
// ══════════════════════════════════════════════════════════════════════
hiLevel = ta.highest(high, iLookback)[1]
loLevel = ta.lowest(low, iLookback)[1]
atr1 = ta.atr(ATR_LEN)[1] // before the breakout bar, so its own range does not widen the buffer
upTrig = hiLevel + iBufAtr * nz(atr1)
dnTrig = loLevel - iBufAtr * nz(atr1)
// Range width in ATRs: a tight base makes a more reliable breakout.
baseAtr = atr1 > 0 ? (hiLevel - loLevel) / atr1 : na
baseOk = iMaxBaseAtr == 0 or na(baseAtr) or baseAtr <= iMaxBaseAtr
// Higher-timeframe trend from its last completed bar ([1] with lookahead on = no repaint).
[htfClose, htfEma] = request.security(syminfo.tickerid, iHtfTf, [close[1], ta.ema(close, iHtfLen)[1]], lookahead=barmerge.lookahead_on)
htfActive = iHtfOn and timeframe.in_seconds(iHtfTf) > timeframe.in_seconds()
htfOkUp = not htfActive or na(htfEma) or htfClose > htfEma
htfOkDn = not htfActive or na(htfEma) or htfClose < htfEma
// Conviction of the bar: 1 = closed at its high, 0 = closed at its low.
clv = high > low ? (close - low) / (high - low) : 0.5
clvOkUp = clv >= iMinClv
clvOkDn = clv <= 1.0 - iMinClv
brkUp = close > upTrig and close[1] <= upTrig[1]
brkDn = close < dnTrig and close[1] >= dnTrig[1]
hawkOkUp = hawkGreen // a bullish breakout needs a green HawkEye bar
hawkOkDn = hawkRed // a bearish breakdown needs a red one
confirmedUp = brkUp and volOk and hawkOkUp and clvOkUp and baseOk and htfOkUp
confirmedDn = brkDn and volOk and hawkOkDn and clvOkDn and baseOk and htfOkDn
suspectUp = brkUp and hasVol and not confirmedUp // no volume data = no verdict, not "suspect"
suspectDn = brkDn and hasVol and not confirmedDn
// ══════════════════════════════════════════════════════════════════════
// VISUALS
// ══════════════════════════════════════════════════════════════════════
plot(volume, "Volume", color=hawkColor, style=plot.style_columns)
plot(hawkVolA, "HawkEye avg volume", color=color.new(color.yellow, 0))
// Highlight the confirmed breakout bar's own column.
plot(confirmedUp or confirmedDn ? volume : na, "Confirmed breakout column",
color=confirmedUp ? color.lime : color.red, style=plot.style_columns)
// Markers in this pane, floating just above the bar's volume column.
markerY = volume * 1.15
plotshape(confirmedUp ? markerY : na, "Confirmed bullish breakout", shape.circle, location.absolute,
color=color.new(color.lime, 0), size=size.tiny)
plotshape(confirmedDn ? markerY : na, "Confirmed bearish breakdown", shape.circle, location.absolute,
color=color.new(color.red, 0), size=size.tiny)
// Suspect (unconfirmed) breakouts are not drawn - they only feed the "Suspect breakout" alert.
// ══════════════════════════════════════════════════════════════════════
// ALERTS
// ══════════════════════════════════════════════════════════════════════
// Every signal is close-based, so alerts only fire on the confirmed bar close
// and never on a live bar that later reverts.
alertcondition(confirmedUp and barstate.isconfirmed, "Confirmed bullish breakout", "Bullish breakout with high relative volume, green HawkEye bar and a strong close - institutional fuel behind it.")
alertcondition(confirmedDn and barstate.isconfirmed, "Confirmed bearish breakdown", "Bearish breakdown with high relative volume, red HawkEye bar and a strong close - institutional fuel behind it.")
alertcondition((suspectUp or suspectDn) and barstate.isconfirmed, "Suspect breakout", "Breakout WITHOUT full confirmation (volume, HawkEye, close, range or trend) - possible false move, be careful.")