本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Designed with passion by Lucas Vivien based on an original idea from Neville Batty.
// V1 = v66
//@version=5
indicator("Custom ATR Trailing Stop", "ATR TSL", overlay=true)
// USER IPUTS
g_0 = "Trade Setup"
barTime = input.time (timestamp("01 Jan 2024 12:00 +0000"), group=g_0, confirm=true, title="Bar Time")
tradeType = input.string("Long", title="Trade Direction" , group=g_0, confirm=true, options=["Long", "Short"])
longSource = input.source(close , title="Long signal source" , group=g_0, confirm=true)
shortSource = input.source(open , title="Short signal source" , group=g_0, confirm=true)
g_1 = "Average True Range"
atrPeriods = input.int (14 , title="ATR Lookback" , group=g_1, confirm=true)
atrSmaPeriods = input.int (14 , title="ATR average Lookback" , group=g_1, confirm=true, tooltip="Simple moving average applied to the Average True Range")
initialATRMultiplier = input.float(3.0, title="Initial ATR Multiplier" , group=g_1, confirm=true)
breakevenMultiplier = input.float(5.0, title="Breakeven ATR Multiplier", group=g_1, confirm=true)
manualBreakeven = input.float(0.0, title="Manual Breakeven Level" , group=g_1, confirm=true, tooltip="- Expressed in Quote Currency.\n- If equal to zero (default value), breakeven price is the close price of the selected candle.")
// ATR CALCULATIONS
atr = ta.atr(atrPeriods)
avgATR = ta.sma(atr, atrSmaPeriods)
initialStopATR = avgATR * initialATRMultiplier
breakevenStopATR = avgATR * breakevenMultiplier
// SIGNAL VALIDATION
validLong = barTime == time and tradeType == "Long" // validLong = ---> PASTE YOUR LONG SIGNAL HERE <---
validShort = barTime == time and tradeType == "Short" // validShort = ---> PASTE YOUR SHORT SIGNAL HERE <---
// Trailing stop variables
var bool inLongTrade = false
var bool inShortTrade = false
var float longEntryPrice = na
var float shortEntryPrice = na
var float longStop = na
var float shortStop = na
if validLong
longStop := longSource - initialStopATR
inLongTrade := true
inShortTrade := false
longEntryPrice := close
alert("Long Signal", alert.freq_once_per_bar_close)
if validShort
shortStop := shortSource + initialStopATR
inShortTrade := true
inLongTrade := false
shortEntryPrice := close
alert("Short Signal", alert.freq_once_per_bar_close)
if inLongTrade
if close > longStop
if close < (manualBreakeven == 0.0 ? longEntryPrice : manualBreakeven)
longStop := math.max(longStop, close - initialStopATR)
if close >= (manualBreakeven == 0.0 ? longEntryPrice : manualBreakeven)
if initialATRMultiplier <= breakevenMultiplier
longStop := math.max(longStop, close - breakevenStopATR)
if initialATRMultiplier > breakevenMultiplier
longStop := math.max(longStop, close - initialStopATR)
if low <= longStop
inLongTrade := false
alert("Long Trailing Stop Hit", alert.freq_once_per_bar)
if inShortTrade
if close < shortStop
if close > (manualBreakeven == 0.0 ? shortEntryPrice : manualBreakeven)
shortStop := math.min(shortStop, close + initialStopATR)
if close <= (manualBreakeven == 0.0 ? shortEntryPrice : manualBreakeven)
if initialATRMultiplier <= breakevenMultiplier
shortStop := math.min(shortStop, close + breakevenStopATR)
if initialATRMultiplier > breakevenMultiplier
shortStop := math.min(shortStop, close + initialStopATR)
if high >= shortStop
inShortTrade := false
alert("Short Trailing Stop Hit", alert.freq_once_per_bar)
// Plotting trailing stops
plot(inLongTrade ? longStop : na, color= longStop > longEntryPrice ? #00FF00 : #FF0000, title="Long Stop" , linewidth=2, style=plot.style_linebr)
plot(inShortTrade ? shortStop : na, color= shortStop < shortEntryPrice ? #00FF00 : #FF0000, title="Short Stop", linewidth=2, style=plot.style_linebr)