本页源码来自 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 Decycler Supertrend", overlay=true)
// USER INPUTS
string cutoffTip = "Minimum Cutoff sets the fastest Decycler response used during cleaner directional movement, while Maximum Cutoff sets the slowest response used during noisier conditions. Lower the minimum for faster reactions or increase the maximum for stronger smoothing."
string efficiencyTip = "Sets the lookback used to measure directional efficiency by comparing net price movement with the total distance travelled. Higher efficiency moves the adaptive Decycler toward the faster cutoff, while lower efficiency moves it toward the slower cutoff."
string rmsTip = "Sets the lookback used to measure the root-mean-square energy of price displacement from the adaptive Decycler. Increase it for steadier bands or decrease it for faster adaptation to changes in residual movement."
string bandTip = "Upper Multiplier controls the RMS distance above the adaptive Decycler, while Lower Multiplier controls the distance below it. Increase either multiplier to widen that side of the Supertrend structure and require a larger move before a reversal."
string colorTip = "Bullish Color represents bullish Supertrend regimes, LONG signals, and bullish visual elements, while Bearish Color represents bearish Supertrend regimes, SHORT signals, and bearish visual elements."
float src = input.source(title="Source", defval=close, group="Adaptive Decycler", tooltip="Choose the price source used by the adaptive Decycler, efficiency calculation, residual measurement, and Supertrend regime logic.")
int minimumCutoff = input.int(title="Minimum Cutoff", defval=15, minval=6, group="Adaptive Decycler", inline="D1", tooltip=cutoffTip)
int maximumCutoff = input.int(title="Maximum Cutoff", defval=50, minval=7, group="Adaptive Decycler", inline="D1", tooltip=cutoffTip)
int efficiencyLength = input.int(title="Efficiency Length", defval=10, minval=2, group="Adaptive Decycler", tooltip=efficiencyTip)
int rmsLength = input.int(title="Residual RMS Length", defval=20, minval=2, group="Supertrend Bands", tooltip=rmsTip)
float upperMultiplier = input.float(title="Upper Multiplier", defval=2.00, minval=0.10, step=0.10, group="Supertrend Bands", inline="B1", tooltip=bandTip)
float lowerMultiplier = input.float(title="Lower Multiplier", defval=2.60, minval=0.10, step=0.10, group="Supertrend Bands", inline="B1", tooltip=bandTip)
bool showDecycler = input.bool(title="Show Decycler", defval=true, group="Visualization", tooltip="Show or hide the adaptive Decycler baseline. Turning this off only changes the chart display and does not affect the Supertrend calculations or signals.")
bool showEnvelope = input.bool(title="Show RMS Envelope", defval=false, group="Visualization", tooltip="Show or hide the upper and lower residual RMS bands. Turning this off only changes the chart display and does not affect regime calculations.")
bool showFill = input.bool(title="Show Envelope Fill", defval=true, group="Visualization", tooltip="Show or hide the shaded region between the RMS bands when the envelope is visible. This is visual only and does not affect the indicator calculations.")
bool showSignals = input.bool(title="Show Signals", defval=true, group="Visualization", tooltip="Show or hide LONG and SHORT markers whenever the active Supertrend regime changes. Turning this off does not change the underlying regime calculation.")
bool colorBars = input.bool(title="Color Bars", defval=true, group="Visualization", tooltip="Color price candles according to the active Supertrend regime. Turning this off only removes candle coloring and does not affect signals.")
color longColor = input.color(title="Bullish Color", defval=color.rgb(57, 255, 20), group="Color Settings", inline="C1", tooltip=colorTip)
color shortColor = input.color(title="Bearish Color", defval=color.rgb(138, 43, 226), group="Color Settings", inline="C1", tooltip=colorTip)
// CALCULATIONS
int fastLimit = math.min(minimumCutoff, maximumCutoff)
int slowLimit = math.max(minimumCutoff, maximumCutoff)
float direction = math.abs(src - nz(src[efficiencyLength], src))
float noise = 0.0
for i = 0 to efficiencyLength - 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 adaptiveCutoff = float(slowLimit) - efficiency * float(slowLimit - fastLimit)
float angle = 2.0 * math.pi / adaptiveCutoff
float cosineValue = math.cos(angle)
float alpha = math.abs(cosineValue) > 0.000001 ? (cosineValue + math.sin(angle) - 1.0) / cosineValue : 1.0
alpha := math.max(0.0, math.min(1.0, alpha))
var float decycler = na
decycler := na(decycler[1]) ? src : alpha * 0.5 * (src + nz(src[1], src)) + (1.0 - alpha) * decycler[1]
float residual = src - decycler
float residualEnergy = ta.sma(residual * residual, rmsLength)
float residualRMS = math.sqrt(math.max(nz(residualEnergy, 0.0), 0.0))
residualRMS := math.max(residualRMS, syminfo.mintick)
float upperEnvelope = decycler + residualRMS * upperMultiplier
float lowerEnvelope = decycler - residualRMS * lowerMultiplier
//SIGNALS
var int SQ = src >= decycler ? 1 : -1
var float trendTrail = na
int previousSQ = nz(SQ[1], SQ)
float previousTrail = nz(trendTrail[1], previousSQ == 1 ? lowerEnvelope : upperEnvelope)
bool long = previousSQ == -1 and src > previousTrail
bool short = previousSQ == 1 and src < previousTrail
if long
SQ := 1
else if short
SQ := -1
else
SQ := previousSQ
if SQ == 1
trendTrail := long ? lowerEnvelope : math.max(lowerEnvelope, previousTrail)
else
trendTrail := short ? upperEnvelope : math.min(upperEnvelope, previousTrail)
bool longSignal = SQ == 1 and nz(SQ[1], SQ) != 1
bool shortSignal = SQ == -1 and nz(SQ[1], SQ) != -1
// COLORS
color col = SQ == 1 ? longColor : shortColor
// PLOTS
plot(SQ == 1 ? trendTrail : na, title="Bullish Supertrend", color=longColor, linewidth=2, style=plot.style_linebr)
plot(SQ == -1 ? trendTrail : na, title="Bearish Supertrend", color=shortColor, linewidth=2, style=plot.style_linebr)
plot(showDecycler ? decycler : na, title="Adaptive Decycler", color=color.new(col, 35), linewidth=2)
upperPlot = plot(showEnvelope ? upperEnvelope : na, title="Upper RMS Band", color=color.new(shortColor, 70), linewidth=1)
lowerPlot = plot(showEnvelope ? lowerEnvelope : na, title="Lower RMS Band", color=color.new(longColor, 70), linewidth=1)
fill(upperPlot, lowerPlot, title="RMS Band Fill", color=showFill and showEnvelope ? color.new(col, 93) : na)
plot(efficiency, title="Efficiency", display=display.data_window)
plot(adaptiveCutoff, title="Adaptive Cutoff", display=display.data_window)
plot(residual, title="Decycler Residual", display=display.data_window)
plot(residualRMS, title="Residual RMS", 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(colorBars ? col : na)
// ALERTS
alertcondition(longSignal, title="Long Signal", message="Adaptive Decycler Supertrend Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Adaptive Decycler Supertrend Short Signal on {{ticker}}")