Decycler High-Low Breakout [SchizoQuant]

SchizoQuant · study · 135 行 · 点赞 830 · 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/
// © SchizoQuant
 
//@version=6
indicator("Decycler High-Low Breakout", overlay=true)
 
// USER INPUTS
string periodTip       = "Sets the cutoff period of the two-pole Ehlers Decycler applied independently to high, low, and close. Higher values remove longer cyclical fluctuations."
string atrTip          = "Sets the ATR lookback used to normalize channel padding and directional movement."
string paddingTip      = "Sets the additional ATR-based distance outside the Decycler high-low channel. Higher values tolerate deeper pullbacks and reduce trend switching."
string slopeTip        = "Sets the number of candles used to measure directional movement of the Decycler close."
string slopeConfirmTip = "Sets the minimum ATR-normalized Decycler slope required for an ordinary channel breakout to reverse the active trend."
string breathingTip    = "Controls the expansion of the protective channel boundary when the Decycler moves in the direction of the established trend."
string overrideTip     = "Sets the additional ATR distance beyond a channel boundary that allows a decisive price breakout to reverse the trend without slope confirmation."
string centerTip       = "Shows or hides the Decycler calculated from closing prices. This setting does not affect calculations or signals."
string channelTip      = "Shows or hides the upper and lower Decycler channel boundaries."
string defenseTip      = "Shows or hides the active trend reversal boundary, including ATR padding and directional breathing room."
string fillTip         = "Shows or hides the shaded area between the upper and lower Decycler channel boundaries."
string bandTip         = "Controls the transparency of the upper and lower Decycler channel lines. Zero is fully opaque and 100 is invisible."
string fillTransTip    = "Controls the transparency of the shaded region between the Decycler channel boundaries. Higher values produce a lighter background."
string signalsTip      = "Shows or hides LONG and SHORT triangles on the main price chart when the confirmed trend regime changes."
string barsTip         = "Shows or hides candle coloring according to the active bullish or bearish trend regime."
string colorTip        = "Bullish Color represents bullish conditions and LONG signals, while Bearish Color represents bearish conditions and SHORT signals."
 
int   decyclerPeriod    = input.int(title="Decycler Period", defval=115, minval=6, group="Decycler Settings", tooltip=periodTip)
int   atrLength         = input.int(title="ATR Length", defval=14, minval=1, group="Channel Settings", tooltip=atrTip)
float channelPadding    = input.float(title="Channel Padding (ATR)", defval=0.1, minval=0.0, maxval=3.0, step=0.05, group="Channel Settings", tooltip=paddingTip)
int   slopeLength       = input.int(title="Decycler Slope Length", defval=3, minval=1, group="Trend Settings", tooltip=slopeTip)
float slopeConfirmation = input.float(title="Slope Confirmation", defval=0.05, minval=0.0, maxval=1.0, step=0.01, group="Trend Settings", tooltip=slopeConfirmTip)
float trendBreathing    = input.float(title="Trend Breathing Room", defval=0.50, minval=0.0, maxval=2.0, step=0.05, group="Trend Settings", tooltip=breathingTip)
float reversalOverride  = input.float(title="Strong Breakout Override (ATR)", defval=0.45, minval=0.0, maxval=3.0, step=0.05, group="Trend Settings", tooltip=overrideTip)
bool  showCenter        = input.bool(title="Show Decycler Centerline", defval=true, group="Visualization", tooltip=centerTip)
bool  showChannel       = input.bool(title="Show Decycler Channel", defval=true, group="Visualization", tooltip=channelTip)
bool  showDefense       = input.bool(title="Show Active Reversal Boundary", defval=true, group="Visualization", tooltip=defenseTip)
bool  showFill          = input.bool(title="Show Channel Fill", defval=true, group="Visualization", tooltip=fillTip)
int   bandTransparency  = input.int(title="Channel Transparency", defval=65, minval=0, maxval=100, group="Visualization", tooltip=bandTip)
int   fillTransparency  = input.int(title="Channel Fill Transparency", defval=80, minval=0, maxval=100, group="Visualization", tooltip=fillTransTip)
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
decyclerFilter(float price, int period) =>
    float angle = 0.707 * 2.0 * math.pi / period
    float alpha = (math.cos(angle) + math.sin(angle) - 1.0) / math.cos(angle)
    float decay = 1.0 - alpha
    float gain = math.pow(1.0 - alpha * 0.5, 2.0)
    var float highPass = 0.0
    highPass := bar_index >= 2 ? gain * (price - 2.0 * price[1] + price[2]) + 2.0 * decay * nz(highPass[1], 0.0) - decay * decay * nz(highPass[2], 0.0) : 0.0
    price - highPass
 
