本页源码来自 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 Donchian Breakout Channel", overlay=true)
// USER INPUTS
string breakoutTip = "Sets the lookback used to identify confirmed Donchian breakouts. Only previously completed candles establish the breakout boundaries."
string fastTip = "Sets the shorter Donchian lookback used for responsive trend defense. Lower values detect structural reversals sooner."
string slowTip = "Sets the longer Donchian lookback used for wider trend defense. Higher values allow established trends to withstand deeper pullbacks."
string acceptanceTip = "Sets the smoothing period for closing-price position within the Donchian channel. Higher values emphasize sustained price acceptance rather than isolated movements."
string depthTip = "Controls the maximum influence of the wider Donchian boundary when price consistently holds the favorable side of its channel. Higher values permit deeper countertrend pullbacks."
string bufferTip = "Sets the additional channel-width penetration required beyond the adaptive defense before confirming a reversal. Higher values reduce marginal reversal signals."
string bandsTip = "Shows or hides the upper and lower Donchian breakout bands and their channel fill. Disabling this option does not affect calculations."
string bandTransparencyTip = "Controls the transparency of the upper and lower Donchian band lines. Higher values make the bands less visible without affecting calculations."
string fillTransparencyTip = "Controls the transparency of the shaded Donchian channel. Higher values produce a lighter background without affecting calculations."
string midpointTip = "Shows or hides the midpoint of the Donchian breakout channel as a visual reference."
string defenseTip = "Shows or hides the adaptive Donchian reversal boundary for the active trend."
string signalsTip = "Shows or hides LONG and SHORT triangles on the main price chart when the confirmed trend direction changes."
string barsTip = "Shows or hides candles colored according to the active bullish or bearish trend."
string colorTip = "Bullish Color represents bullish conditions and LONG signals, while Bearish Color represents bearish conditions and SHORT signals."
int breakoutLength = input.int(title="Breakout Length", defval=24, minval=2, group="Trend Settings", tooltip=breakoutTip)
int fastLength = input.int(title="Fast Donchian Length", defval=8, minval=2, group="Trend Settings", tooltip=fastTip)
int slowLength = input.int(title="Slow Donchian Length", defval=21, minval=3, group="Trend Settings", tooltip=slowTip)
int acceptanceLen = input.int(title="Acceptance Length", defval=6, minval=1, group="Adaptive Settings", tooltip=acceptanceTip)
float maximumDepth = input.float(title="Maximum Defense Depth", defval=0.70, minval=0.0, maxval=1.0, step=0.05, group="Adaptive Settings", tooltip=depthTip)
float reversalBuffer = input.float(title="Reversal Buffer", defval=0.02, minval=0.0, maxval=0.25, step=0.01, group="Signal Settings", tooltip=bufferTip)
bool showBands = input.bool(title="Show Donchian Bands", defval=true, group="Visualization", tooltip=bandsTip)
int bandTransparency = input.int(title="Band Transparency", defval=20, minval=0, maxval=100, group="Visualization", tooltip=bandTransparencyTip)
int fillTransparency = input.int(title="Band Fill Transparency", defval=80, minval=0, maxval=100, group="Visualization", tooltip=fillTransparencyTip)
bool showMidpoint = input.bool(title="Show Midpoint", defval=false, group="Visualization", tooltip=midpointTip)
bool showDefense = input.bool(title="Show Adaptive Defense", defval=true, group="Visualization", tooltip=defenseTip)
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 upperBand = ta.highest(high[1], breakoutLength)
float lowerBand = ta.lowest(low[1], breakoutLength)
float midpoint = (upperBand + lowerBand) * 0.5
float channelWidth = math.max(upperBand - lowerBand, syminfo.mintick)
float fastHigh = ta.highest(high[1], fastLength)
float fastLow = ta.lowest(low[1], fastLength)
float slowHigh = ta.highest(high[1], slowLength)
float slowLow = ta.lowest(low[1], slowLength)
float nearSupport = math.max(fastLow, slowLow)
float deepSupport = math.min(fastLow, slowLow)
float nearResistance = math.min(fastHigh, slowHigh)
float deepResistance = math.max(fastHigh, slowHigh)
float location = math.max(0.0, math.min(1.0, (close - lowerBand) / channelWidth))
float acceptance = ta.ema(location, acceptanceLen)
float bullishQuality = math.max(0.0, math.min(1.0, (acceptance - 0.35) / 0.55))
float bearishQuality = math.max(0.0, math.min(1.0, (0.65 - acceptance) / 0.55))
float bullishDepth = maximumDepth * bullishQuality
float bearishDepth = maximumDepth * bearishQuality
float longDefense = nearSupport * (1.0 - bullishDepth) + deepSupport * bullishDepth
float shortDefense = nearResistance * (1.0 - bearishDepth) + deepResistance * bearishDepth
float bufferDistance = channelWidth * reversalBuffer
bool ready = not na(upperBand) and not na(lowerBand) and not na(fastHigh) and not na(fastLow) and not na(slowHigh) and not na(slowLow) and not na(acceptance)
// SIGNALS
var int SQ = 0
bool bullishBreakout = ready and close > upperBand
bool bearishBreakout = ready and close < lowerBand
bool bullishReversal = ready and close > shortDefense + bufferDistance
bool bearishReversal = ready and close < longDefense - bufferDistance
bool longSignal = false
bool shortSignal = false
if barstate.isconfirmed and ready
if SQ == 0
if bullishBreakout
SQ := 1
longSignal := true
else if bearishBreakout
SQ := -1
shortSignal := true
else if SQ == -1 and bullishReversal
SQ := 1
longSignal := true
else if SQ == 1 and bearishReversal
SQ := -1
shortSignal := true
// COLORS
color col = SQ == 1 ? longColor : SQ == -1 ? shortColor : color.gray
color colors = colorBars and SQ != 0 ? col : na
color bandColor = color.new(col, bandTransparency)
color fillColor = SQ == 1 ? color.new(longColor, fillTransparency) : SQ == -1 ? color.new(shortColor, fillTransparency) : color.new(color.gray, math.min(100, fillTransparency + 2))
color defenseColor = color.new(col, 0)
float activeDefense = SQ == 1 ? longDefense : SQ == -1 ? shortDefense : na
// PLOTS
upperPlot = plot(showBands ? upperBand : na, title="Upper Donchian Band", color=bandColor, linewidth=1)
lowerPlot = plot(showBands ? lowerBand : na, title="Lower Donchian Band", color=bandColor, linewidth=1)
fill(upperPlot, lowerPlot, title="Donchian Channel", color=showBands ? fillColor : na)
plot(showMidpoint ? midpoint : na, title="Donchian Midpoint", color=color.new(col, 65), linewidth=1)
plot(showDefense ? activeDefense : na, title="Adaptive Donchian Defense", color=defenseColor, linewidth=2, style=plot.style_linebr)
plot(acceptance, title="Channel Acceptance", display=display.data_window)
plot(bullishQuality, title="Bullish Acceptance Quality", display=display.data_window)
plot(bearishQuality, title="Bearish Acceptance Quality", display=display.data_window)
plot(bullishDepth, title="Bullish Defense Depth", display=display.data_window)
plot(bearishDepth, title="Bearish Defense Depth", display=display.data_window)
plot(longDefense, title="Bullish Defense", display=display.data_window)
plot(shortDefense, title="Bearish Defense", 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="Adaptive Donchian Breakout Channel LONG on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Adaptive Donchian Breakout Channel SHORT on {{ticker}}")