//@version=5
indicator("1.5 ATR From High and -1.5 ATR From Low", shorttitle = "TSL", overlay=true)
// Input for ATR length
atrLength = input.int(21, title="ATR Length")
// Calculate the ATR
atrValue = ta.atr(atrLength)
// Variables to store the last higher low, last lower high, and their corresponding ATR lines
var float lastHigherLow = na
var float lastLowerHigh = na
var float atrLineLow = na
var float atrLineHigh = na
// Detect higher low and lower high
if (low > low[1] and (na(lastHigherLow) or low > lastHigherLow))
lastHigherLow := low
atrLineLow := lastHigherLow - 1.5 * atrValue
if (high < high[1] and (na(lastLowerHigh) or high < lastLowerHigh)) lastLowerHigh := high atrLineHigh := lastLowerHigh + 1.5 * atrValue // Recalculate the lines if they're broken by a bar if (low < atrLineLow) lastHigherLow := low atrLineLow := lastHigherLow - 1.5 * atrValue if (high > atrLineHigh)
lastLowerHigh := high
atrLineHigh := lastLowerHigh + 1.5 * atrValue
// Plot the -1.5 ATR from the low when a higher low occurs
plot(na(atrLineLow) ? na : atrLineLow, color=color.red, linewidth=2, title="-1.5 ATR From Low")
// Plot the 1.5 ATR from the high when a lower high occurs
plot(na(atrLineHigh) ? na : atrLineHigh, color=color.blue, linewidth=2, title="1.5 ATR From High")