ATR Trailing Stop v5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 5
// Author = TradeAutomation


indicator(title="ATR Trailing Stop v5", shorttitle="ATR Trailing Stop", overlay=true, timeframe="", timeframe_gaps=false)


ATRPeriod = input.int(12, "ATR Period", tooltip = "This is the number of bars back that the script uses to calculate the Average True Range.")
ATRMultiplier = input.float(3, "ATR Multiplier", step=.1, tooltip = "This is the multiple of the ATR average that will function as the trail.")

ATR = ta.atr(ATRPeriod)
Stop = ATRMultiplier*ATR

var ATRTrailingStop = 0.0
ATRTrailingStop := if close>ATRTrailingStop[1] and close[1]>ATRTrailingStop[1]
    math.max(ATRTrailingStop[1], close-Stop)
else if close<ATRTrailingStop[1] and close[1]<ATRTrailingStop[1]
    math.min(ATRTrailingStop[1], close+Stop)
else if close>ATRTrailingStop[1]
    close-Stop 
else 
    close+Stop

var Position = 0.0
Position := if close[1]<ATRTrailingStop[1] and close>ATRTrailingStop[1]
    1
else if close[1]>ATRTrailingStop[1] and close<ATRTrailingStop[1]
    -1
else
    Position[1]

PlotColor = Position == -1 ? color.red: Position == 1 ? color.green : color.navy
plot(ATRTrailingStop, color=PlotColor, linewidth=input(1, "Line Width"), title="ATR Trailing Stop")
