本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Uncle_the_shooter
//@version=6
indicator("SuperTrend Weighted by Divergence", overlay=true, max_lines_count=500, max_labels_count=500)
// INPUT SETTINGS
atrLen = input.int(10, "ATR Length", minval=1)
baseFactor = input.float(3.0, "Base ATR Multiplier", minval=0.1, step=0.1)
pivotLength = input.int(2, "Pivot Length (Left/Right)", minval=1)
divSensitivity = input.float(
0.3,
"Divergence Sensitivity",
minval=0.0,
maxval=1.0,
step=0.01,
tooltip="0.0 = No divergence influence\n1.0 = Maximum ATR multiplier reduction (strongest effect)"
)
colorBarsEnabled = input.bool(true, "Color bars by trend", group="Color Settings")
// COLOR SETTINGS
upColor = input.color(color.rgb(6, 162, 47), "Uptrend Color (Bullish)")
downColor = input.color(color.rgb(207, 23, 23), "Downtrend Color (Bearish)")
// FIXED PARAMETERS
confirmBars = 1
len2 = 6
smoothLen2 = 7
calculateDivergence = true
showLine = true
fillShade = true
divBars = 200
// MPO4 OSCILLATOR FUNCTION
calcCPO(_len, _smooth) =>
body = math.abs(close - open)
avgBody = ta.sma(body, _len)
direction = close > open ? 1.0 : close < open ? -1.0 : 0.0
weight = avgBody != 0 ? body / avgBody : 1.0
contrib = direction * weight
rolling = 0.0
for i = 0 to _len - 1
rolling += contrib[i]
norm = rolling / (_len * 2) * 100
ta.ema(norm, _smooth)
osc = calcCPO(len2, smoothLen2)
// DIVERGENCE CALCULATION
lookbackRight = pivotLength
lookbackLeft = pivotLength
pivotHighOsc = ta.pivothigh(osc, lookbackLeft, lookbackRight)
pivotLowOsc = ta.pivotlow(osc, lookbackLeft, lookbackRight)
var bool bullCond = false
var bool bearCond = false
oscLbr = osc[lookbackRight]
if calculateDivergence
plFound = not na(pivotLowOsc)
oscHL = oscLbr > ta.valuewhen(plFound, oscLbr, 1)
priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1)
bullCond := priceLL and oscHL and plFound
phFound = not na(pivotHighOsc)
oscLH = oscLbr < ta.valuewhen(phFound, oscLbr, 1)
priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1)
bearCond := priceHH and oscLH and phFound
bullDiv = bullCond
bearDiv = bearCond
// ATR AND SUPERTREND BASE
atr = ta.atr(atrLen)
hl2 = (high + low) / 2
var float trailLong = na
var float trailShort = na
var int regime = 0
trailLong := na(trailLong) ? hl2 : trailLong
trailShort := na(trailShort) ? hl2 : trailShort
var int bullCount = 0
var int bearCount = 0
bullCount := close > trailShort ? bullCount + 1 : 0
bearCount := close < trailLong ? bearCount + 1 : 0
// TREND FLIP LOGIC
nextRegime = regime
if regime == 0
if bullCount >= confirmBars
nextRegime := 1
else if bearCount >= confirmBars
nextRegime := -1
else if regime == 1 and bearCount >= confirmBars
nextRegime := -1
else if regime == -1 and bullCount >= confirmBars
nextRegime := 1
flipHappens = nextRegime != regime
// DIVERGENCE IMPACT LOGIC
var int divAge = 9999
var int divType = 0
if bullDiv
divAge := 0
divType := 1
else if bearDiv
divAge := 0
divType := -1
else if flipHappens
divAge := 9999
divType := 0
else
divAge += 1
divActive = divAge <= divBars and divType == -nextRegime
factorAdjusted = baseFactor
if divActive and not flipHappens
factorAdjusted := baseFactor * (1 - divSensitivity)
// SUPERTREND BANDS AND TRAILS
bandTop = hl2 + factorAdjusted * atr
bandBot = hl2 - factorAdjusted * atr
if nextRegime == 1
trailLong := math.max(bandBot, nz(trailLong[1], bandBot))
trailShort := bandTop
else if nextRegime == -1
trailShort := math.min(bandTop, nz(trailShort[1], bandTop))
trailLong := bandBot
else
trailLong := bandBot
trailShort := bandTop
regime := nextRegime
// VISUALIZATION
plotLine = flipHappens ? na : (regime == 1 ? trailLong : trailShort)
pLine = plot(plotLine, color = showLine ? color.new(regime == 1 ? upColor : downColor, 40) : na, linewidth = 2, style = plot.style_linebr)
pCenter = plot(hl2, display = display.none)
fill(
pLine,
pCenter,
plotLine,
hl2,
fillShade ? color.new(regime == 1 ? upColor : downColor, 75) : na,
fillShade ? color.new(regime == 1 ? upColor : downColor, 95) : na
)
// BUY / SELL LABELS
if flipHappens
label.new(
bar_index,
regime == 1 ? low - atr * 0.5 : high + atr * 0.5,
regime == 1 ? "Buy" : "Sell",
color = color.new(regime == 1 ? upColor : downColor, 40),
style = regime == 1 ? label.style_label_up : label.style_label_down,
textcolor = color.white,
size = size.small
)
// BAR COLORING – teraz zależne od nowego inputu
barcolor(colorBarsEnabled ? (regime == 1 ? upColor : downColor) : na)
// DIVERGENCE MARKERS
plotshape(bullDiv, title="Bullish Divergence", location=location.belowbar, color=upColor, style=shape.circle, size=size.tiny)
plotshape(bearDiv, title="Bearish Divergence", location=location.abovebar, color=downColor, style=shape.circle, size=size.tiny)
// ALERTS
alertcondition(flipHappens and regime == 1, title="Buy Signal", message="SuperTrend Buy Signal")
alertcondition(flipHappens and regime == -1, title="Sell Signal", message="SuperTrend Sell Signal")
alertcondition(bullDiv, title="Bullish Divergence", message="Bullish Divergence Detected")
alertcondition(bearDiv, title="Bearish Divergence", message="Bearish Divergence Detected")