本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(shorttitle="Daily BB - Boring Money", title="Daily Bollinger Bands + Stretch - Boring Money Concepts", overlay=true)
TT_CONF = "ON = use last COMPLETED daily bar (non-repainting). OFF = developing daily bar (repaints)."
TT_TRIG = "Close = bar must CLOSE beyond threshold (fewer false triggers, one bar later). Wick = high/low excursion (faster, noisier)."
TT_BOTH = "ON = alert only when Z AND ATR stretch both exceed thresholds. Filters squeeze false-triggers."
tf = input.timeframe("D", "Band Timeframe")
useConfirmed = input.bool(true, "Use Completed Daily Bar (No Repaint)", tooltip = TT_CONF)
length = input.int(20, "BB Length (Recent Window)", minval = 1)
maType = input.string("SMA", "Basis MA Type", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
src = input(close, "Source")
mult = input.float(2.0, "StdDev", minval = 0.001, maxval = 50)
atrLen = input.int(14, "ATR Length", minval = 1)
offset = input.int(0, "Offset", minval = -500, maxval = 500, display = display.none)
zAlert = input.float(2.5, "Z Alert Threshold", minval = 0.1, group = "Alerts")
atrAlert = input.float(2.0, "ATR Stretch Alert Threshold", minval = 0.1, group = "Alerts")
trigOn = input.string("Close", "Trigger On", options = ["Close", "Wick"], tooltip = TT_TRIG, group = "Alerts")
requireBoth = input.bool(true, "Require Z AND ATR Agreement", tooltip = TT_BOTH, group = "Alerts")
ma(source, len, _type) =>
switch _type
"SMA" => ta.sma(source, len)
"EMA" => ta.ema(source, len)
"SMMA (RMA)" => ta.rma(source, len)
"WMA" => ta.wma(source, len)
"VWMA" => ta.vwma(source, len)
dailyCalc(int shift) =>
b = ma(src, length, maType)
d = mult * ta.stdev(src, length)
u = b + d
l = b - d
a = ta.atr(atrLen)
[b[shift], u[shift], l[shift], a[shift]]
shift = useConfirmed ? 1 : 0
look = useConfirmed ? barmerge.lookahead_on : barmerge.lookahead_off
[dBasis, dUpper, dLower, dATR] = request.security(syminfo.tickerid, tf, dailyCalc(shift), gaps = barmerge.gaps_off, lookahead = look)
// Z-score stretch (sigma units, recent window)
sigma = (dUpper - dBasis) / mult
zClose = sigma > 0 ? (close - dBasis) / sigma : na
zHigh = sigma > 0 ? (high - dBasis) / sigma : na
zLow = sigma > 0 ? (low - dBasis) / sigma : na
// ATR stretch (daily ATR units from Daily BB Basis)
aClose = dATR > 0 ? (close - dBasis) / dATR : na
aHigh = dATR > 0 ? (high - dBasis) / dATR : na
aLow = dATR > 0 ? (low - dBasis) / dATR : na
// Trigger values
useClose = trigOn == "Close"
zUpVal = useClose ? zClose : zHigh
zDnVal = useClose ? zClose : zLow
aUpVal = useClose ? aClose : aHigh
aDnVal = useClose ? aClose : aLow
upCond = requireBoth ? (zUpVal >= zAlert and aUpVal >= atrAlert) : (zUpVal >= zAlert or aUpVal >= atrAlert)
dnCond = requireBoth ? (zDnVal <= -zAlert and aDnVal <= -atrAlert) : (zDnVal <= -zAlert or aDnVal <= -atrAlert)
// Once per day per direction
newDay = ta.change(time("D")) != 0
var bool upFired = false
var bool dnFired = false
if newDay
upFired := false
dnFired := false
upSignal = upCond and not upFired
dnSignal = dnCond and not dnFired
if upSignal
upFired := true
if dnSignal
dnFired := true
// Chart plots
plot(dBasis, "Daily BB Basis", color = #1A237E, offset = offset)
p1 = plot(dUpper, "Daily BB Upper", color = #F23645, offset = offset)
p2 = plot(dLower, "Daily BB Lower", color = #089981, offset = offset)
fill(p1, p2, title = "Daily BB Fill", color = color.rgb(33, 150, 243, 95))
plotshape(upSignal, "Stretch Up Signal", shape.triangledown, location.abovebar, color = #F23645, size = size.small)
plotshape(dnSignal, "Stretch Down Signal", shape.triangleup, location.belowbar, color = #089981, size = size.small)
// Data columns (off-chart)
plot(dATR, "Daily ATR", display = display.data_window)
plot(zClose, "Daily BB Z Close", display = display.data_window)
plot(zHigh, "Daily BB Z High", display = display.data_window)
plot(zLow, "Daily BB Z Low", display = display.data_window)
plot(aClose, "Daily ATR Stretch Close", display = display.data_window)
plot(aHigh, "Daily ATR Stretch High", display = display.data_window)
plot(aLow, "Daily ATR Stretch Low", display = display.data_window)
plot(upSignal ? 1 : 0, "Daily Stretch Signal Up", display = display.data_window)
plot(dnSignal ? 1 : 0, "Daily Stretch Signal Down", display = display.data_window)
// Alerts
alertcondition(upSignal, "Daily Stretch Up", "{{ticker}} Daily stretch UP - review chart")
alertcondition(dnSignal, "Daily Stretch Down", "{{ticker}} Daily stretch DOWN - review chart")