BBW + Squeeze

kroo_sol · study · 73 行 · 点赞 10 · TradingView 原页

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

Pine Script

//@version=6
// BBW + Squeeze v2
// Changes vs. v1 (details and sources: REVIEW.md):
//  1. Lookback is now a calendar-time window (days), not a bar count. A monotonic
//     deque over `time` keeps the window minimum, so 4h crypto, 1D crypto and 1D
//     stocks all compare against the same ~6 calendar months without switching
//     inputs. Pine exposes no session hours (syminfo.session = regular/extended
//     only), so any days->bars conversion would be a market-type heuristic.
//  2. Warm-up gate is time-based (>= window of valid BBW history) instead of
//     bar_index; the low line is hidden until the window is full, so a
//     partial-history low is never shown as a "6-month low".
//  3. Timeframes with fewer than MIN_BARS bars per window (weekly, monthly) show
//     no squeeze: a minimum over ~26 or ~6 bars is not an extreme.
//  4. Colors from the user's chart (screenshot) as input.color(); transparency
//     lives in the default value so the input stays editable (Style/Inputs tab).
//  5. alertcondition() removed: the script is for visual reading only.
//  6. Low line drawn as step line (jumps when an old low leaves the window are
//     window bookkeeping, not a market event). Numeric inputs hidden from the
//     status line to keep it short on mobile.
// Unchanged on purpose: BBW formula (identical to TradingView's BBW, x100),
// close as source, tolerance logic. The current (open) bar still repaints.
indicator("BBW + Squeeze", precision = 2)
 
len  = input.int(20, "BB-Länge", minval = 2, display = display.none)
mult = input.float(2.0, "StdAbw", minval = 0.1, display = display.none,
     tooltip = "Ändert nur die Höhe der Linien, nicht die Squeeze-Erkennung.")
days = input.int(182, "Vergleichszeitraum (Kalendertage)", minval = 30, display = display.none,
     tooltip = "Bollinger: Tief der letzten ~6 Monate. Gerechnet in Kalendertagen, unabhängig von Zeiteinheit und Handelszeiten. 182 Tage ~ 125 Handelstage bei Aktien.")
tol  = input.float(5.0, "Toleranz über Tief (%)", minval = 0, maxval = 20, display = display.none,
     tooltip = "Relativ zum Tief, nicht in Prozentpunkten. 0 = nur exakt neues Tief (Bollinger pur).")
colBbw = input.color(#DBDBDB,   "Farbe BandWidth")
colLow = input.color(#F23645,   "Farbe Tief")
colBg  = input.color(#E91E6347, "Farbe Squeeze-Hintergrund")  // #E91E63 at ~72 % transparency
 
MS_DAY   = 86400000
MIN_BARS = 50  // weekly (~26) and monthly (~6) windows are too coarse for an extreme
 
windowMs  = days * MS_DAY
tooCoarse = days * 86400.0 / timeframe.in_seconds() < MIN_BARS
 
basis = ta.sma(close, len)
dev   = mult * ta.stdev(close, len)
bbw   = (2 * dev) / basis * 100
 
// Monotonic deque: values ascend from front to back, front = minimum of the window.
var array<float> dqVal  = array.new<float>()
var array<int>   dqTime = array.new<int>()
var int tStart = na  // open time of the first valid BBW value (warm-up anchor)
 
if not na(bbw)
    if na(tStart)
        tStart := time
    // Values >= the new one can never become the window minimum again.
    while dqVal.size() > 0 and dqVal.last() >= bbw
        dqVal.pop()
        dqTime.pop()
    dqVal.push(bbw)
    dqTime.push(time)
 
// Expire entries older than the calendar window.
cutoff = time - windowMs
while dqTime.size() > 0 and dqTime.first() < cutoff
    dqVal.shift()
    dqTime.shift()
 
float lowLine = dqVal.size() > 0 ? dqVal.first() : na
 
ready   = not tooCoarse and not na(tStart) and time - tStart >= windowMs
squeeze = ready and bbw <= lowLine * (1 + tol / 100)
 
plot(bbw, "BandWidth", color = colBbw)
plot(ready ? lowLine : na, "Tief", color = colLow, style = plot.style_stepline)
bgcolor(squeeze ? colBg : na)

← 返回列表