本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Vector Trend ", shorttitle="Vector Trend ", overlay=true, max_lines_count=500)
//====================================================
// 01. INPUTS
//====================================================
groupEngine = "Ehlers Engine"
src = input.source(close, "Price Source", group=groupEngine)
alpha = input.float(0.12, "Trend Response", minval=0.02, maxval=0.50, step=0.01, group=groupEngine)
//====================================================
// 02. TREND DEFENSE
//====================================================
groupDefense = "Trend Defense"
noiseLength = input.int(20, "Noise Length", minval=2, group=groupDefense)
bandMultiplier = input.float(2.7, "Defense Multiplier", minval=0.2, step=0.1, group=groupDefense)
atrLength = input.int(14, "ATR Length", minval=1, group=groupDefense)
minimumATR = input.float(0.8, "Minimum ATR Distance", minval=0.1, step=0.1, group=groupDefense)
//====================================================
// 03. REVERSAL LOGIC
//====================================================
groupReversal = "Reversal Logic"
reversalStrength = input.float(0.5, "Reversal Override", minval=0.0, step=0.1, group=groupReversal)
//====================================================
// 04. SUPPORT AND RESISTANCE INPUTS
//====================================================
groupSR = "Support & Resistance"
showSR = input.bool(true, "Show Support & Resistance", group=groupSR)
pivotStrength = input.int(5, "Pivot Strength", minval=1, maxval=50, group=groupSR)
maxLevels = input.int(10, "Maximum Levels", minval=1, maxval=100, group=groupSR)
srLineWidth = input.int(1, "Line Width", minval=1, maxval=4, group=groupSR)
srStyleInput = input.string("Dashed", "Line Style", options=["Solid", "Dashed", "Dotted"], group=groupSR)
supportColor = input.color(color.new(color.lime, 25), "Support Color", group=groupSR)
resistanceColor = input.color(color.new(color.red, 25), "Resistance Color", group=groupSR)
//====================================================
// 05. VISUALIZATION
//====================================================
groupVisual = "Visualization"
showSignals = input.bool(true, "Show BUY / SELL Signals", group=groupVisual)
colorBars = input.bool(false, "Color Candles", group=groupVisual)
//====================================================
// 06. COLORS
//====================================================
groupColors = "Colors"
longColor = input.color(color.rgb(0, 200, 90), "BUY Color", group=groupColors)
shortColor = input.color(color.rgb(235, 50, 60), "SELL Color", group=groupColors)
//====================================================
// 07. EHLERS ENGINE
//====================================================
a2 = alpha * alpha
c0 = alpha - 0.25 * a2
c1 = 0.50 * a2
c2 = -(alpha - 0.75 * a2)
c3 = 2.0 * (1.0 - alpha)
c4 = -(1.0 - alpha) * (1.0 - alpha)
seed = (src + 2.0 * nz(src[1], src) + nz(src[2], src)) / 4.0
var float instantTrend = na
if bar_index < 7
instantTrend := seed
else
instantTrend := c0 * src + c1 * nz(src[1], src) + c2 * nz(src[2], src) + c3 * nz(instantTrend[1], seed) + c4 * nz(instantTrend[2], seed)
//====================================================
// 08. MOMENTUM
//====================================================
trigger = 2.0 * instantTrend - nz(instantTrend[2], instantTrend)
trendMomentum = trigger - instantTrend
bullishMomentum = trendMomentum > 0.0
bearishMomentum = trendMomentum < 0.0
//====================================================
// 09. RESIDUAL NOISE
//====================================================
residual = src - instantTrend
residualPower = ta.rma(residual * residual, noiseLength)
residualRMS = math.sqrt(math.max(nz(residualPower, residual * residual), 0.0))
//====================================================
// 10. ATR DEFENSE
//====================================================
atrValue = math.max(nz(ta.atr(atrLength), ta.tr(true)), syminfo.mintick)
noiseDistance = residualRMS * bandMultiplier
minimumDistance = atrValue * minimumATR
defenseDistance = math.max(noiseDistance, minimumDistance)
//====================================================
// 11. TREND BOUNDARIES
//====================================================
bullishBoundary = instantTrend - defenseDistance
bearishBoundary = instantTrend + defenseDistance
//====================================================
// 12. TREND REGIME AND TRAILING STOP
//====================================================
var int SQ = src >= instantTrend ? 1 : -1
var float trendTrail = na
previousSQ = nz(SQ[1], SQ)
initialTrail = previousSQ == 1 ? src - defenseDistance : src + defenseDistance
previousTrail = nz(trendTrail[1], initialTrail)
if previousSQ == 1
trendTrail := math.max(previousTrail, bullishBoundary)
else
trendTrail := math.min(previousTrail, bearishBoundary)
//====================================================
// 13. REVERSAL DETECTION
//====================================================
strongBearishBreak = src < instantTrend - atrValue * reversalStrength
strongBullishBreak = src > instantTrend + atrValue * reversalStrength
shortCondition = previousSQ == 1 and src < trendTrail and (bearishMomentum or strongBearishBreak)
longCondition = previousSQ == -1 and src > trendTrail and (bullishMomentum or strongBullishBreak)
//====================================================
// 14. CONFIRMED REVERSAL
//====================================================
long = longCondition and barstate.isconfirmed
short = shortCondition and barstate.isconfirmed
if long
SQ := 1
trendTrail := math.min(bullishBoundary, src - minimumDistance)
else if short
SQ := -1
trendTrail := math.max(bearishBoundary, src + minimumDistance)
else
SQ := previousSQ
//====================================================
// 15. UNIQUE BUY / SELL SIGNALS
//====================================================
buySignal = SQ == 1 and nz(SQ[1], SQ) != 1 and barstate.isconfirmed
sellSignal = SQ == -1 and nz(SQ[1], SQ) != -1 and barstate.isconfirmed
//====================================================
// 16. SUPPORT AND RESISTANCE ENGINE
//====================================================
pivotHigh = ta.pivothigh(high, pivotStrength, pivotStrength)
pivotLow = ta.pivotlow(low, pivotStrength, pivotStrength)
srLineStyle = srStyleInput == "Solid" ? line.style_solid : srStyleInput == "Dotted" ? line.style_dotted : line.style_dashed
var array<line> srLines = array.new<line>()
if showSR and barstate.isconfirmed
if not na(pivotHigh)
resistanceLine = line.new(
x1=bar_index - pivotStrength,
y1=pivotHigh,
x2=bar_index,
y2=pivotHigh,
xloc=xloc.bar_index,
extend=extend.right,
color=resistanceColor,
style=srLineStyle,
width=srLineWidth)
array.push(srLines, resistanceLine)
if not na(pivotLow)
supportLine = line.new(
x1=bar_index - pivotStrength,
y1=pivotLow,
x2=bar_index,
y2=pivotLow,
xloc=xloc.bar_index,
extend=extend.right,
color=supportColor,
style=srLineStyle,
width=srLineWidth)
array.push(srLines, supportLine)
while array.size(srLines) > maxLevels
oldLine = array.shift(srLines)
line.delete(oldLine)
//====================================================
// 17. COLORS
//====================================================
regimeColor = SQ == 1 ? longColor : shortColor
candleColor = colorBars ? regimeColor : na
//====================================================
// 18. BUY SIGNAL
//====================================================
plotshape(
showSignals and buySignal,
title="BUY Signal",
style=shape.triangleup,
location=location.belowbar,
color=longColor,
size=size.small,
text="BUY",
textcolor=color.white)
//====================================================
// 19. SELL SIGNAL
//====================================================
plotshape(
showSignals and sellSignal,
title="SELL Signal",
style=shape.triangledown,
location=location.abovebar,
color=shortColor,
size=size.small,
text="SELL",
textcolor=color.white)
//====================================================
// 20. CANDLE COLORS
//====================================================
barcolor(candleColor, title="Trend Candle Colors")
//====================================================
// 21. DATA WINDOW
//====================================================
plot(trendMomentum / atrValue, title="Normalized Ehlers Momentum", display=display.data_window)
plot(defenseDistance / atrValue, title="Defense Distance ATR", display=display.data_window)
plot(SQ, title="Trend Regime", display=display.data_window)
//====================================================
// 22. ALERT CONDITIONS
//====================================================
alertcondition(buySignal, title="BUY Alert", message="Vector Trend PRO BUY on {{ticker}}")
alertcondition(sellSignal, title="SELL Alert", message="Vector Trend PRO SELL on {{ticker}}")
alertcondition(buySignal or sellSignal, title="BUY / SELL Alert", message="Vector Trend PRO signal on {{ticker}}")
//====================================================
// END OF VECTOR TREND PRO
//====================================================