本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Breakout Alert: squeeze depth, release signals, regime filter, higher-timeframe panel and outcome stats
// Squeeze and momentum code is based on "Squeeze Momentum Indicator [LazyBear]" by LazyBear (open-source on
// TradingView, 2014), itself derived from John Carter's TTM Squeeze ("Mastering the Trade", ch. 11).
// Full credit to LazyBear and John Carter.
// New in this version: Pine v6 rewrite, the BB-multiplier fix LazyBear posted later (with a switch to reproduce the
// published code), three squeeze depth levels, "squeeze fired" signals in the momentum direction, an optional public
// regime filter (200 MA, ADX), text labels (FIRED UP / FIRED DOWN), a live status label, a higher-timeframe squeeze
// panel with a colour legend, alerts and an outcome stats table.
//@version=6
indicator("Breakout Alert", shorttitle = "Breakout Alert", overlay = false, precision = 4, max_labels_count = 500)
// ───────── Original inputs (LazyBear defaults) ─────────
grpO = "Squeeze (original settings)"
length = input.int(20, "BB length", minval = 2, group = grpO)
mult = input.float(2.0, "BB mult factor", step = 0.1, group = grpO)
lengthKC = input.int(20, "KC length", minval = 2, group = grpO)
multKC = input.float(1.5, "KC mult factor (main squeeze)", step = 0.1, group = grpO)
useTR = input.bool(true, "Use true range (KC)", group = grpO)
legacyBB = input.bool(false, "Reproduce the published code's BB bug (BB uses KC mult)", group = grpO, tooltip = "The code shown on the original page used the KC multiplier for the Bollinger Bands. LazyBear later posted a fix. Off = fixed version.")
// ───────── Depth levels ─────────
grpD = "Squeeze depth"
useDepth = input.bool(true, "Show depth (tight / normal / loose)", group = grpD)
kcTight = input.float(1.0, "Tight KC mult", step = 0.1, group = grpD)
kcLoose = input.float(2.0, "Loose KC mult", step = 0.1, group = grpD)
// ───────── Signals + regime ─────────
grpR = "Release signals and regime filter"
minBars = input.int(4, "Min squeeze bars before a release counts", minval = 1, group = grpR)
regime = input.string("200 MA direction", "Regime filter", options = ["Off", "200 MA direction", "200 MA direction + ADX"], group = grpR)
maLen = input.int(200, "Trend MA length", minval = 20, group = grpR)
adxLen = input.int(14, "ADX length", minval = 2, group = grpR)
adxMin = input.float(18.0, "Min ADX", minval = 0, group = grpR)
allowShort = input.bool(true, "Show short (bearish) releases", group = grpR)
onPrice = input.bool(false, "Also show FIRED labels on the price chart", group = grpR)
showBlk = input.bool(true, "Show filtered-out releases (grey)", group = grpR)
// ───────── HTF panel ─────────
grpH = "Higher-timeframe panel"
showH = input.bool(true, "Show HTF panel", group = grpH)
tf1 = input.timeframe("W", "Timeframe 1", group = grpH)
tf2 = input.timeframe("M", "Timeframe 2", group = grpH)
// ───────── Stats ─────────
grpS = "Outcome stats"
showTbl = input.bool(true, "Show stats table", group = grpS)
fwd = input.int(20, "Measure the move after N bars", minval = 1, group = grpS)
// ───────── Core (LazyBear) ─────────
sqzCalc(float kcM) =>
basis = ta.sma(close, length)
dev = (legacyBB ? multKC : mult) * ta.stdev(close, length)
ma = ta.sma(close, lengthKC)
rng = useTR ? ta.tr : high - low
rma = ta.sma(rng, lengthKC)
(basis - dev > ma - rma * kcM) and (basis + dev < ma + rma * kcM)
sqzOn = sqzCalc(multKC)
sqzTight = sqzCalc(kcTight)
sqzLoose = sqzCalc(kcLoose)
val = ta.linreg(close - math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), lengthKC, 0)
// HTF state of the main squeeze, non-repainting (previous closed HTF bar)
htfState(string tf) =>
request.security(syminfo.tickerid, tf, sqzOn[1], lookahead = barmerge.lookahead_on)
htfMom(string tf) =>
request.security(syminfo.tickerid, tf, val[1], lookahead = barmerge.lookahead_on)
h1On = htfState(tf1)
h2On = htfState(tf2)
h1Mom = htfMom(tf1)
h2Mom = htfMom(tf2)
// ───────── Release ("fired") signals ─────────
sqzBars = ta.barssince(not sqzOn) // consecutive squeeze bars up to the previous bar
fired = sqzOn[1] and not sqzOn and nz(sqzBars[1]) >= minBars
trendMA = ta.sma(close, maLen)
[diPlus, diMinus, adx] = ta.dmi(adxLen, adxLen)
adxOK = regime != "200 MA direction + ADX" or adx >= adxMin
upOK = regime == "Off" or (close > trendMA and adxOK)
dnOK = regime == "Off" or (close < trendMA and adxOK)
firedUp = fired and val > 0 and upOK
firedDn = fired and val < 0 and dnOK and allowShort
firedBlocked = fired and not firedUp and not firedDn
// ───────── Stats: direction-adjusted move N bars after the signal ─────────
var array<int> sBar = array.new<int>()
var array<float> sPx = array.new<float>()
var array<int> sDir = array.new<int>()
var float sumUp = 0.0
var int nUp = 0
var int wUp = 0
var float sumDn = 0.0
var int nDn = 0
var int wDn = 0
var float base = 0.0
var int nBase = 0
if firedUp or firedDn
sBar.push(bar_index)
sPx.push(close)
sDir.push(firedUp ? 1 : -1)
if sBar.size() > 0
for i = 0 to sBar.size() - 1
if bar_index - sBar.get(i) == fwd
r = close / sPx.get(i) - 1
if sDir.get(i) == 1
sumUp += r
nUp += 1
wUp += r > 0 ? 1 : 0
else
sumDn += -r
nDn += 1
wDn += r < 0 ? 1 : 0
if bar_index >= fwd
base += close / close[fwd] - 1
nBase += 1
// ───────── Plots ─────────
bcolor = val > 0 ? (val > nz(val[1]) ? color.lime : color.green) : (val < nz(val[1]) ? color.red : color.maroon)
plot(val, "Momentum", bcolor, 4, plot.style_histogram)
dotCol = not sqzLoose ? color.new(color.blue, 0) : not useDepth ? (sqzOn ? color.black : color.gray) : sqzTight ? color.new(#ff9800, 0) : sqzOn ? color.new(color.red, 0) : color.new(color.gray, 0)
plot(0, "Squeeze dots", dotCol, 3, plot.style_cross)
depthName = sqzTight ? "tight squeeze" : sqzOn ? "squeeze" : sqzLoose ? "loose squeeze" : "no squeeze"
addLbl(string txt, color col, bool atTop, string sz) =>
label.new(bar_index, 0, txt, color = col, textcolor = color.white, style = atTop ? label.style_label_down : label.style_label_up, size = sz)
if onPrice
label.new(bar_index, atTop ? high : low, txt, yloc = atTop ? yloc.abovebar : yloc.belowbar, color = col, textcolor = color.white, style = atTop ? label.style_label_down : label.style_label_up, size = sz, force_overlay = true)
if firedUp
addLbl("FIRED UP", color.new(color.green, 10), false, size.small)
if firedDn
addLbl("FIRED DOWN", color.new(color.red, 10), true, size.small)
if showBlk and firedBlocked
addLbl("fired (filtered)", color.new(color.gray, 20), val < 0, size.tiny)
// status at the last bar
var label lNow = na
if barstate.islast
label.delete(lNow)
nBars = sqzOn ? str.tostring(nz(sqzBars)) + " bars" : ""
lNow := label.new(bar_index + 2, val, "Now: " + depthName + (nBars != "" ? " (" + nBars + ")" : "") + " · momentum " + (val > 0 ? "up" : "down") + (val > nz(val[1]) ? ", rising" : ", falling"), color = color.new(color.gray, 20), textcolor = color.white, style = label.style_label_left, size = size.normal)
// ───────── Alerts ─────────
alertcondition(sqzOn and not sqzOn[1], "Squeeze on", "Breakout Alert: squeeze started on {{ticker}}")
alertcondition(firedUp, "Squeeze fired up", "Breakout Alert: fired up (regime OK) on {{ticker}}")
alertcondition(firedDn, "Squeeze fired down", "Breakout Alert: fired down (regime OK) on {{ticker}}")
// ───────── Tables ─────────
var table th = table.new(position.top_left, 3, 5, bgcolor = color.new(color.black, 25), border_width = 1)
stateTxt(bool on) => on ? "in squeeze" : "no squeeze"
if showH and barstate.islast
th.cell(0, 0, "Higher timeframes (last closed bar)", text_color = color.white, text_size = size.small)
th.merge_cells(0, 0, 2, 0)
th.cell(0, 3, "Dots: orange = tight, red = squeeze, grey = loose, blue = none", text_color = color.silver, text_size = size.small)
th.merge_cells(0, 3, 2, 3)
th.cell(0, 4, "Histogram: green = up momentum, red = down", text_color = color.silver, text_size = size.small)
th.merge_cells(0, 4, 2, 4)
th.cell(0, 1, tf1, text_color = color.white, text_size = size.small)
th.cell(1, 1, stateTxt(h1On), text_color = h1On ? color.orange : color.silver, text_size = size.small)
th.cell(2, 1, na(h1Mom) ? "-" : h1Mom > 0 ? "momentum up" : "momentum down", text_color = nz(h1Mom) > 0 ? color.lime : color.red, text_size = size.small)
th.cell(0, 2, tf2, text_color = color.white, text_size = size.small)
th.cell(1, 2, stateTxt(h2On), text_color = h2On ? color.orange : color.silver, text_size = size.small)
th.cell(2, 2, na(h2Mom) ? "-" : h2Mom > 0 ? "momentum up" : "momentum down", text_color = nz(h2Mom) > 0 ? color.lime : color.red, text_size = size.small)
var table ts = table.new(position.top_right, 4, 5, bgcolor = color.new(color.black, 25), border_width = 1)
fmt(x) => na(x) ? "-" : str.tostring(x * 100, "#.##") + "%"
if showTbl and barstate.islast
ts.cell(0, 0, "After a release: move over the next " + str.tostring(fwd) + " bars", text_color = color.white, text_size = size.small)
ts.merge_cells(0, 0, 3, 0)
ts.cell(0, 1, "Signal", text_color = color.silver, text_size = size.small)
ts.cell(1, 1, "Count", text_color = color.silver, text_size = size.small)
ts.cell(2, 1, "Avg move (signal direction)", text_color = color.silver, text_size = size.small)
ts.cell(3, 1, "Went the right way", text_color = color.silver, text_size = size.small)
ts.cell(0, 2, "FIRED UP", text_color = color.lime, text_size = size.small)
ts.cell(1, 2, str.tostring(nUp), text_color = color.white, text_size = size.small)
ts.cell(2, 2, fmt(nUp > 0 ? sumUp / nUp : na), text_color = color.white, text_size = size.small)
ts.cell(3, 2, nUp > 0 ? str.tostring(100.0 * wUp / nUp, "#") + "%" : "-", text_color = color.white, text_size = size.small)
ts.cell(0, 3, "FIRED DOWN", text_color = color.red, text_size = size.small)
ts.cell(1, 3, str.tostring(nDn), text_color = color.white, text_size = size.small)
ts.cell(2, 3, fmt(nDn > 0 ? sumDn / nDn : na), text_color = color.white, text_size = size.small)
ts.cell(3, 3, nDn > 0 ? str.tostring(100.0 * wDn / nDn, "#") + "%" : "-", text_color = color.white, text_size = size.small)
ts.cell(0, 4, "Any bar, long (for comparison)", text_color = color.silver, text_size = size.small)
ts.cell(1, 4, str.tostring(nBase), text_color = color.silver, text_size = size.small)
ts.cell(2, 4, fmt(nBase > 0 ? base / nBase : na), text_color = color.silver, text_size = size.small)
ts.cell(3, 4, "", text_color = color.silver, text_size = size.small)