Reversal Points
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © enesyetkin

//@version=5
indicator('Reversal Points', overlay=true)

newHighSensitivity = input.int(defval=50, title='New Low Sensitivity', minval=1)

Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=2.0)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)


Supertrend(src, Multiplier, Periods,changeATR) =>

    atr2 = ta.sma(ta.tr, Periods)
    atr = changeATR ? ta.atr(Periods) : atr2
    up = src - Multiplier * atr
    up1 = nz(up[1], up)
    up := close[1] > up1 ? math.max(up, up1) : up
    dn = src + Multiplier * atr
    dn1 = nz(dn[1], dn)
    dn := close[1] < dn1 ? math.min(dn, dn1) : dn
    trend = 1
    trend := nz(trend[1], trend)
    trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend


    [trend, up,dn]

[trend, up,dn] = Supertrend(src, Multiplier, Periods,changeATR)




//upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
//dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1


var newHighCondition = 0

higher = ta.highest(newHighSensitivity)


if higher == higher[newHighSensitivity / 10]
    newHighCondition := 0
    newHighCondition
//    lowerLevel:=0.0
if higher != higher[newHighSensitivity / 10] and close > higher[newHighSensitivity / 10]
    newHighCondition := 1
    newHighCondition


HighColor = newHighCondition == 0 ? color.orange : na

topswitch = 0
topswitch :=  sellSignal ? 0 : (not newHighCondition == 0) and trend == 1  ? 1  : topswitch[1]

reversalsignal = topswitch[1] == 1 and topswitch == 0 ? true : false



var newLowCondition = 0

lower = ta.lowest(newHighSensitivity)


if lower == lower[newHighSensitivity / 10]
    newLowCondition := 0
    newLowCondition
//    lowerLevel:=0.0
if lower != lower[newHighSensitivity / 10] and close < lower[newHighSensitivity / 10]
    newLowCondition := 1
    newLowCondition


lowColor = newLowCondition == 0 ? color.orange : na

bottomswitch = 0
bottomswitch :=  buySignal ? 0 : (not newLowCondition == 0) and trend == -1  ? 1  : bottomswitch[1]

bottomreversalsignal = bottomswitch[1] == 1 and bottomswitch == 0 ? true : false


plotshape(reversalsignal, title='Sell', text='Sell', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))

plotshape(bottomreversalsignal, title='Buy', text='Buy', location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))

//plot(higher, color=HighColor)

