//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
indicator("Pymander's EZ Trend Alignment", shorttitle="EZ Trend Align", overlay=true)
// --- Inputs ---
cciLen = input.int(20, title="CCI Length", tooltip="Length for CCI calculation")
atrLen = input.int(5, title="ATR Length", tooltip="Length for ATR calculation")
multiplier = input.float(1.0, title="ATR Multiplier")
originalColoring = input.bool(true, title="Original Coloring")
// --- Style ---
greenCol = input.color(#089981, title="Bullish Color", group="Style")
redCol = input.color(#f23645, title="Bearish Color", group="Style")
// --- Core Logic ---
thisCCI = ta.cci(close, cciLen)
lastCCI = nz(thisCCI[1])
atrSma = ta.sma(ta.tr, atrLen)
float bufferDn = high + multiplier * atrSma
float bufferUp = low - multiplier * atrSma
// Trend flip alignment
if thisCCI >= 0 and lastCCI < 0
bufferUp := bufferDn[1]
if thisCCI <= 0 and lastCCI > 0
bufferDn := bufferUp[1]
// Trailing stop logic
if thisCCI >= 0
if bufferUp < bufferUp[1]
bufferUp := bufferUp[1]
else if thisCCI <= 0
if bufferDn > bufferDn[1]
bufferDn := bufferDn[1]
// Output Line
float x = na
x := thisCCI >= 0 ? bufferUp : thisCCI <= 0 ? bufferDn : nz(x[1])
// Colors
int swap = na
swap := x > nz(x[1]) ? 1 : x < nz(x[1]) ? -1 : nz(swap[1], 1)
swapColor2 = swap == 1 ? greenCol : redCol
swapColor3 = thisCCI >= 0 ? greenCol : redCol
plotColor = originalColoring ? swapColor3 : swapColor2
// --- Premium Visuals ---
p_line = plot(x, title="Trend Line", color=plotColor, linewidth=3, style=plot.style_linebr)
p_price = plot(hl2, title="Price Mid", display=display.none)
// Premium gradient fill for trailing stop aesthetic
fill(p_line, p_price, x, hl2, color.new(plotColor, 50), color.new(plotColor, 100), title="Trend Fill")