本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('ATR Trailing Stop', overlay = true)
lookbackBars = input.int(5, 'Lookback Bars for Top', minval = 1)
atrLength = input.int(14, 'ATR Length', minval = 1)
nearThresholdPct = input.float(1, 'Near Stop Threshold (%)', step = 0.1)
showStopLine = input.bool(true, 'Show Stop Line', tooltip = 'Display the trailing stop line on chart')
showLabel = input.bool(true, 'Show Stop Label', tooltip = 'Display stop level label on price scale')
labelColor = input.color(color.red, 'Label Color', tooltip = 'Color for the stop level label')
// Dynamic ATR Multiplier based on volatility regimes
lowVolThreshold = input.float(2.5, 'Low Volatility Threshold (%)', step = 0.1, tooltip = 'ATR% below this = Low volatility')
midVolThreshold = input.float(7.0, 'Mid Volatility Threshold (%)', step = 0.1, tooltip = 'ATR% below this = Medium volatility')
multLow = input.float(5.0, 'Low Vol Multiplier', step = 0.1, tooltip = 'Wider stop for low volatility (slower trail)')
multMid = input.float(3.0, 'Mid Vol Multiplier', step = 0.1, tooltip = 'Moderate stop for mid volatility')
multHigh = input.float(1.5, 'High Vol Multiplier', step = 0.1, tooltip = 'Tighter stop for high volatility (faster trail)')
atrValue = ta.atr(atrLength)
atrPercent = atrValue / close * 100
// Determine volatility regime and set multiplier
var string volRegime = 'MID'
float atrMultiplier = multMid
if atrPercent < lowVolThreshold
volRegime := 'LOW'
atrMultiplier := multLow
atrMultiplier
else if atrPercent < midVolThreshold
volRegime := 'MID'
atrMultiplier := multMid
atrMultiplier
else
volRegime := 'HIGH'
atrMultiplier := multHigh
atrMultiplier
//----------------------
// State variables (continuous trailing, no reset)
//----------------------
var float top = na
var float trailingStop = na
// rolling lookback-bar high
windowHigh = ta.highest(high, lookbackBars)
if na(top)
top := windowHigh
top
else
top := math.max(top[1], windowHigh) // top never down
top
stopCandidate = top - atrMultiplier * atrValue
if na(trailingStop)
trailingStop := stopCandidate
trailingStop
else // Trailing stop can only move UP, never down (locks in profits)
trailingStop := math.max(trailingStop[1], stopCandidate)
trailingStop
// Detect stop hit
stopHit = close <= trailingStop
// Reset logic: if price drops significantly below stop, reset and start fresh
resetThreshold = 0.05 // 5% below stop triggers reset
if close < trailingStop * (1 - resetThreshold)
top := windowHigh
trailingStop := top - atrMultiplier * atrValue
trailingStop
//----------------------
// Alerts & plots
//----------------------
priceAboveStop = close > trailingStop
distancePct = (close - trailingStop) / trailingStop * 100.0
nearStop = priceAboveStop and distancePct <= nearThresholdPct
stopHitAlert = stopHit
plot(showStopLine ? trailingStop : na, title = 'ATR Trailing Stop', color = color.red, linewidth = 2)
alertcondition(nearStop, 'Trailing Stop - Near', 'Price is close to the ATR trailing stop (long).')
alertcondition(stopHitAlert, 'Trailing Stop - Hit', 'Price has HIT the ATR trailing stop (long).')
var label slLabel = na
if showLabel and barstate.islast and not na(trailingStop)
// delete previous label
if not na(slLabel)
label.delete(slLabel)
// create new label 1 bar to the right of the last bar
string labelText = str.tostring(trailingStop, '#.##') + '\n' + volRegime + ' Vol (x' + str.tostring(atrMultiplier, '#.#') + ')' + '\nATR: ' + str.tostring(atrPercent, '#.##') + '%'
slLabel := label.new(x = bar_index + 1, y = trailingStop, text = labelText, style = label.style_label_left, color = color.new(labelColor, 60), textcolor = color.white, size = size.small)
slLabel