本页源码来自 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("Band-Channel Persistence Trend", overlay=true)
// USER INPUTS
string sourceTip = "Choose the price source used to calculate the Ehlers SuperSmoother trend center and adaptive volatility bands."
string filterTip = "Sets the smoothing period of the two-pole Ehlers SuperSmoother. Lower values react faster to price changes, while higher values produce a steadier trend center."
string bandTip = "Sets the lookback used to measure the root mean square distance between price and its smoothed center. Higher values produce a more stable volatility envelope."
string multiplierTip = "Controls the width of the adaptive volatility bands. Higher values provide more room for price fluctuations before a potential trend reversal."
string atrTip = "Sets the ATR period used to measure market volatility and establish the minimum adaptive band distance."
string floorTip = "Sets the minimum band distance in ATR multiples, preventing the adaptive volatility envelope from becoming excessively narrow."
string channelTip = "Sets the lookback for the structural channel using highs and lows from previously completed candles. The channel helps determine the active trend-defense boundary."
string residenceTip = "Measures how consistently price closes above or below the smoothed center. Strong directional residence increases the influence of structural channel boundaries."
string structureTip = "Sets the lookback for detecting decisive countertrend breaks beyond previously completed price structure. Such breaks can accelerate reversal confirmation."
string confirmTip = "Sets the number of consecutive closing-price breaches required to confirm a reversal when a decisive structural break has not occurred."
string defenseTip = "Shows or hides the persistent bullish and bearish trailing defense lines. Disabling this option only affects visualization and does not change signal calculations."
string bandsTip = "Shows or hides the adaptive volatility bands and the shaded region between them. Disabling this option does not affect calculations or signals."
string showChannelTip = "Shows or hides the upper and lower structural channel boundaries calculated from previously completed candles."
string signalsTip = "Shows or hides LONG and SHORT markers when the persistent trend regime changes on a confirmed candle."
string barsTip = "Shows or hides candles colored according to the active bullish or bearish trend regime."
string colorTip = "Bullish Color represents bullish trend conditions and LONG signals, while Bearish Color represents bearish trend conditions and SHORT signals."
float source = input.source(title="Source", defval=hlc3, group="Trend Settings", tooltip=sourceTip)
int filterLength = input.int(title="Filter Length", defval=12, minval=3, group="Trend Settings", tooltip=filterTip)
int bandLength = input.int(title="Band Length", defval=20, minval=2, group="Band Settings", tooltip=bandTip)
float bandMultiplier = input.float(title="Band Multiplier", defval=1.9, minval=0.1, step=0.1, group="Band Settings", tooltip=multiplierTip)
int atrLength = input.int(title="ATR Length", defval=14, minval=1, group="Band Settings", tooltip=atrTip)
float atrFloor = input.float(title="ATR Floor", defval=0.75, minval=0.1, step=0.05, group="Band Settings", tooltip=floorTip)
int channelLength = input.int(title="Channel Length", defval=22, minval=2, group="Channel Settings", tooltip=channelTip)
int residenceLength = input.int(title="Residence Length", defval=12, minval=2, group="Channel Settings", tooltip=residenceTip)
int structureLength = input.int(title="Reversal Structure", defval=5, minval=2, group="Signal Settings", tooltip=structureTip)
int confirmBars = input.int(title="Reversal Confirmation", defval=2, minval=1, maxval=4, group="Signal Settings", tooltip=confirmTip)
bool showDefense = input.bool(title="Show Trend Defense", defval=true, group="Visualization", tooltip=defenseTip)
bool showBands = input.bool(title="Show Bands", defval=true, group="Visualization", tooltip=bandsTip)
bool showChannel = input.bool(title="Show Channel", defval=true, group="Visualization", tooltip=showChannelTip)
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 a1 = math.exp(-1.414 * math.pi / filterLength)
float b1 = 2.0 * a1 * math.cos(1.414 * math.pi / filterLength)
float c2 = b1
float c3 = -a1 * a1
float c1 = 1.0 - c2 - c3
var float center = na
center := na(center[1]) or na(center[2]) ? source : c1 * (source + source[1]) * 0.5 + c2 * center[1] + c3 * center[2]
float atrValue = ta.atr(atrLength)
float residual = source - center
float rms = math.sqrt(ta.sma(residual * residual, bandLength))
float bandDistance = math.max(rms * bandMultiplier, atrValue * atrFloor)
float upperBand = center + bandDistance
float lowerBand = center - bandDistance
float channelHigh = ta.highest(high[1], channelLength)
float channelLow = ta.lowest(low[1], channelLength)
float recentHigh = ta.highest(high[1], structureLength)
float recentLow = ta.lowest(low[1], structureLength)
float bullishResidence = ta.sma(close > center ? 1.0 : 0.0, residenceLength)
float bearishResidence = 1.0 - bullishResidence
float longWeight = 0.15 + 0.60 * bullishResidence
float shortWeight = 0.15 + 0.60 * bearishResidence
float longBoundary = lowerBand * (1.0 - longWeight) + channelLow * longWeight
float shortBoundary = upperBand * (1.0 - shortWeight) + channelHigh * shortWeight
bool ready = not na(bandDistance) and not na(channelHigh) and not na(channelLow) and not na(recentHigh) and not na(recentLow) and not na(bullishResidence) and not na(atrValue)
var int SQ = 0
var float defense = na
var int breachCount = 0
// SIGNALS
bool longSignal = false
bool shortSignal = false
if barstate.isconfirmed and ready
if SQ == 0
SQ := close >= center ? 1 : -1
breachCount := 0
defense := SQ == 1 ? math.min(longBoundary, close - atrValue * 0.75) : math.max(shortBoundary, close + atrValue * 0.75)
else if SQ == 1
bool breached = close < defense
breachCount := breached ? breachCount + 1 : 0
bool decisiveBreak = breached and close < recentLow
bool confirmedBreak = breached and breachCount >= confirmBars
if decisiveBreak or confirmedBreak
SQ := -1
defense := math.max(shortBoundary, close + atrValue * 0.75)
breachCount := 0
shortSignal := true
else if not breached
defense := math.max(defense, math.min(longBoundary, close - syminfo.mintick))
else
bool breached = close > defense
breachCount := breached ? breachCount + 1 : 0
bool decisiveBreak = breached and close > recentHigh
bool confirmedBreak = breached and breachCount >= confirmBars
if decisiveBreak or confirmedBreak
SQ := 1
defense := math.min(longBoundary, close - atrValue * 0.75)
breachCount := 0
longSignal := true
else if not breached
defense := math.min(defense, math.max(shortBoundary, close + syminfo.mintick))
// COLORS
color col = SQ == 1 ? longColor : SQ == -1 ? shortColor : color.gray
color colors = colorBars and SQ != 0 ? col : na
// PLOTS
plot(showDefense and SQ == 1 ? defense : na, title="LONG Defense", color=longColor, linewidth=3, style=plot.style_linebr)
plot(showDefense and SQ == -1 ? defense : na, title="SHORT Defense", color=shortColor, linewidth=3, style=plot.style_linebr)
upperPlot = plot(showBands ? upperBand : na, title="Upper Band", color=color.new(shortColor, 78), linewidth=1)
lowerPlot = plot(showBands ? lowerBand : na, title="Lower Band", color=color.new(longColor, 78), linewidth=1)
fill(upperPlot, lowerPlot, title="Adaptive Band Area", color=color.new(color.gray, 94))
plot(showChannel ? channelHigh : na, title="Channel High", color=color.new(shortColor, 83), linewidth=1)
plot(showChannel ? channelLow : na, title="Channel Low", color=color.new(longColor, 83), linewidth=1)
plot(float(SQ), title="Trend State", display=display.data_window)
plot(bullishResidence, title="Bullish Residence", display=display.data_window)
plot(bearishResidence, title="Bearish Residence", display=display.data_window)
plot(bandDistance, title="Adaptive Band Distance", display=display.data_window)
plot(longWeight, title="Bullish Structural Weight", display=display.data_window)
plot(shortWeight, title="Bearish Structural Weight", display=display.data_window)
plot(float(breachCount), title="Defense Breach Count", display=display.data_window)
plotshape(showSignals and longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, color=longColor, size=size.small, text="LONG", textcolor=color.white, force_overlay=true)
plotshape(showSignals and shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=shortColor, size=size.small, 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="SchizoQuant Band-Channel Persistence Trend: LONG")
alertcondition(shortSignal, title="SHORT Signal", message="SchizoQuant Band-Channel Persistence Trend: SHORT")