ADX Lifecycle Trail [SchizoQuant]

SchizoQuant · study · 126 行 · 点赞 167 · 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("ADX Lifecycle Trail", overlay=true)
 
// USER INPUTS
string dmiTip       = "Sets the directional movement horizon used to calculate +DI, -DI, and ADX."
string dominanceTip = "Sets the minimum normalized +DI versus -DI dominance required to confirm an opposite trend reversal. Higher values require stronger directional dominance before the trend regime can reverse."
string lifecycleTip = "Measures how ADX has changed over the selected lookback. Strengthening ADX shifts structural defense toward the longer horizon while weakening ADX shifts defense toward the shorter horizon."
string scaleTip     = "Controls how much ADX change is required to move fully between short, medium, and long structural defense. Higher values make lifecycle adaptation slower while lower values make it more responsive."
string structureTip = "Defines three completed structural horizons. The ADX lifecycle continuously blends between these fixed horizons rather than changing their lengths dynamically."
string atrTip       = "Sets the ATR period used to initialize the trend trail after a new bullish or bearish regime begins."
string resetTip     = "Controls the initial ATR-based trail distance after a regime reversal before structural defense takes over. Higher values initialize the trail farther from price."
string trailTip     = "Shows or hides the active structural trend trail. Disabling this option only affects the display and does not change calculations or signals."
string signalsTip   = "Shows or hides LONG and SHORT markers when the internal trend regime changes. Disabling this option only affects the display and does not change signal calculations."
string barsTip      = "Shows or hides candles colored according to the active bullish or bearish trend regime. Disabling this option only affects the display and does not change calculations or signals."
string colorTip     = "Bullish Color represents the active bullish trend regime while Bearish Color represents the active bearish trend regime."
 
float src             = input.source(title="Source", defval=close, group="Directional Engine", tooltip="Choose the price source used for trend-trail break confirmation.")
int   dmiLength       = input.int(title="DMI Length", defval=17, minval=2, group="Directional Engine", inline="D1", tooltip=dmiTip)
float dominanceLevel  = input.float(title="Dominance Required", defval=0.10, minval=0.00, maxval=0.80, step=0.01, group="Directional Engine", inline="D1", tooltip=dominanceTip)
int   lifecycleLength = input.int(title="ADX Lifecycle Lookback", defval=4, minval=1, group="Lifecycle Adaptation", inline="A1", tooltip=lifecycleTip)
float lifecycleScale  = input.float(title="Lifecycle Scale", defval=8.0, minval=0.5, step=0.5, group="Lifecycle Adaptation", inline="A1", tooltip=scaleTip)
int   shortLength     = input.int(title="Short Defense", defval=5, minval=2, group="Structural Defense", inline="S1", tooltip=structureTip)
int   mediumLength    = input.int(title="Medium Defense", defval=12, minval=3, group="Structural Defense", inline="S1", tooltip=structureTip)
int   longLength      = input.int(title="Long Defense", defval=24, minval=4, group="Structural Defense", inline="S1", tooltip=structureTip)
int   atrLength       = input.int(title="ATR Length", defval=14, minval=1, group="Trail Reset", inline="R1", tooltip=atrTip)
float resetMultiplier = input.float(title="Reset Multiplier", defval=2.5, minval=0.1, step=0.1, group="Trail Reset", inline="R1", tooltip=resetTip)
bool  showTrail       = input.bool(title="Show Trend Trail", defval=true, group="Visualization", tooltip=trailTip)
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 upMove   = high - nz(high[1], high)
float downMove = nz(low[1], low) - low
 
float plusDM  = upMove > downMove and upMove > 0.0 ? upMove : 0.0
float minusDM = downMove > upMove and downMove > 0.0 ? downMove : 0.0
 
float trueRange  = ta.tr(true)
float smoothedTR = math.max(ta.rma(trueRange, dmiLength), syminfo.mintick)
 
float plusDI  = 100.0 * ta.rma(plusDM, dmiLength) / smoothedTR
float minusDI = 100.0 * ta.rma(minusDM, dmiLength) / smoothedTR
 
float diSum     = plusDI + minusDI
float dominance = diSum > 0.0 ? (plusDI - minusDI) / diSum : 0.0
 
float dx  = diSum > 0.0 ? 100.0 * math.abs(plusDI - minusDI) / diSum : 0.0
float adx = ta.rma(dx, dmiLength)
 
float previousADX = nz(adx[lifecycleLength], adx)
float adxChange   = adx - previousADX
 
float lifecycle = adxChange / lifecycleScale
lifecycle := math.max(-1.0, math.min(1.0, lifecycle))
 
float shortLow  = nz(ta.lowest(low, shortLength)[1], low)
float mediumLow = nz(ta.lowest(low, mediumLength)[1], low)
float longLow   = nz(ta.lowest(low, longLength)[1], low)
 
float shortHigh  = nz(ta.highest(high, shortLength)[1], high)
float mediumHigh = nz(ta.highest(high, mediumLength)[1], high)
float longHigh   = nz(ta.highest(high, longLength)[1], high)
 
float bullishDefense = lifecycle >= 0.0 ? mediumLow + lifecycle * (longLow - mediumLow) : mediumLow + (-lifecycle) * (shortLow - mediumLow)
float bearishDefense = lifecycle >= 0.0 ? mediumHigh + lifecycle * (longHigh - mediumHigh) : mediumHigh + (-lifecycle) * (shortHigh - mediumHigh)
 
float atrValue      = math.max(ta.atr(atrLength), syminfo.mintick)
float resetDistance = atrValue * resetMultiplier
 
// SIGNALS
var int   SQ         = dominance >= 0.0 ? 1 : -1
var float trendTrail = na
 
int previousSQ = nz(SQ[1], SQ)
 
float previousTrail = nz(trendTrail[1], previousSQ == 1 ? src - resetDistance : src + resetDistance)
 
if previousSQ == 1
    trendTrail := math.max(previousTrail, bullishDefense)
else
    trendTrail := math.min(previousTrail, bearishDefense)
 
bool long  = previousSQ == -1 and src > trendTrail and dominance > dominanceLevel
bool short = previousSQ == 1 and src < trendTrail and dominance < -dominanceLevel
 
if long
    SQ := 1
    trendTrail := src - resetDistance
else if short
    SQ := -1
    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
color colors = colorBars ? col : na
 
// PLOTS
plot(showTrail and SQ == 1 ? trendTrail : na, title="Bullish Lifecycle Trail", color=longColor, linewidth=2, style=plot.style_linebr)
plot(showTrail and SQ == -1 ? trendTrail : na, title="Bearish Lifecycle Trail", color=shortColor, linewidth=2, style=plot.style_linebr)
 
plot(dominance * 100.0, title="DMI Dominance", display=display.data_window)
plot(plusDI, title="+DI", display=display.data_window)
plot(minusDI, title="-DI", display=display.data_window)
plot(adx, title="ADX", display=display.data_window)
plot(adxChange, title="ADX Change", display=display.data_window)
plot(lifecycle, title="ADX Lifecycle", display=display.data_window)
plot(bullishDefense, title="Bullish Structural Defense", display=display.data_window)
plot(bearishDefense, title="Bearish Structural Defense", 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(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="ADX Lifecycle Trail Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="ADX Lifecycle Trail Short Signal on {{ticker}}")

← 返回列表