float decyclerHigh  = decyclerFilter(high, decyclerPeriod)
float decyclerLow   = decyclerFilter(low, decyclerPeriod)
float decyclerClose = decyclerFilter(close, decyclerPeriod)
 
float upperChannel = math.max(decyclerHigh, math.max(decyclerLow, decyclerClose))
float lowerChannel = math.min(decyclerHigh, math.min(decyclerLow, decyclerClose))
 
float atrValue = ta.atr(atrLength)
float safeATR  = math.max(nz(atrValue, syminfo.mintick), syminfo.mintick)
 
float decyclerSlope  = decyclerClose - decyclerClose[slopeLength]
float normalizedSlope = decyclerSlope / (safeATR * math.sqrt(float(slopeLength)))
 
float bullishStrength = math.min(math.max(normalizedSlope, 0.0), 2.0)
float bearishStrength = math.min(math.max(-normalizedSlope, 0.0), 2.0)
 
float bullishPadding = safeATR * channelPadding * (1.0 + trendBreathing * bullishStrength)
float bearishPadding = safeATR * channelPadding * (1.0 + trendBreathing * bearishStrength)
 
float lowerDefense = lowerChannel - bullishPadding
float upperDefense = upperChannel + bearishPadding
 
float bearishBreakout = lowerDefense - safeATR * reversalOverride
float bullishBreakout = upperDefense + safeATR * reversalOverride
 
bool bearishSlope = normalizedSlope < -slopeConfirmation
bool bullishSlope = normalizedSlope > slopeConfirmation
 
bool bearishBreak = close < lowerDefense and (bearishSlope or close < bearishBreakout)
bool bullishBreak = close > upperDefense and (bullishSlope or close > bullishBreakout)
 
int  warmup = math.max(decyclerPeriod, math.max(atrLength, slopeLength))
bool ready  = bar_index >= warmup and not na(atrValue) and not na(normalizedSlope)
 
// SIGNALS
var int SQ = 0
 
bool longSignal  = false
bool shortSignal = false
 
if barstate.isconfirmed and ready
    if SQ == 0
        SQ := close >= decyclerClose ? 1 : -1
    else if SQ == -1 and bullishBreak
        SQ         := 1
        longSignal := true
    else if SQ == 1 and bearishBreak
        SQ          := -1
        shortSignal := true
 
// COLORS
color col         = SQ == 1 ? longColor : SQ == -1 ? shortColor : color.gray
color colors      = colorBars and SQ != 0 ? col : na
color centerColor = color.new(col, 0)
color upperColor  = color.new(shortColor, bandTransparency)
color lowerColor  = color.new(longColor, bandTransparency)
color fillColor   = color.new(col, fillTransparency)
 
float activeDefense = SQ == 1 ? lowerDefense : SQ == -1 ? upperDefense : na
 
// PLOTS
plot(showCenter ? decyclerClose : na, title="Decycler Trend", color=centerColor, linewidth=3)
upperPlot = plot(showChannel or showFill ? upperChannel : na, title="Decycler Upper Channel", color=showChannel ? upperColor : na, linewidth=1)
lowerPlot = plot(showChannel or showFill ? lowerChannel : na, title="Decycler Lower Channel", color=showChannel ? lowerColor : na, linewidth=1)
fill(upperPlot, lowerPlot, title="Decycler Channel Fill", color=showFill ? fillColor : na)
plot(showDefense ? activeDefense : na, title="Active Trend Defense", color=color.new(col, 35), linewidth=2, style=plot.style_linebr)
plot(decyclerSlope, title="Decycler Slope", display=display.data_window)
plot(normalizedSlope, title="Normalized Decycler Slope", display=display.data_window)
plot(bullishStrength, title="Bullish Trend Strength", display=display.data_window)
plot(bearishStrength, title="Bearish Trend Strength", display=display.data_window)
plot(bullishPadding, title="Bullish Channel Padding", display=display.data_window)
plot(bearishPadding, title="Bearish Channel Padding", display=display.data_window)
plot(lowerDefense, title="Lower Reversal Boundary", display=display.data_window)
plot(upperDefense, title="Upper Reversal Boundary", display=display.data_window)
plot(float(SQ), title="Trend State", 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, force_overlay=true)
plotshape(showSignals and shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, size=size.small, color=shortColor, text="SHORT", textcolor=color.white, force_overlay=true)
barcolor(colors, title = "Color Bars")
plotcandle(open, high, low, close, "Color Bars", colors, colors, bordercolor = colors, force_overlay = true)
 
// ALERTS
alertcondition(longSignal, title="Long Signal", message="Decycler High-Low Breakout LONG on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Decycler High-Low Breakout SHORT on {{ticker}}")

← 返回列表