ATR + Trailing Stops

SimpleCryptoLife · study · 80 行 · 点赞 2,765 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// @version=4
// Based off of script by Bluephantom by @RickSanchex
// Extended by @paul108 to include trailing option and made compatible with PineScript v4
study(title="ATR + Trailing Stops", shorttitle="ATR Trail", overlay=true, linktoseries=true)
 
// ============================== GET INPUTS ==================================
 
atrLookback = input(defval=14,title="ATR Lookback Period",type=input.integer)
multiplier = input(defval=3,title="ATR Multiplier",type=input.float, step=0.1, minval=0.5, maxval=4)
trailMode = input(title="Trail Mode", defval="Trailing", options=["Running", "Trailing"])
flipInput = input(title="Flip Trail on:", defval="Close", options=["Close","Wick"]) 
 
 
// ============================== CALCULATE ATR ==================================
 
atrValue = atr(atrLookback)
atrMultiplied = atrValue * multiplier
 
// plot the price plus or minus the ATR
atrLow = low - atrMultiplied
atrHigh = high + atrMultiplied
 
 
// ============================== CALCULATE TRAILING ATR ==================================
 
// Calculate both the low and high trailing ATRs every time. The low one never goes down, and the high one never goes up
// Set them to something to start with
trailAtrLow = atrLow
trailAtrHigh = atrHigh
 
// If the ATR Low has gone up AND it has gone above the trail, the low trailing ATR should also go up. If the ATR Low has gone up or down, but not below the trail, the ATR trail stays where it is
trailAtrLow := na(trailAtrLow[1]) ? trailAtrLow : atrLow >= trailAtrLow[1] ? atrLow : trailAtrLow[1]
// Same for the High
trailAtrHigh := na(trailAtrHigh[1]) ? trailAtrHigh : atrHigh <= trailAtrHigh[1] ? atrHigh : trailAtrHigh[1]
 
 
// ============================== FLIP WHEN PRICE CROSSES ==================================
 
// We start off long and short, but at the end of the first bar, and until the first flip, we are neither long nor short
isLong = true
isShort = true
 
// Flip based on candle close or High/Low
flipSourceLong = flipInput == "Close" ? close : flipInput == "Wick" ? high : na
flipSourceShort = flipInput == "Close" ? close : flipInput == "Wick" ? low : na
 
// We flip long if the Close/High is above the trailing ATR High and the previous was not.
isLong := flipSourceLong > trailAtrHigh and flipSourceLong[1] <= trailAtrHigh[1] ? true : false
 
// We flip short if the Close/Low is below the trailing ATR Low and the previous one was not.
isShort := flipSourceShort < trailAtrLow and flipSourceShort[1] >= trailAtrLow[1] ? true : false
 
//We stay long if the previous bar was long and we have not met conditions to flip short this time
isLong := isLong[1] and not isShort ? true : isLong
 
//We stay short if the previous bar was short and we have not met conditions to flip long this time
isShort := isShort[1] and not isLong ? true : isShort
 
 
// ============================== RESET LINES AND MAKE INACTIVE ONE INVISIBLE ==================================
 
// When we flip long, reset the long stop, i.e. the trailAtrLow.
trailAtrLow := isLong and not isLong[1] ? atrLow : trailAtrLow
// When we flip short, reset the short stop, i.e. the trailAtrHigh
trailAtrHigh := isShort and not isShort[1] ? atrHigh : trailAtrHigh
 
// If we are long, we calculate both trails but only plot the Low. If we are short, we only plot the High. 
// If we are neither long nor short (i.e. in the beginning) we plot both.
trailAtrLowPlot = isLong ? trailAtrLow : not isLong and not isShort ? trailAtrLow : na
trailAtrHighPlot = isShort ? trailAtrHigh : not isLong and not isShort ? trailAtrHigh: na
 
 
// ============================== PLOT LINES ==================================
 
// If we are in Running mode, plot the price plus and minus the ATR
plot(trailMode == "Running" ? atrLow : na, "Running ATR Low", color=color.white, linewidth=1, transp=60)
plot(trailMode == "Running" ? atrHigh : na,"Running ATR High", color=color.white, linewidth=1, transp=60)
 
plot(trailMode == "Trailing" ? trailAtrLowPlot : na, "Trailing ATR Long Stop", color=color.red, style=plot.style_linebr, linewidth=1)
plot(trailMode == "Trailing" ? trailAtrHighPlot : na, "Trailing ATR Short Stop", color=color.red, style=plot.style_linebr, linewidth=1)

← 返回列表