Bollinger Scanner - Strong Opening BOTH Bands

Beatrizgonza · study · 166 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("Bollinger Scanner - Strong Opening BOTH Bands", overlay=true, dynamic_requests=true)
 
// SETTINGS
 
length = input.int(20, "Bollinger Length", minval=2)
mult = input.float(2.0, "Standard Deviations", minval=0.1)
 
widthLookback = input.int(50, "Width Comparison Lookback", minval=5)
widthFactor = input.float(0.60, "Minimum Width / Average Width", minval=0.1, step=0.01)
minWidthPct = input.float(0.0, "Minimum Band Width (%) - 0 Disables", minval=0.0, step=0.1)
 
minOutwardATR = input.float(0.10, "Minimum Outward Movement of EACH Band (ATR)", minval=0.01, step=0.01)
flatThreshold = input.float(0.02, "Sideways Threshold (ATR)", minval=0.0, step=0.01)
 
showTable = input.bool(true, "Show Table")
showBands = input.bool(true, "Show Bollinger Bands")
showSignals = input.bool(true, "Show CALL and PUT")
 
tickerList = input.string("AMZN,AAPL,GOOG,META,MSFT,NVDA,TSLA,SP:SPX,CBOE:VIX,AMEX:SPY,NASDAQ:QQQ,AMEX:DIA,AMEX:IWM,AMD,MU,AVGO,QCOM,TSM,INTC,CBRS,SNDK,AAOI,COHR,IPGP,LITE,POET,PLTR,COIN,LLY,OKLO,NFLX,NOW,SHOP,NASDAQ:SOXX,AMEX:USO", "Tickers to Monitor")
 
// CONDITIONS
//
// Each outer band must open outward with sufficient strength:
// Upper band must rise.
// Lower band must fall.
//
// A large total width alone is NOT sufficient.
// Weak or flat outward movement is filtered out.
//
// CALL: both bands open strongly and the middle band rises.
// PUT: both bands open strongly and the middle band falls.
//
// If the middle band is sideways, wait for direction.
// Both bands must still satisfy the opening filters at that time.
//
// If either outer band closes, becomes flat, or opens too weakly,
// the current bar has no CALL or PUT.
//
// Labels appear on every qualifying chart bar.
// Alerts trigger when a qualifying condition starts or changes direction.
 
f_conditions() =>
    [basis, upper, lower] = ta.bb(close, length, mult)
 
    atrValue = ta.atr(14)
 
    width = upper - lower
 
    widthPct = not na(basis) and math.abs(basis) > 0 ? 100.0 * width / math.abs(basis) : na
 
    averageWidth = ta.sma(widthPct, widthLookback)[1]
 
    wideEnough = not na(averageWidth) and averageWidth > 0 and widthPct >= averageWidth * widthFactor and widthPct >= minWidthPct
 
    // Measure each outer band's outward movement separately.
    upperOutward = upper - upper[1]
    lowerOutward = lower[1] - lower
 
    requiredMovement = atrValue * minOutwardATR
 
    upperOpeningStrong = atrValue > 0 and upperOutward >= requiredMovement
    lowerOpeningStrong = atrValue > 0 and lowerOutward >= requiredMovement
 
    bothOpeningStrong = upperOpeningStrong and lowerOpeningStrong
 
    // Determine whether the middle band rises, falls, or is sideways.
    slope = basis - basis[1]
    tolerance = atrValue * flatThreshold
 
    validCondition = wideEnough and bothOpeningStrong
 
    // 1 = CALL, -1 = PUT, 0 = no qualifying condition.
    direction = validCondition ? (slope > tolerance ? 1 : slope < -tolerance ? -1 : 0) : 0
 
    // Alert on entry into a qualifying state or direction change.
    signal = direction != 0 and direction != nz(direction[1], 0) ? direction : 0
 
    [basis, upper, lower, direction, signal]
 
f_data() =>
    [basis, upper, lower, direction, signal] = f_conditions()
    [direction, signal, close, time, time_close]
 
// SCANNER
 
