Volatility Trail [BOSWaves]

BOSWaves · study · 182 行 · 点赞 1,832 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BOSWaves
 
//@version=6
indicator("Volatility Trail [BOSWaves]", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ┌────────────────────────────── BOSWaves ─ Inputs ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string gCore  = "Trail Engine"
const string gCloud = "Cloud & Candles"
const string gSig   = "Signals"
 
int    hullLen    = input.int(72, "Hull Length", minval = 5, group = gCore, tooltip = "Hull MA smoothing length. Lower = faster response.")
int    atrLen     = input.int(9, "ATR Length", minval = 1, group = gCore, tooltip = "ATR lookback for trail distance.")
float  atrFactor  = input.float(1.7, "ATR Factor", step = 0.1, minval = 0.5, group = gCore, tooltip = "Multiplier for trail distance. Higher = more room before flip.")
string src        = input.string("Close", "Source", options = ["Close", "HL2", "HLC3", "OHLC4"], group = gCore)
int    trailSmth  = input.int(4, "Trail Smoothing", minval = 1, maxval = 10, group = gCore, tooltip = "EMA smoothing on the displayed trail. Signals fire from the raw trail.")
 
bool   showCloud  = input.bool(true, "Show Gradient Cloud", group = gCloud, tooltip = "Layered gradient that radiates from the trail toward price.")
bool   showCandle = input.bool(true, "Color Candles", group = gCloud)
string candleMode = input.string("Distance", "Candle Color Mode", options = ["Distance", "Acceleration", "Both"], group = gCloud, tooltip = "Distance: brightness by how far price is from the trail.\nAcceleration: brightness by how fast the trail is advancing.\nBoth: combines distance and acceleration.")
int    accelLen   = input.int(2, "Acceleration Lookback",group = gCloud)
 
bool   showSig    = input.bool(true, "Show Buy/Sell", group = gSig)
bool   showRetest = input.bool(true, "Retest Diamonds", group = gSig, tooltip = "Marks when price dips back toward the trail after trending away.")
int    rtCooldown = input.int(10, "Retest Cooldown", minval = 1, maxval = 50, group = gSig)
int    sigBuffer  = input.int(5, "Signal Buffer", minval = 1, maxval = 20, group = gSig, tooltip = "Bars after a flip before retests can fire.")
 
color  bullCol    = input.color(#00ff00, "Bullish", inline = "c", group = gCloud)
color  bearCol    = input.color(#ff0000, "Bearish", inline = "c", group = gCloud)
 
// ┌────────────────────────────── BOSWaves ─ Source ──────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float srcVal = switch src
    "HL2"   => hl2
    "HLC3"  => hlc3
    "OHLC4" => ohlc4
    => close
 
// ┌────────────────────────────── BOSWaves ─ Hull + ATR Trail ───────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float hull = ta.hma(srcVal, hullLen)
float atr  = ta.atr(atrLen)
 
float upperBand = hull + atr * atrFactor
float lowerBand = hull - atr * atrFactor
 
var float trail = na
var bool  trend = true
 
float prevTrail = nz(trail)
 
if na(trail)
    trail := close > hull ? lowerBand : upperBand
 
if trend
    trail := math.max(lowerBand, prevTrail)
    if close < trail
        trend := false
        trail := upperBand
else
    trail := math.min(upperBand, prevTrail)
    if close > trail
        trend := true
        trail := lowerBand
 
float trailVis = ta.ema(trail, trailSmth)
 
// ┌────────────────────────────── BOSWaves ─ Flip Detection ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
bool flipUp  = trend and not trend[1]
bool flipDn  = not trend and trend[1]
bool anyFlip = flipUp or flipDn
 
var int   flipBar    = 0
var int   lastBullRT = na
var int   lastBearRT = na
 
if anyFlip
    flipBar := bar_index
 
color trendCol = trend ? bullCol : bearCol
 
// ┌────────────────────────────── BOSWaves ─ Gradient Layers ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float gap   = close - trailVis
float band1 = trailVis + gap * 0.2
float band2 = trailVis + gap * 0.4
float band3 = trailVis + gap * 0.65
float band4 = trailVis + gap * 0.85
 
float b1s = ta.ema(band1, trailSmth)
float b2s = ta.ema(band2, trailSmth)
float b3s = ta.ema(band3, trailSmth)
float b4s = ta.ema(band4, trailSmth)
 
pTrail = plot(trailVis, "Trail", trendCol, linewidth = 2)
 
pB1 = plot(showCloud ? b1s : na, "Band 1", color = color.new(trendCol, 100), display = display.all, editable = false)
pB2 = plot(showCloud ? b2s : na, "Band 2", color = color.new(trendCol, 100), display = display.all, editable = false)
pB3 = plot(showCloud ? b3s : na, "Band 3", color = color.new(trendCol, 100), display = display.all, editable = false)
pB4 = plot(showCloud ? b4s : na, "Band 4", color = color.new(trendCol, 100), display = display.all, editable = false)
 
fill(pTrail, pB1, trailVis, b1s, trend ? color.new(bullCol, 20) : color.new(bearCol, 75), trend ? color.new(bullCol, 75) : color.new(bearCol, 20))
 
fill(pB1, pB2, b1s, b2s, trend ? color.new(bullCol, 50) : color.new(bearCol, 85), trend ? color.new(bullCol, 85) : color.new(bearCol, 50))
 
fill(pB2, pB3, b2s, b3s, trend ? color.new(bullCol, 75) : color.new(bearCol, 92), trend ? color.new(bullCol, 92) : color.new(bearCol, 75))
 
fill(pB3, pB4, b3s, b4s, trend ? color.new(bullCol, 88) : color.new(bearCol, 95), trend ? color.new(bullCol, 95) : color.new(bearCol, 88))
 
// ┌────────────────────────────── BOSWaves ─ Candle Scoring ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float distToTrail = math.abs(close - trailVis)
float distScore   = nz(atr) > 0 ? math.min(distToTrail / (atr * 3), 1.0) : 0.0
 
float trailSpeed  = math.abs(trailVis - nz(trailVis[accelLen])) / accelLen
float speedNorm   = nz(atr) > 0 ? math.min(trailSpeed / (atr * 0.2), 1.0) : 0.0
 
float rawScore = switch candleMode
    "Distance"     => distScore
    "Acceleration" => speedNorm
    "Both"         => math.max(distScore, speedNorm)
    => distScore
 
// Crush low scores hard — only genuinely strong bars break through
float exScore = math.pow(rawScore, 2.5)
 
// ┌────────────────────────────── BOSWaves ─ Candle Rendering ───────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
color dimCol  = #2a2a2e
 
color bodyCol = showCandle ? color.from_gradient(exScore, 0, 1, dimCol, trendCol) : na
color wickCol = showCandle ? color.from_gradient(exScore, 0, 1, color.new(dimCol, 30), trendCol) : na
color bordCol = showCandle ? color.from_gradient(exScore, 0, 1, color.new(dimCol, 20), trendCol) : na
 
plotcandle(open, high, low, close, "Candles",
     wickcolor   = wickCol,
     color       = bodyCol,
     bordercolor = bordCol,
     display     = showCandle ? display.all : display.none)
// ┌────────────────────────────── BOSWaves ─ Retest Diamonds ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
bool farEnough = bar_index - flipBar >= sigBuffer
 
float retestZone = atr * atrFactor * 0.3
bool  bullRT     = showRetest and trend and (low - trailVis) < retestZone and (low - trailVis) > 0 and farEnough
bool  bearRT     = showRetest and not trend and (trailVis - high) < retestZone and (trailVis - high) > 0 and farEnough
 
bool bullRTok = bullRT and (na(lastBullRT) or bar_index - lastBullRT >= rtCooldown)
bool bearRTok = bearRT and (na(lastBearRT) or bar_index - lastBearRT >= rtCooldown)
 
if bullRTok
    lastBullRT := bar_index
if bearRTok
    lastBearRT := bar_index
 
plotchar(bullRTok, "Bull Retest", "◆", location.belowbar, bullCol, size = size.tiny)
plotchar(bearRTok, "Bear Retest", "◆", location.abovebar, bearCol, size = size.tiny)
 
// ┌────────────────────────────── BOSWaves ─ Buy / Sell Signals ─────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
if showSig and flipUp
    label.new(bar_index, low - atr * 1.2, "▲",
         style = label.style_label_up,
         size = size.normal,
         color = bullCol,
         textcolor = color.white)
 
if showSig and flipDn
    label.new(bar_index, high + atr * 1.2, "▼",
         style = label.style_label_down,
         size = size.normal,
         color = bearCol,
         textcolor = color.white)
 
// ┌────────────────────────────── BOSWaves ─ Alerts ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
alertcondition(flipUp, "Trail Long", "Volatility Trail flipped bullish on {{ticker}}")
alertcondition(flipDn, "Trail Short", "Volatility Trail flipped bearish on {{ticker}}")
alertcondition(bullRTok, "Bull Retest", "Bullish trail retest on {{ticker}}")
alertcondition(bearRTok, "Bear Retest", "Bearish trail retest on {{ticker}}")

← 返回列表