MESA Phase-Adaptive Band Trend [SchizoQuant]

SchizoQuant · study · 115 行 · 点赞 366 · 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("MESA Phase-Adaptive Band Trend", overlay=true)
 
// USER INPUTS
string phaseTip = "Fast Limit sets the quickest phase response the indicator can use, while Slow Limit sets the slowest. Increase Fast Limit for stronger reactions during rapid phase movement. Lower Slow Limit for more smoothing when phase activity is weak."
string trendTip = "Trend Length controls the EMA centerline, while Volatility Length controls how much return history is used for the adaptive bands. Increase Trend Length for a smoother centerline and increase Volatility Length for steadier band width."
string bandTip  = "Active Multiplier controls band width when phase activity is strong, while Slow Multiplier controls band width when phase activity is weak. Lower Active Multiplier for faster regime changes during active markets, and increase Slow Multiplier to require larger moves during quieter conditions."
string colorTip = "Bullish Color is used for bullish regimes and LONG signals, while Bearish Color is used for bearish regimes and SHORT signals."
 
float src         = input.source(title="Source", defval=hl2, group="Phase Settings", tooltip="Choose the price source used to estimate Hilbert phase behavior and calculate the trend.")
float fastLimit   = input.float(title="Fast Limit", defval=0.50, minval=0.01, maxval=1.0, step=0.01, group="Phase Settings", inline="P1", tooltip=phaseTip)
float slowLimit   = input.float(title="Slow Limit", defval=0.05, minval=0.001, maxval=1.0, step=0.001, group="Phase Settings", inline="P1", tooltip=phaseTip)
int   trendLength = input.int(title="Trend Length", defval=20, minval=2, group="Trend Settings", inline="T1", tooltip=trendTip)
int   volLength   = input.int(title="Volatility Length", defval=20, minval=2, group="Trend Settings", inline="T1", tooltip=trendTip)
float activeMult  = input.float(title="Active Multiplier", defval=1.5, minval=0.1, step=0.1, group="Adaptive Bands", inline="B1", tooltip=bandTip)
float slowMult    = input.float(title="Slow Multiplier", defval=3.0, minval=0.1, step=0.1, group="Adaptive Bands", inline="B1", tooltip=bandTip)
bool  showBands   = input.bool(title="Show Bands", defval=true, group="Visualization", tooltip="Show or hide the phase-responsive volatility bands. Turning this off only changes the chart display and does not affect the trend or signal calculations.")
bool  showFill    = input.bool(title="Show Fill", defval=true, group="Visualization", tooltip="Show or hide the shaded area between the upper and lower adaptive bands. This is visual only and does not change the indicator calculations.")
bool  showSignals = input.bool(title="Show Signals", defval=true, group="Visualization", tooltip="Show or hide LONG and SHORT markers when the active trend 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 the price candles according to the currently active trend 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="C1", tooltip=colorTip)
color shortColor  = input.color(title="Bearish Color", defval=color.rgb(138, 43, 226), group="Color Settings", inline="C1", tooltip=colorTip)
 
// CALCULATIONS
float smooth       = (4.0 * src + 3.0 * nz(src[1], src) + 2.0 * nz(src[2], src) + nz(src[3], src)) / 10.0
var float period   = 10.0
float periodFactor = 0.075 * nz(period[1], 10.0) + 0.54
 
float detrender = (0.0962 * smooth + 0.5769 * nz(smooth[2]) - 0.5769 * nz(smooth[4]) - 0.0962 * nz(smooth[6])) * periodFactor
float q1        = (0.0962 * detrender + 0.5769 * nz(detrender[2]) - 0.5769 * nz(detrender[4]) - 0.0962 * nz(detrender[6])) * periodFactor
float i1        = nz(detrender[3])
 
float jI = (0.0962 * i1 + 0.5769 * nz(i1[2]) - 0.5769 * nz(i1[4]) - 0.0962 * nz(i1[6])) * periodFactor
float jQ = (0.0962 * q1 + 0.5769 * nz(q1[2]) - 0.5769 * nz(q1[4]) - 0.0962 * nz(q1[6])) * periodFactor
 
float i2Raw = i1 - jQ
float q2Raw = q1 + jI
 
var float i2 = 0.0
var float q2 = 0.0
 
i2 := 0.2 * i2Raw + 0.8 * nz(i2[1])
q2 := 0.2 * q2Raw + 0.8 * nz(q2[1])
 
float reRaw = i2 * nz(i2[1]) + q2 * nz(q2[1])
float imRaw = i2 * nz(q2[1]) - q2 * nz(i2[1])
 
var float re = 0.0
var float im = 0.0
 
re := 0.2 * reRaw + 0.8 * nz(re[1])
im := 0.2 * imRaw + 0.8 * nz(im[1])
 
float angle          = re != 0.0 ? math.atan(im / re) * 180.0 / math.pi : 0.0
float rawPeriod      = angle != 0.0 ? math.abs(360.0 / angle) : nz(period[1], 10.0)
float previousPeriod = nz(period[1], 10.0)
float limitedPeriod  = math.min(rawPeriod, 1.5 * previousPeriod)
 
limitedPeriod := math.max(limitedPeriod, 0.67 * previousPeriod)
limitedPeriod := math.max(6.0, math.min(50.0, limitedPeriod))
period        := 0.2 * limitedPeriod + 0.8 * previousPeriod
 
var float phase = 0.0
phase := i1 != 0.0 ? math.atan(q1 / i1) * 180.0 / math.pi : nz(phase[1])
 
float deltaPhase = nz(phase[1]) - phase
deltaPhase := math.max(deltaPhase, 1.0)
 
float alphaPhase = fastLimit / deltaPhase
alphaPhase := math.max(alphaPhase, slowLimit)
 
float phaseRange    = math.max(fastLimit - slowLimit, 0.0001)
float phaseActivity = math.max(0.0, math.min(1.0, (alphaPhase - slowLimit) / phaseRange))
 
float trend       = ta.ema(src, trendLength)
float priceReturn = src[1] != 0.0 ? (src - src[1]) / src[1] : 0.0
float volatility  = ta.stdev(priceReturn, volLength)[1]
 
float adaptiveMult = slowMult - (slowMult - activeMult) * phaseActivity
float upperBand    = trend + adaptiveMult * volatility * trend
float lowerBand    = trend - adaptiveMult * volatility * trend
 
// SIGNALS
bool long  = close > upperBand
bool short = close < lowerBand
 
var int SQ = 0
 
if long
    SQ := 1
else if short
    SQ := -1
 
bool longSignal  = SQ == 1 and SQ[1] != 1
bool shortSignal = SQ == -1 and SQ[1] != -1
 
// COLORS
color col = SQ == 1 ? longColor : shortColor
 
// PLOTS
plot(trend, title="Trend", color=col, linewidth=2)
upperPlot = plot(showBands ? upperBand : na, title="Upper Phase Band", color=color.new(longColor, 35), linewidth=1)
lowerPlot = plot(showBands ? lowerBand : na, title="Lower Phase Band", color=color.new(shortColor, 35), linewidth=1)
fill(upperPlot, lowerPlot, title="Phase Band Fill", color=showFill ? color.new(col, 90) : na)
plotshape(showSignals and longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, color=longColor, size=size.small, text="LONG", textcolor=color.white)
plotshape(showSignals and shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=shortColor, size=size.small, text="SHORT", textcolor=color.white)
barcolor(colorBars ? col : na)
 
// ALERTS
 
alertcondition(longSignal, title="Long Signal", message="Phase-Responsive Band Trend Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Phase-Responsive Band Trend Short Signal on {{ticker}}")

← 返回列表