var array<string> symbols = str.split(tickerList, ",")
varip array<int> barTimes = array.new_int(array.size(symbols), 0)
varip array<bool> sent = array.new_bool(array.size(symbols), false)
 
var table panel = table.new(position.top_right, 2, array.size(symbols) + 1)
 
if barstate.isfirst
    if array.size(symbols) > 40
        runtime.error("Use a maximum of 40 tickers.")
 
    for i = 0 to array.size(symbols) - 1
        if str.length(str.trim(array.get(symbols, i))) == 0
            runtime.error("Empty ticker in the list. Check the commas.")
 
if barstate.islast and showTable
    table.cell(panel, 0, 0, "TICKER", bgcolor=color.gray, text_color=color.white)
    table.cell(panel, 1, 0, timeframe.period, bgcolor=color.gray, text_color=color.white)
 
string messages = ""
 
for i = 0 to array.size(symbols) - 1
    symbol = str.trim(array.get(symbols, i))
 
    [direction, signal, price, barTime, barEnd] = request.security(symbol, timeframe.period, f_data(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
 
    // Reset alert tracking on each new bar.
    if not na(barTime) and barTime != array.get(barTimes, i)
        array.set(barTimes, i, barTime)
        array.set(sent, i, false)
 
    // Live-alert restriction only.
    // Historical chart labels remain visible after market close.
    currentBar = not na(barTime) and not na(barEnd) and timenow >= barTime and timenow < barEnd
 
    // Maximum one alert per ticker per bar.
    if barstate.isrealtime and currentBar and signal != 0 and not array.get(sent, i)
        signalText = signal == 1 ? "CALL" : "PUT"
        messages += symbol + " | " + signalText + " | Strong opening of BOTH bands | Price: " + str.tostring(price, "#.####") + "\n"
        array.set(sent, i, true)
 
    // Display the current qualifying condition.
    if barstate.islast and showTable
        statusText = na(price) ? "NO DATA" : direction == 1 ? "CALL" : direction == -1 ? "PUT" : "-"
 
        background = na(price) ? color.gray : direction == 1 ? color.new(color.green, 20) : direction == -1 ? color.new(color.red, 20) : color.new(color.gray, 80)
 
        table.cell(panel, 0, i + 1, symbol, text_color=color.white, bgcolor=color.new(color.black, 20), text_size=size.tiny)
        table.cell(panel, 1, i + 1, statusText, text_color=color.white, bgcolor=background, text_size=size.tiny)
 
// LIVE ALERTS
// Conditions can change before the current bar closes.
 
if barstate.isrealtime and str.length(messages) > 0
    alert("Bollinger: Strong Opening BOTH Bands | TF " + timeframe.period + "\n" + messages, alert.freq_all)
 
// BOLLINGER BANDS FOR THE CHART SYMBOL
 
[chartBasis, chartUpper, chartLower, chartDirection, chartSignal] = f_conditions()
 
plot(showBands ? chartBasis : na, "Middle Band", color=color.orange)
 
upperPlot = plot(showBands ? chartUpper : na, "Upper Band", color=color.blue)
lowerPlot = plot(showBands ? chartLower : na, "Lower Band", color=color.blue)
 
fill(upperPlot, lowerPlot, color=color.new(color.blue, 94))
 
// LAST AVAILABLE DAY
// Uses the chart symbol's exchange timezone.
// Historical qualifying labels remain after market close.
 
lastDay = year(time) == year(last_bar_time) and month(time) == month(last_bar_time) and dayofmonth(time) == dayofmonth(last_bar_time)
 
// Label every qualifying bar.
 
callMark = chartDirection == 1
putMark = chartDirection == -1
 
plotshape(showSignals and lastDay and callMark, title="CALL Signal", style=shape.labelup, location=location.belowbar, color=color.green, text="CALL", textcolor=color.white, size=size.tiny)
 
plotshape(showSignals and lastDay and putMark, title="PUT Signal", style=shape.labeldown, location=location.abovebar, color=color.red, text="PUT", textcolor=color.white, size=size.tiny)

← 返回列表