Adaptive VIDYA Trend [SchizoQuant]

SchizoQuant · study · 83 行 · 点赞 259 · 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 VIDYA Trend", overlay=true)
 
// USER INPUTS
string shortTip      = "Sets the short-term standard deviation lookback used in the VIDYA volatility ratio. Lower values make the adaptive response more sensitive to recent volatility."
string longTip       = "Sets the long-term standard deviation lookback used as the reference volatility in the VIDYA adaptation ratio. Higher values create a slower volatility reference."
string smoothingTip  = "Sets the base smoothing factor multiplied by the short-to-long volatility ratio. Higher values make VIDYA respond faster to price changes."
string bandTip       = "Sets the lookback used to measure the standard deviation of price displacement from VIDYA. Higher values create a steadier estimate of residual volatility."
string multiplierTip = "Upper Band Multiplier controls the residual-deviation distance above VIDYA, while Lower Band Multiplier controls the distance below it. Higher values widen the corresponding side and require stronger price displacement before the trend regime changes."
string bandsTip      = "Shows or hides the adaptive upper and lower VIDYA bands. Disabling this option only affects the display and does not change calculations or signals."
string fillTip       = "Shows or hides the colored area between the adaptive VIDYA bands. 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 active trend regime changes. Disabling this option only affects the display and does not change signal calculations."
string barsTip       = "Colors price candles 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 bullish trend regimes and LONG signals, while Bearish Color represents bearish regimes and SHORT signals."
 
float src             = input.source(title="Source", defval=close, group="VIDYA Settings", tooltip="Choose the price source used by the adaptive VIDYA calculation, residual measurement, bands, and regime logic.")
int   shortLen        = input.int(title="Short Deviation Length", defval=10, minval=2, group="VIDYA Settings", inline="V1", tooltip=shortTip)
int   longLen         = input.int(title="Long Deviation Length", defval=30, minval=3, group="VIDYA Settings", inline="V1", tooltip=longTip)
float baseSmoothing   = input.float(title="Base Smoothing", defval=0.20, minval=0.01, maxval=1.00, step=0.01, group="VIDYA Settings", tooltip=smoothingTip)
int   bandLength      = input.int(title="Band Length", defval=20, minval=2, group="Adaptive Bands", tooltip=bandTip)
float upperMultiplier = input.float(title="Upper Band Multiplier", defval=1.50, minval=0.10, step=0.10, group="Adaptive Bands", inline="B1", tooltip=multiplierTip)
float lowerMultiplier = input.float(title="Lower Band Multiplier", defval=1.50, minval=0.10, step=0.10, group="Adaptive Bands", inline="B1", tooltip=multiplierTip)
bool  showBands       = input.bool(title="Show Bands", defval=true, group="Visualization", tooltip=bandsTip)
bool  showFill        = input.bool(title="Show Fill", defval=true, group="Visualization", tooltip=fillTip)
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 shortDeviation = ta.stdev(src, shortLen)
float longDeviation  = ta.stdev(src, longLen)
 
float k = longDeviation > 0.0 ? shortDeviation / longDeviation : 0.0
 
float adaptiveSmoothing = k * baseSmoothing
adaptiveSmoothing := math.max(0.0, math.min(1.0, adaptiveSmoothing))
 
var float vidya = na
vidya := na(vidya[1]) ? src : adaptiveSmoothing * src + (1.0 - adaptiveSmoothing) * vidya[1]
 
float residual          = src - vidya
float residualDeviation = ta.stdev(residual, bandLength)
 
float upperBand = vidya + residualDeviation * upperMultiplier
float lowerBand = vidya - residualDeviation * lowerMultiplier
 
// SIGNALS
bool long  = src > upperBand
bool short = src < lowerBand
 
var int SQ = src >= vidya ? 1 : -1
 
if long
    SQ := 1
else if short
    SQ := -1
 
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(vidya, title="Adaptive VIDYA", color=col, linewidth=3)
upperPlot = plot(showBands ? upperBand : na, title="Upper Adaptive Band", color=color.new(longColor, 65), linewidth=1)
lowerPlot = plot(showBands ? lowerBand : na, title="Lower Adaptive Band", color=color.new(shortColor, 65), linewidth=1)
fill(upperPlot, lowerPlot, color=showFill ? color.new(col, 90) : na, title="Adaptive Trend Fill")
plot(k, title="VIDYA Volatility Ratio", display=display.data_window)
plot(adaptiveSmoothing, title="VIDYA Adaptive Smoothing", display=display.data_window)
plot(residual, title="Price VIDYA Residual", display=display.data_window)
plot(residualDeviation, title="Residual Deviation", 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="Adaptive VIDYA Trend Long Signal on {{ticker}}")
alertcondition(shortSignal, title="Short Signal", message="Adaptive VIDYA Trend Short Signal on {{ticker}}")

← 返回列表