ATR Stop Loss v4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dratzlogic

//@version=5
indicator("ATR Stop Loss v4.1", overlay=true, shorttitle="ATR SL v4.1")

// Inputs
atrLength = input(14, title="ATR Length")
stopMultiplier = input(1.5, title="ATR Stop Multiplier")
BoxPlacement = input.string(position.top_right, "Box Placement", options=[position.bottom_right, position.bottom_left, position.top_right, position.top_left, position.top_center, position.bottom_center])
plotATR = input(true, title="Plot ATR Stop?")
plotTrailing = input(true, title="Plot Trailing Stop?")
plotLong = input(true, title="Plot Long Stop?")
plotShort = input(true, title="Plot Short Stop?")

// Colors
text_color = input(color.white, "Text Color")
atr_bg_color = input(color.blue, "ATR Row Background Color")
trailing_bg_color = input(color.purple, "Trailing Row Background Color")
long_bg_color = input(color.green, "Long Row Background Color")
short_bg_color = input(color.red, "Short Row Background Color")

// Calculate ATR and stop values
atrValue = ta.atr(atrLength)
longStop = close - atrValue * stopMultiplier
shortStop = close + atrValue * stopMultiplier
trailingStop = atrValue * stopMultiplier

// Prepare a table
var table myTable = table.new(BoxPlacement, 1, 4)

if barstate.islast
    // Populate table
    table.cell(myTable, 0, 0, text="ATR: " + str.tostring(atrValue, "#.##"), text_color=text_color, bgcolor=atr_bg_color)
    table.cell(myTable, 0, 1, text="Trailing: " + str.tostring(trailingStop, "#.##"), text_color=text_color, bgcolor=trailing_bg_color)
    table.cell(myTable, 0, 2, text="Long: " + str.tostring(longStop, "#.##"), text_color=text_color, bgcolor=long_bg_color)
    table.cell(myTable, 0, 3, text="Short: " + str.tostring(shortStop, "#.##"), text_color=text_color, bgcolor=short_bg_color)

// Plotting stop lines
plot(plotATR ? atrValue : na, title="ATR Stop", color=color.blue)
plot(plotTrailing ? trailingStop : na, title="Trailing Stop", color=color.purple)
plot(plotLong ? longStop : na, title="Long Stop", color=color.green)
plot(plotShort ? shortStop : na, title="Short Stop", color=color.red)
