本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("BB Strategy - Candle Break Confirmation (Auto Alerts)", overlay=true)
// ─────────────────────────────
// Bollinger Band settings
length = input.int(20, title="BB Length")
stdDev = input.float(1.5, title="BB Std Dev")
basis = ta.sma(close, length)
dev = stdDev * ta.stdev(close, length)
upperBB = basis + dev
lowerBB = basis - dev
// ─────────────────────────────
// Conditions for previous candle (strict close above/below)
// Note: In v6, if history is insufficient on the first few bars,
// these safely evaluate to 'false' instead of returning 'na' errors.
prevAboveUpper = low[1] > upperBB[1] // Previous candle fully above upper BB
prevBelowLower = high[1] < lowerBB[1] // Previous candle fully below lower BB
// Current candle break confirmation
sellSignal = prevAboveUpper and low < low[1] // Break previous candle low → Sell
buySignal = prevBelowLower and high > high[1] // Break previous candle high → Buy
// ─────────────────────────────
// Plot Bollinger Bands
plot(basis, title="Basis", color=color.gray)
plot(upperBB, title="Upper BB", color=color.red)
plot(lowerBB, title="Lower BB", color=color.green)
// ─────────────────────────────
// Plot signals
plotshape(sellSignal, title="Sell Signal", location=location.abovebar,
color=color.red, style=shape.labeldown, text="SELL", size=size.tiny)
plotshape(buySignal, title="Buy Signal", location=location.belowbar,
color=color.lime, style=shape.labelup, text="BUY", size=size.tiny)
// ─────────────────────────────
// Combine alerts — single alert triggers for both
alertSignal = buySignal or sellSignal
// When alert triggers, message tells if it's BUY or SELL
alertcondition(alertSignal, title="BB Auto Buy/Sell Alert",
message="BB Strategy Signal — {{ticker}}: {{close}} → " +
"🟢 BUY" + " if candle broke above previous low below lower BB, " +
"🔴 SELL" + " if candle broke below previous high above upper BB. Check chart for confirmation.")