本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NordicAlphaLab
//@version=6
indicator("Halo SuperTrend | NAL", shorttitle = "Halo SuperTrend", overlay = true, precision = 2)
// ███╗ ██╗ █████╗ ██╗
// ████╗ ██║██╔══██╗██║
// ██╔██╗ ██║███████║██║
// ██║╚██╗██║██╔══██║██║
// ██║ ╚████║██║██║ ██║█████
// ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝
// ══════════════════════════════════════ ◈ Visuals ◈ ══════════════════════════════════════
string col_mode = input.string("Standard", title = "Color Mode", group = "Visuals", options = ["Standard", "Nordic", "Simple"])
[col_up, col_dn, col_nu] = switch col_mode
"Standard" => [color.rgb(0, 255, 200), color.rgb(32, 94, 144), color.gray]
"Nordic" => [color.rgb(46, 161, 255), color.rgb(150, 154, 169), color.gray]
"Simple" => [color.lime, color.red, color.gray]
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
string groupBaseline = "Baseline & ATR"
string groupGARCH = "GARCH Regime Multiplier"
float src = input.source(close, "Source", group = groupBaseline)
int baseline_len = input.int(50, "ZLEMA Baseline Length", minval = 1, group = groupBaseline)
int ATR_Len = input.int(24, "ATR Length", minval = 1, group = groupBaseline)
float ATR_Mult = input.float(1.8, "ATR Multiple", minval = 0.1, step = 0.1, group = groupBaseline)
bool GARCH_UseAdaptiveCoefficients = input.bool(true, "Use Adaptive GARCH Coefficients?", group = groupGARCH)
int GARCH_LongRunLen = input.int(80, "Long-Run Variance Length", minval = 2, group = groupGARCH)
int GARCH_RegimeLen = input.int(60, "Regime Normalization Length", minval = 2, group = groupGARCH)
int GARCH_MultSmooth = input.int(20, "Multiplier Smoothing", minval = 1, group = groupGARCH)
float GARCH_MinMult = input.float(0.50, "Min Multiplier", minval = 0.1, step = 0.05, group = groupGARCH, inline = "MUL")
float GARCH_MaxMult = input.float(0.90, "Max Multiplier", minval = 0.1, step = 0.05, group = groupGARCH, inline = "MUL")
if GARCH_MinMult > GARCH_MaxMult
runtime.error("Minimum GARCH Multiplier cannot exceed Maximum GARCH Multiplier.")
// ══════════════════════════════════════ ◈ Functions ◈ ══════════════════════════════════════
f_zlema(series float source, simple int len) =>
int lag = int(math.floor((len - 1) / 2))
float zeroLagSource = source + (source - nz(source[lag], source))
ta.ema(zeroLagSource, len)
f_clamp(float value, float minValue, float maxValue) =>
math.max(minValue, math.min(value, maxValue))
// ══════════════════════════════════════ ◈ ZLEMA Baseline & ATR ◈ ══════════════════════════════════════
float baseline = f_zlema(src, baseline_len)
float ATR_Value = ta.atr(ATR_Len)
// ══════════════════════════════════════ ◈ Adaptive Recursive GARCH Regime Multiplier ◈ ══════════════════════════════════════
float GARCH_LogReturn = close > 0.0 and close[1] > 0.0 ? math.log(close / close[1]) : 0.0
float GARCH_ShockVariance = math.pow(GARCH_LogReturn, 2.0)
float GARCH_LongRunVariance = nz(ta.ema(GARCH_ShockVariance, GARCH_LongRunLen), GARCH_ShockVariance)
var float GARCH_ConditionalVariance = na
float GARCH_FitTargetVariance = nz(GARCH_ShockVariance[1], GARCH_ShockVariance)
float GARCH_FitShockVariance = nz(GARCH_ShockVariance[2], GARCH_FitTargetVariance)
float GARCH_FitLongRunVariance = nz(GARCH_LongRunVariance[2], GARCH_LongRunVariance)
float GARCH_FitConditionalVariance = nz(GARCH_ConditionalVariance[2], GARCH_FitLongRunVariance)
int GARCH_BetaIndex = int(math.min(math.max(math.round(0.85 * 100.0), 1.0), 99.0))
int GARCH_GammaIndex = int(math.min(math.max(math.round(0.05 * 100.0), 1.0), 100.0 - GARCH_BetaIndex))
bool GARCH_CanOptimize = bar_index > 10
if GARCH_UseAdaptiveCoefficients and GARCH_CanOptimize
float GARCH_BetaBestSSE = na
for GARCH_betaCandidate = 1 to 99 by 1
float GARCH_betaCandidateWeight = GARCH_betaCandidate / 100.0
float GARCH_betaEstimate = GARCH_betaCandidateWeight * GARCH_FitConditionalVariance + (1.0 - GARCH_betaCandidateWeight) * GARCH_FitShockVariance
float GARCH_betaCandidateSSE = math.sum(math.pow(GARCH_betaEstimate - GARCH_FitTargetVariance, 2.0), 5)
if not na(GARCH_betaCandidateSSE) and (na(GARCH_BetaBestSSE) or GARCH_betaCandidateSSE < GARCH_BetaBestSSE)
GARCH_BetaBestSSE := GARCH_betaCandidateSSE
GARCH_BetaIndex := GARCH_betaCandidate
float GARCH_GammaBestSSE = na
for GARCH_gammaCandidate = 1 to 100 - GARCH_BetaIndex by 1
float GARCH_betaWeight = GARCH_BetaIndex / 100.0
float GARCH_gammaWeight = GARCH_gammaCandidate / 100.0
float GARCH_alphaWeight = 1.0 - GARCH_betaWeight - GARCH_gammaWeight
float GARCH_gammaEstimate = GARCH_gammaWeight * GARCH_FitLongRunVariance + GARCH_alphaWeight * GARCH_FitShockVariance + GARCH_betaWeight * GARCH_FitConditionalVariance
float GARCH_gammaCandidateSSE = math.sum(math.pow(GARCH_gammaEstimate - GARCH_FitTargetVariance, 2.0), 5)
if not na(GARCH_gammaCandidateSSE) and (na(GARCH_GammaBestSSE) or GARCH_gammaCandidateSSE < GARCH_GammaBestSSE)
GARCH_GammaBestSSE := GARCH_gammaCandidateSSE
GARCH_GammaIndex := GARCH_gammaCandidate
float GARCH_ManualCoefficientSum = math.max(0.1 + 0.85 + 0.05, 0.000001)
float GARCH_ManualAlpha = 0.1 / GARCH_ManualCoefficientSum
float GARCH_ManualBeta = 0.85 / GARCH_ManualCoefficientSum
float GARCH_ManualGamma = 0.05 / GARCH_ManualCoefficientSum
float GARCH_Beta = GARCH_UseAdaptiveCoefficients ? GARCH_BetaIndex / 100.0 : GARCH_ManualBeta
float GARCH_Gamma = GARCH_UseAdaptiveCoefficients ? GARCH_GammaIndex / 100.0 : GARCH_ManualGamma
float GARCH_Alpha = GARCH_UseAdaptiveCoefficients ? math.max(1.0 - GARCH_Beta - GARCH_Gamma, 0.0) : GARCH_ManualAlpha
float GARCH_PreviousConditionalVariance = nz(GARCH_ConditionalVariance[1], GARCH_LongRunVariance)
GARCH_ConditionalVariance := GARCH_Gamma * GARCH_LongRunVariance + GARCH_Alpha * GARCH_ShockVariance + GARCH_Beta * GARCH_PreviousConditionalVariance
float GARCH_ProjectedVariance = f_zlema(GARCH_ConditionalVariance, 7)
float GARCH_Volatility = math.sqrt(math.max(GARCH_ProjectedVariance, 0.0))
float GARCH_RegimeBase = nz(ta.ema(GARCH_Volatility, GARCH_RegimeLen), GARCH_Volatility)
float GARCH_RegimeMultiplierRaw = GARCH_RegimeBase != 0.0 ? GARCH_Volatility / GARCH_RegimeBase : 1.0
float GARCH_RegimeMultiplierSmooth = f_zlema(GARCH_RegimeMultiplierRaw, GARCH_MultSmooth)
float GARCH_RegimeMultiplier = f_clamp(GARCH_RegimeMultiplierSmooth, GARCH_MinMult, GARCH_MaxMult)
// ══════════════════════════════════════ ◈ Halo SuperTrend ◈ ══════════════════════════════════════
// ZLEMA-centered bands use an ATR width adjusted by the GARCH regime multiplier.
float hybridBandWidth = ATR_Value * ATR_Mult * GARCH_RegimeMultiplier
float UpperBand = baseline + hybridBandWidth
float LowerBand = baseline - hybridBandWidth
float PreviousUpperBand = nz(UpperBand[1], UpperBand)
float PreviousLowerBand = nz(LowerBand[1], LowerBand)
// Raw bands become trailing SuperTrend bands.
UpperBand := UpperBand < PreviousUpperBand or close[1] > PreviousUpperBand ? UpperBand : PreviousUpperBand
LowerBand := LowerBand > PreviousLowerBand or close[1] < PreviousLowerBand ? LowerBand : PreviousLowerBand
int Direction = na
float SuperTrend = na
float PreviousSuperTrend = SuperTrend[1]
if na(ATR_Value[1]) or na(GARCH_RegimeMultiplier[1])
Direction := 1
else if PreviousSuperTrend == PreviousUpperBand
Direction := close > UpperBand ? -1 : 1
else
Direction := close < LowerBand ? 1 : -1
SuperTrend := Direction == -1 ? LowerBand : UpperBand
// ══════════════════════════════════════ ◈ NAL State ◈ ══════════════════════════════════════
bool Bullish = Direction == -1
bool Bearish = Direction == 1
int NAL = 0
NAL := Bullish ? 1 : Bearish ? -1 : nz(NAL[1], 0)
bool BullishFlip = barstate.isconfirmed and Bullish and not Bullish[1]
bool BearishFlip = barstate.isconfirmed and Bearish and not Bearish[1]
color col = Bullish ? col_up : Bearish ? col_dn : col_nu
// ══════════════════════════════════════ ◈ Gradient Geometry ◈ ══════════════════════════════════════
float BodyAnchor = math.avg(open, close)
float GradientTop = math.max(BodyAnchor, SuperTrend)
float GradientBottom = math.min(BodyAnchor, SuperTrend)
bool BandAboveBody = SuperTrend >= BodyAnchor
color BullTopColor = BandAboveBody ? color.new(col_up, 50) : color.new(col_up, 100)
color BullBottomColor = BandAboveBody ? color.new(col_up, 100) : color.new(col_up, 50)
color BearTopColor = BandAboveBody ? color.new(col_dn, 50) : color.new(col_dn, 100)
color BearBottomColor = BandAboveBody ? color.new(col_dn, 100) : color.new(col_dn, 50)
// ══════════════════════════════════════ ◈ Plots ◈ ══════════════════════════════════════
// Invisible candle-body anchor used only to terminate the gradient at price.
BodyPlot = plot(BodyAnchor, "Candle Body Anchor", color = na, display = display.none)
// Only the active SuperTrend boundary is displayed.
BullPlot = plot(Bullish ? SuperTrend : na, "Bullish Halo SuperTrend", color = col_up, linewidth = 2, style = plot.style_linebr)
BearPlot = plot(Bearish ? SuperTrend : na, "Bearish Halo SuperTrend", color = col_dn, linewidth = 2, style = plot.style_linebr)
// Smooth vertical gradient from the active SuperTrend boundary into the bars.
fill(BodyPlot, BullPlot, Bullish ? GradientTop : na, Bullish ? GradientBottom : na, BullTopColor, BullBottomColor, title = "Bullish Gradient", fillgaps = false)
fill(BodyPlot, BearPlot, Bearish ? GradientTop : na, Bearish ? GradientBottom : na, BearTopColor, BearBottomColor, title = "Bearish Gradient", fillgaps = false)
plot(Bullish ? SuperTrend : na, "Bullish SuperTrend Glow", color = color.new(col_up, 55), linewidth = 5, style = plot.style_linebr, display = display.pane)
plot(Bearish ? SuperTrend : na, "Bearish SuperTrend Glow", color = color.new(col_dn, 55), linewidth = 5, style = plot.style_linebr, display = display.pane)
plot(Bullish ? SuperTrend : na, "Bullish SuperTrend Edge", color = col_up, linewidth = 2, style = plot.style_linebr, display = display.pane)
plot(Bearish ? SuperTrend : na, "Bearish SuperTrend Edge", color = col_dn, linewidth = 2, style = plot.style_linebr, display = display.pane)
// Trend-colored candles.
plotcandle(open, high, low, close, "Bar Color", color = col, wickcolor = col, bordercolor = col, display = display.pane, force_overlay = true)
// ══════════════════════════════════════ ◈ Alerts ◈ ══════════════════════════════════════
alertcondition(BullishFlip, "Halo SuperTrend Bullish", "Halo SuperTrend changed to bullish on {{ticker}}.")
alertcondition(BearishFlip, "Halo SuperTrend Bearish", "Halo SuperTrend changed to bearish on {{ticker}}.")