本页源码来自 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/
// © Mr_Rakun
//@version=6
indicator("Dynamic Adaptive SuperTrend [Mr_Rakun]", overlay=true, format=format.price, precision=2)
// --- INPUTS ---
grp1 = "Dynamic Multiplier Range"
atrPeriod = input.int(14, "ATR Period", minval=1, group=grp1)
trendMult = input.float(2.0, "Trend Multiplier (Min Bound)", minval=0.5, step=0.1, group=grp1, tooltip="The lower multiplier limit reached when the trend is at maximum intensity (100% strength).")
flatMult = input.float(6.0, "Ranging Multiplier (Max Bound)", minval=1.0, step=0.1, group=grp1, tooltip="The upper multiplier limit reached when the market is completely flat/sideways (0% strength).")
grp2 = "Trend Strength Measurement Settings"
measureLen = input.int(10, "Measurement Period", minval=2, group=grp2, tooltip="Number of bars used to measure the magnitude and directional efficiency of price movement.")
maxAtrMove = input.float(4.0, "Max Breakout Reference (x ATR)", minval=1.5, step=0.5, group=grp2, tooltip="How many multiples of ATR in net distance over the period should be considered 100% trend strength? (Lower values cause the multiplier to drop toward 2.0 faster).")
smoothLen = input.int(3, "Multiplier Smoothing (EMA)", minval=1, group=grp2, tooltip="Smooths the dynamic multiplier for fluid bar-to-bar transitions (e.g., 2.3 -> 2.8 -> 3.4) instead of abrupt jumps.")
usePivotMid = input.bool(true, "Use Donchian/Pivot Midpoint Source", group=grp2)
showTable = input.bool(true, "Show Live Multiplier Table", group=grp2)
// --- 1. MEASURING DIRECTION AND MAGNITUDE TOGETHER ---
atrVal = ta.atr(atrPeriod)
// Net distance traveled (How far did price move compared to N bars ago?)
netMove = math.abs(close - close[measureLen])
// Total path traveled (In sideways/choppy markets, the path lengthens while netMove remains small)
totalPath = math.sum(math.abs(close - close[1]), measureLen)
// Efficiency Ratio / Linearity (0 to 1): How cleanly is price moving without whipsawing?
er = totalPath != 0 ? netMove / totalPath : 0.0
// Magnitude: Net distance expressed as a multiple of ATR
moveInAtr = atrVal != 0 ? netMove / atrVal : 0.0
// --- 2. TREND STRENGTH RATIO (0.00 = Flat/Ranging, 1.00 = Max Trend) ---
// Combines both the ATR-normalized movement magnitude and directional efficiency (ER)
rawStrength = (moveInAtr / maxAtrMove) * er
// Clamps the ratio between 0 and 1 (0% to 100%) while keeping intermediate values completely continuous (e.g., 0.12, 0.45, 0.88)
trendStrength = math.min(1.0, math.max(0.0, rawStrength))
// --- 3. CONTINUOUS MULTIPLIER SCALING BETWEEN 2.0 AND 6.0 ---
// Returns flatMult (6.0) when strength is 0, trendMult (2.0) when strength is 1, and scales proportionally in between (e.g., 2.3, 4.15, 5.6)
rawDynamicMult = flatMult - (trendStrength * (flatMult - trendMult))
// Light smoothing for fluid bar-to-bar multiplier transitions
dynamicMult = ta.ema(rawDynamicMult, smoothLen)
// --- 4. SUPERTREND CALCULATION (Non-Repainting) ---
src = usePivotMid ? math.avg(ta.highest(high, measureLen), ta.lowest(low, measureLen)) : hl2
upBand = src - (dynamicMult * atrVal)
dnBand = src + (dynamicMult * atrVal)
var float trailingUp = na
var float trailingDn = na
var int dir = 1
prevUp = nz(trailingUp[1], upBand)
prevDn = nz(trailingDn[1], dnBand)
trailingUp := close[1] > prevUp ? math.max(upBand, prevUp) : upBand
trailingDn := close[1] < prevDn ? math.min(dnBand, prevDn) : dnBand
if dir == -1 and close > prevDn
dir := 1
else if dir == 1 and close < prevUp
dir := -1
superTrend = dir == 1 ? trailingUp : trailingDn
// --- 5. PLOTTING AND SIGNALS ---
trendColor = dir == 1 ? color.new(#00e676, 0) : color.new(#ff5252, 0)
upTrend = plot(dir < 0 ? superTrend : na, "Up Trend", color = trendColor, style = plot.style_linebr)
downTrend = plot(dir < 0 ? na : superTrend, "Down Trend", color = trendColor, style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle", display = display.none)
fill(bodyMiddle, upTrend, title = "Uptrend background", color = color.new(trendColor, 80), fillgaps = false)
fill(bodyMiddle, downTrend, title = "Downtrend background", color = color.new(trendColor, 80), fillgaps = false)
buySignal = dir == 1 and dir[1] == -1 and barstate.isconfirmed
sellSignal = dir == -1 and dir[1] == 1 and barstate.isconfirmed
plotshape(buySignal ? superTrend : na, "Buy Signal", shape.labelup, location.absolute, color.new(#00e676, 0), text="Buy", textcolor=color.black, size=size.small)
plotshape(sellSignal ? superTrend : na, "Sell Signal", shape.labeldown, location.absolute, color.new(#ff5252, 0), text="Sell", textcolor=color.white, size=size.small)
// --- 6. LIVE MULTIPLIER TRACKING TABLE ---
var table infoTable = table.new(position.top_right, 2, 3, bgcolor=color.new(#1e222d, 10), border_color=color.new(#363c4e, 0), border_width=1)
if barstate.islast and showTable
table.cell(infoTable, 0, 0, "Multiplier Range:", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(infoTable, 1, 0, str.tostring(trendMult, "#.#") + " - " + str.tostring(flatMult, "#.#"), text_color=color.white, text_size=size.small)
table.cell(infoTable, 0, 1, "Trend Strength:", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(infoTable, 1, 1, str.tostring(trendStrength * 100, "#.0") + "%", text_color=trendColor, text_size=size.small)
table.cell(infoTable, 0, 2, "Active Multiplier:", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
table.cell(infoTable, 1, 2, str.tostring(dynamicMult, "#.##"), text_color=color.yellow, text_size=size.normal)