Adaptive Persistence Trail [SchizoQuant]

SchizoQuant · study · 113 行 · 点赞 109 · 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("Adaptive Persistence Trail", overlay=true)
 
// USER INPUTS
string persistenceTip = "Sets the lookback used to compare cumulative upward and downward price travel. More one-sided movement produces stronger directional persistence, while balanced movement represents less directional conditions."
string accelerationTip = "Minimum Acceleration controls how slowly the trail advances during weak persistence, while Maximum Acceleration controls how quickly it approaches the active regime extreme during strongly directional conditions."
string protectionTip = "Sets the recent price-structure window used to prevent the accelerating trail from advancing through nearby highs or lows. Increase it for broader structural protection or decrease it for a tighter trail."
string resetTip = "Reset ATR Length controls the volatility lookback used when a new regime begins, while Reset Multiplier scales how far the new trail is initially placed from price. ATR is used only for this reset distance and does not continuously drive the trail."
string colorTip = "Bullish Color represents bullish persistence regimes, LONG signals, and bullish visual elements, while Bearish Color represents bearish persistence regimes, SHORT signals, and bearish visual elements."
 
float src               = input.source(title="Source", defval=close, group="Persistence Settings", tooltip="Choose the price source used to measure directional price travel and evaluate trail reversals.")
int   persistenceLength = input.int(title="Persistence Length", defval=20, minval=2, group="Persistence Settings", tooltip=persistenceTip)
float minAcceleration   = input.float(title="Minimum Acceleration", defval=0.5, minval=0.01, maxval=1.00, step=0.01, group="Acceleration Trail", inline="A1", tooltip=accelerationTip)
float maxAcceleration   = input.float(title="Maximum Acceleration", defval=1, minval=0.01, maxval=1.00, step=0.01, group="Acceleration Trail", inline="A1", tooltip=accelerationTip)
int   protectionLength  = input.int(title="Structure Protection", defval=21, minval=1, group="Acceleration Trail", tooltip=protectionTip)
int   atrLength         = input.int(title="Reset ATR Length", defval=14, minval=1, group="Trail Reset", inline="R1", tooltip=resetTip)
float resetMultiplier   = input.float(title="Reset Multiplier", defval=2, minval=0.10, step=0.10, group="Trail Reset", inline="R1", tooltip=resetTip)
bool  showTrail         = input.bool(title="Show Trail", defval=true, group="Visualization", tooltip="Show or hide the adaptive persistence acceleration trail. Turning this off only changes the chart display and does not affect regime calculations or signals.")
bool  showSignals       = input.bool(title="Show Signals", defval=true, group="Visualization", tooltip="Show or hide LONG and SHORT markers whenever the active persistence 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 persistence 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="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 bullishTravel = 0.0
float bearishTravel = 0.0
 
for i = 0 to persistenceLength - 1
    float priceChange = nz(src[i], src) - nz(src[i + 1], src)
 
    if priceChange > 0.0
        bullishTravel += priceChange
    else if priceChange < 0.0
        bearishTravel += -priceChange
 
float totalTravel = bullishTravel + bearishTravel
float persistence = totalTravel > 0.0 ? (bullishTravel - bearishTravel) / totalTravel : 0.0
persistence := math.max(-1.0, math.min(1.0, persistence))
 
float clarity = math.abs(persistence)
 
float lowerAcceleration = math.min(minAcceleration, maxAcceleration)
float upperAcceleration = math.max(minAcceleration, maxAcceleration)
float acceleration      = lowerAcceleration + clarity * (upperAcceleration - lowerAcceleration)
 
float atrValue      = math.max(ta.atr(atrLength), syminfo.mintick)
float resetDistance = atrValue * resetMultiplier
 
float recentLow  = ta.lowest(low, protectionLength)
float recentHigh = ta.highest(high, protectionLength)
 
// SIGNALS
var int SQ              = persistence >= 0.0 ? 1 : -1
var float regimeExtreme = src
var float trendTrail    = na
 
int previousSQ          = nz(SQ[1], SQ)
float previousExtreme   = nz(regimeExtreme[1], src)
float previousTrail     = nz(trendTrail[1], previousSQ == 1 ? src - resetDistance : src + resetDistance)
 
if previousSQ == 1
    regimeExtreme := math.max(previousExtreme, high)
 
    float acceleratedTrail = previousTrail + acceleration * (regimeExtreme - previousTrail)
    float protectedTrail   = math.min(acceleratedTrail, recentLow)
 
    trendTrail := math.max(previousTrail, protectedTrail)
else
    regimeExtreme := math.min(previousExtreme, low)
 
    float acceleratedTrail = previousTrail + acceleration * (regimeExtreme - previousTrail)
    float protectedTrail   = math.max(acceleratedTrail, recentHigh)
 
    trendTrail := math.min(previousTrail, protectedTrail)
 
bool long  = previousSQ == -1 and src > trendTrail
bool short = previousSQ == 1 and src < trendTrail
 
if long
    SQ            := 1
    regimeExtreme := high
    trendTrail    := src - resetDistance
else if short
    SQ            := -1
    regimeExtreme := low
    trendTrail    := src + resetDistance
else
    SQ := previousSQ
 
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(showTrail and SQ == 1 ? trendTrail : na, title="Bullish Persistence Trail", color=longColor, linewidth=2, style=plot.style_linebr)
plot(showTrail and SQ == -1 ? trendTrail : na, title="Bearish Persistence Trail", color=shortColor, linewidth=2, style=plot.style_linebr)
plot(persistence, title="Directional Persistence", display=display.data_window)
plot(clarity, title="Trend Clarity", display=display.data_window)
plot(acceleration, title="Trail Acceleration", display=display.data_window)
plot(regimeExtreme, title="Regime Extreme", display=display.data_window)
plot(resetDistance, title="Reset Distance", 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="Smart Persistence Acceleration Trail Trend Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Smart Persistence Acceleration Trail Trend Short Signal on {{ticker}}")

← 返回列表