HD Volatility Trail

HDAlgos · study · 177 行 · 点赞 817 · TradingView 原页

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

Pine Script

//@version=6
indicator("HD Volatility Trail", shorttitle="HD-VTrail", overlay=true, max_labels_count=500)
 
//====================
// Inputs
//====================
grpCore   = "Core Settings"
grpDyn    = "Vol / Trend Adaption"
grpFlip   = "Flip Logic"
grpLabel  = "Label Settings"
grpColor  = "Colours"
 
emaLen      = input.int(8,   "Basis EMA Length", minval=2, group=grpCore)
atrLen      = input.int(14,  "ATR Length",       minval=1, group=grpCore)
baseMult    = input.float(2, "Base ATR Multiplier", minval=0.1, step=0.1, group=grpCore)
 
// Volatility & trend adaption
volLookback   = input.int(20,  "Volatility Lookback", minval=2, group=grpDyn)
volPower      = input.float(1.0, "Vol Stretch Sensitivity", minval=0.1, step=0.05, group=grpDyn)
trendLookback = input.int(25, "Trend Memory Length", minval=2, group=grpDyn)
trendImpact   = input.float(0.4, "Trend Impact (0..1)", minval=0.0, maxval=1.0, step=0.05, group=grpDyn)
 
// Bounds & confirmation
multMin     = input.float(1.0, "Min Effective Mult", minval=0.1, step=0.1, group=grpFlip)
multMax     = input.float(4.0, "Max Effective Mult", minval=0.5, step=0.1, group=grpFlip)
confirmBars = input.int(1,   "Bars Needed To Flip", minval=1, group=grpFlip)
 
// Label / visuals
showLabels  = input.bool(true, "Show Flip Labels", group=grpLabel)
lblOffsetK  = input.float(0.7, "Label Offset (x ATR)", minval=0.1, step=0.1, group=grpLabel)
lblSizeOpt  = input.string("Normal", "Label Size", options=["Tiny","Small","Normal","Large","Huge"], group=grpLabel)
 
bullCol     = input.color(color.rgb(0, 190, 160), "Bull Colour", group=grpColor)
bearCol     = input.color(color.rgb(210, 40, 140), "Bear Colour", group=grpColor)
flatCol     = input.color(color.new(color.gray, 75), "Flat Colour", group=grpColor)
 
//====================
// Helpers
//====================
toSize(str) =>
    sz = size.normal
    if str == "Tiny"
        sz := size.tiny
    else if str == "Small"
        sz := size.small
    else if str == "Large"
        sz := size.large
    else if str == "Huge"
        sz := size.huge
    sz
 
//====================
// Core series
//====================
basis   = ta.ema(close, emaLen)
atr     = ta.atr(atrLen)
atrAvg  = ta.sma(atr, volLookback)
 
// Volatility stretch: >1 = expanded, <1 = compressed
volStretchRaw = atrAvg == 0.0 ? 1.0 : atr / atrAvg
volStretch    = math.pow(volStretchRaw, volPower)
 
// Direction memory via EMA of sign of slope
slope        = basis - basis[1]
dirStep      = slope >= 0.0 ? 1.0 : -1.0
trendMemory  = ta.ema(dirStep, trendLookback)          // approx -1..+1
trendBoost   = 1.0 + trendImpact * math.abs(trendMemory)
 
// Effective multiplier (clamped)
multRaw   = baseMult * volStretch * trendBoost
multUpper = math.min(multRaw, multMax)
multFinal = math.max(multUpper, multMin)
 
// Bands around basis
bandTop   = basis + multFinal * atr
bandBot   = basis - multFinal * atr
 
//====================
// Trailing logic (supertrend-style)
//====================
var float trailLong  = na   // used in bull mode (below price)
var float trailShort = na   // used in bear mode (above price)
var int   regime     = 0    // 1 = bull, -1 = bear, 0 = neutral
 
if na(trailLong)
    trailLong := bandBot
if na(trailShort)
    trailShort := bandTop
 
// Counters for confirmation
var int bullCount = 0
var int bearCount = 0
 
aboveShort = close > trailShort
belowLong  = close < trailLong
 
if aboveShort
    bullCount += 1
else
    bullCount := 0
 
if belowLong
    bearCount += 1
else
    bearCount := 0
 
// Update trails based on regime
if regime == 1
    trailLong  := math.max(bandBot, nz(trailLong[1], bandBot))
    trailShort := bandTop
else if regime == -1
    trailShort := math.min(bandTop, nz(trailShort[1], bandTop))
    trailLong  := bandBot
else
    trailLong  := bandBot
    trailShort := bandTop
 
// Flip regime with confirmation
if regime == 0
    if bullCount >= confirmBars
        regime := 1
    else if bearCount >= confirmBars
        regime := -1
else if regime == 1 and bearCount >= confirmBars
    regime := -1
else if regime == -1 and bullCount >= confirmBars
    regime := 1
 
//====================
// Visuals
//====================
isBull  = regime == 1
isBear  = regime == -1
 
pShort  = plot(trailShort, "Short Trail", color=isBear ? bearCol : flatCol, style=plot.style_linebr, linewidth=2)
pLong   = plot(trailLong,  "Long Trail",  color=isBull ? bullCol : flatCol, style=plot.style_linebr, linewidth=2)
 
// Hidden price plot for fills
pPrice  = plot(close, title="Price (hidden)", color=color.new(color.gray, 100), display=display.none)
 
fill(pShort, pPrice, color=isBear ? color.new(bearCol, 80) : color.new(color.gray, 100), title="Bear Zone")
fill(pLong,  pPrice, color=isBull ? color.new(bullCol, 80) : color.new(color.gray, 100), title="Bull Zone")
 
barcolor(isBull ? bullCol : isBear ? bearCol : flatCol)
 
//====================
// Flip markers & alerts
//====================
bullFlip = ta.change(regime) == 2    // -1 -> 1
bearFlip = ta.change(regime) == -2   //  1 -> -1
 
lblSize = toSize(lblSizeOpt)
 
if showLabels and bullFlip
    yLong = trailLong - lblOffsetK * atr
    label.new(
         bar_index, yLong,
         text="Long",
         style=label.style_label_up,
         size=lblSize,
         color=color.new(bullCol, 0),
         textcolor=color.black
     )
 
if showLabels and bearFlip
    yShort = trailShort + lblOffsetK * atr
    label.new(
         bar_index, yShort,
         text="Short",
         style=label.style_label_down,
         size=lblSize,
         color=color.new(bearCol, 0),
         textcolor=color.white
     )
 
alertcondition(bullFlip, "HD-VTrail Bull Flip", "HD-VTrail: Bullish regime flip.")
alertcondition(bearFlip, "HD-VTrail Bear Flip", "HD-VTrail: Bearish regime flip.")

← 返回列表