OST - Overlapped Super Trend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yosimadsu
// Modified original code of KivancOzbilgic's Supertrend

//@version=5
indicator("OST - Overlapped Super Trend", "OST", overlay=true, timeframe="", timeframe_gaps=true)

useHA = input.bool(false, "Use Heiken Ashi")

CLOSE = close
OPEN = open
HIGH = high
LOW = low

CLOSE := useHA ? (OPEN + CLOSE + HIGH + LOW) / 4 : CLOSE
OPEN := useHA ? na(OPEN[1]) ? (OPEN + CLOSE) / 2: (OPEN[1] + CLOSE[1]) / 2 : OPEN
HIGH := useHA ? math.max(HIGH, math.max(OPEN, CLOSE)) : HIGH
LOW := useHA ? math.min(LOW, math.min(OPEN, CLOSE)) : LOW
    
A_Periods = input(2, 'ATR Period')
A_Multiplier = input.float(2, 'ATR Multiplier', step=0.1)
A_atr = ta.atr(A_Periods)

// BUY
A_Bup = LOW - A_Multiplier * A_atr
A_Bup1 = nz(A_Bup[1], A_Bup)
A_Bup := CLOSE[1] > A_Bup1 ? math.max(A_Bup, A_Bup1) : A_Bup
A_Bdn = LOW + A_Multiplier * A_atr
A_Bdn1 = nz(A_Bdn[1], A_Bdn)
A_Bdn := CLOSE[1] < A_Bdn1 ? math.min(A_Bdn, A_Bdn1) : A_Bdn

A_trendBuy = 1
A_trendBuy := nz(A_trendBuy[1], A_trendBuy)
A_trendBuy := A_trendBuy == -1 and CLOSE > A_Bdn1 ? 1 : A_trendBuy == 1 and CLOSE < A_Bup1 ? -1 : A_trendBuy

A_upPlot = plot(A_trendBuy == 1 ? A_Bup : na, title='Up Trend', style=plot.style_linebr, linewidth=1, color=color.new(color.green, 0))
A_buySignal = A_trendBuy == 1 and A_trendBuy[1] == -1
plotshape(A_buySignal ? A_Bup : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.aqua, 0))

// SEL
A_Sup = HIGH - A_Multiplier * A_atr
A_Sup1 = nz(A_Sup[1], A_Sup)
A_Sup := CLOSE[1] > A_Sup1 ? math.max(A_Sup, A_Sup1) : A_Sup
A_Sdn = HIGH + A_Multiplier * A_atr
A_Sdn1 = nz(A_Sdn[1], A_Sdn)
A_Sdn := CLOSE[1] < A_Sdn1 ? math.min(A_Sdn, A_Sdn1) : A_Sdn

A_trendSel = 1
A_trendSel := nz(A_trendSel[1], A_trendSel)
A_trendSel := A_trendSel == -1 and CLOSE > A_Sdn1 ? 1 : A_trendSel == 1 and CLOSE < A_Sup1 ? -1 : A_trendSel

A_dnPlot = plot(A_trendSel == 1 ? na : A_Sdn, title='Down Trend', style=plot.style_linebr, linewidth=1, color=color.new(color.red, 0))
A_sellSignal = A_trendSel == -1 and A_trendSel[1] == 1
plotshape(A_sellSignal ? A_Sdn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.fuchsia, 0))