本页源码来自 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/
// © SchizoQuant
//@version=6
indicator("Adaptive SuperSmoother", overlay=true)
// USER INPUTS
string adaptTip = "Adapts the moving-average period to price-change volatility. Higher volatility shortens the effective period for faster response while lower volatility lengthens it for smoother tracking."
string filterTip = "Applies an Ehlers SuperSmoother to the adaptive moving average. The final trend dynamically blends the responsive adaptive line with this smoother version."
string erTip = "Measures directional efficiency. Clean directional movement gives more weight to the faster adaptive MA while noisy movement gives more weight to the SuperSmoother."
string signalTip = "Uses the trend slope and a small adaptive noise threshold to prevent insignificant movements from repeatedly changing the internal trend regime."
string signalsTip = "Shows or hides LONG and SHORT markers when the internal trend regime changes. Disabling this option only affects the display and does not change signal calculations."
string barsTip = "Shows or hides price candles colored according to the active bullish or bearish trend regime. Disabling this option only affects the display and does not change calculations or signals."
string colorTip = "Bullish Color represents the active bullish trend regime while Bearish Color represents the active bearish trend regime."
float src = input.source(title="Source", defval=close, group="Adaptive MA Settings", tooltip="Choose the price source used throughout the indicator.")
int maMin = input.int(title="Minimum MA Period", defval=14, minval=1, group="Adaptive MA Settings", inline="A1", tooltip=adaptTip)
int maMax = input.int(title="Maximum MA Period", defval=50, minval=2, group="Adaptive MA Settings", inline="A1", tooltip=adaptTip)
int volLen = input.int(title="Volatility Period", defval=9, minval=2, group="Adaptive MA Settings", tooltip=adaptTip)
int smoothLength = input.int(title="SuperSmoother Length", defval=3, minval=3, group="Filter Settings", tooltip=filterTip)
int erLength = input.int(title="Efficiency Length", defval=10, minval=2, group="Efficiency Settings", tooltip=erTip)
float fastBias = input.float(title="Minimum Fast Weight", defval=0.30, minval=0.0, maxval=1.0, step=0.05, group="Efficiency Settings", tooltip="Sets the minimum contribution of the responsive adaptive MA during noisy conditions. Increase it to keep more of the faster adaptive response or decrease it to favor the SuperSmoother more strongly.")
int noiseLength = input.int(title="Noise Length", defval=20, minval=2, group="Signal Settings", inline="S1", tooltip=signalTip)
float noiseMult = input.float(title="Noise Multiplier", defval=0.60, minval=0.0, step=0.05, group="Signal Settings", inline="S1", tooltip=signalTip)
bool showSignals = input.bool(title="Show Signals", defval=true, group="Visualization", tooltip=signalsTip)
bool colorBars = input.bool(title="Color Bars", defval=true, group="Visualization", tooltip=barsTip)
color longColor = input.color(title="Bullish Color", defval=color.rgb(57, 255, 20), group="Color Settings", inline="COL1", tooltip=colorTip)
color shortColor = input.color(title="Bearish Color", defval=color.rgb(138, 43, 226), group="Color Settings", inline="COL1", tooltip=colorTip)
// CALCULATIONS
float deltaP = src - src[1]
float s = ta.stdev(deltaP, volLen)
float sMean = ta.sma(s, volLen)
float sMin = sMean * 0.25
float sMax = sMean * 1.75
float volatilityRange = sMax - sMin
float volatilityPosition = volatilityRange > 0.0 ? math.max(0.0, math.min(1.0, (s - sMin) / volatilityRange)) : 0.0
float adaptiveLength = s <= sMin ? maMax : s >= sMax ? maMin : maMax - (maMax - maMin) * volatilityPosition
float alpha = 2.0 / (adaptiveLength + 1.0)
var float adaptiveMA = na
adaptiveMA := na(adaptiveMA[1]) ? src : adaptiveMA[1] + alpha * (src - adaptiveMA[1])
float a1 = math.exp(-1.414 * math.pi / smoothLength)
float b1 = 2.0 * a1 * math.cos(1.414 * math.pi / smoothLength)
float c2 = b1
float c3 = -a1 * a1
float c1 = 1.0 - c2 - c3
var float smoothMA = na
smoothMA := na(smoothMA[1]) or na(smoothMA[2]) ? adaptiveMA : c1 * (adaptiveMA + nz(adaptiveMA[1], adaptiveMA)) * 0.5 + c2 * smoothMA[1] + c3 * smoothMA[2]
float direction = math.abs(src - nz(src[erLength], src))
float noise = 0.0
for i = 0 to erLength - 1
noise += math.abs(nz(src[i], src) - nz(src[i + 1], src))
float efficiency = noise > 0.0 ? direction / noise : 0.0
efficiency := math.max(0.0, math.min(1.0, efficiency))
float fastWeight = fastBias + (1.0 - fastBias) * efficiency
float slowWeight = 1.0 - fastWeight
float trend = adaptiveMA * fastWeight + smoothMA * slowWeight
float slope = trend - trend[1]
float slopeNoise = ta.rma(math.abs(slope), noiseLength)
float threshold = slopeNoise * noiseMult
// SIGNALS
bool long = slope > threshold
bool short = slope < -threshold
var int SQ = 0
if long
SQ := 1
else if short
SQ := -1
bool longSignal = SQ == 1 and SQ[1] != 1
bool shortSignal = SQ == -1 and SQ[1] != -1
// COLORS
color col = SQ == 1 ? longColor : shortColor
color colors = colorBars ? col : na
// PLOTS
plot(trend, title="Adaptive SuperSmoother Trend", color=col, linewidth=2)
plot(adaptiveMA, title="Adaptive MA", display=display.data_window)
plot(smoothMA, title="SuperSmoothed MA", display=display.data_window)
plot(efficiency, title="Efficiency Ratio", display=display.data_window)
plot(fastWeight, title="Fast Filter Weight", display=display.data_window)
plotshape(showSignals and longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, size=size.small, color=longColor, text="LONG", textcolor=color.white)
plotshape(showSignals and shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, size=size.small, color=shortColor, text="SHORT", textcolor=color.white)
barcolor(colors, title = "Plot Bars")
plotcandle(open, high, low, close, "Plot Bars", colors, colors, bordercolor = colors, force_overlay = true)
// ALERTS
alertcondition(longSignal, title="Long Signal", message="Adaptive SuperSmoother Trend Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Adaptive SuperSmoother Trend Short Signal on {{ticker}}")