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

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

look_back = input.int(defval = 20, title = "Candle Lookback")
confirm_in = input.int(defval = 3, title = "Confirm Within")

bull_candle = 0
bear_candle = 0

v_up = false
v_dn = false

var bull_active_candle      = false
var bull_active_candle_low  = 0.0
var bull_active_candle_high = 0.0
var bull_signal_active      = false
var bull_candle_count       = 0

var bear_active_candle      = false
var bear_active_candle_low  = 0.0
var bear_active_candle_high = 0.0
var bear_signal_active      = false
var bear_candle_count       = 0

for i = 0 to (look_back - 1)
    if close < low[i]
        bull_candle := bull_candle + 1
    if close > high[i]
        bear_candle := bear_candle + 1

if bull_candle              == (look_back - 1)
    bull_active_candle      := true
    bull_active_candle_low  := low
    bull_active_candle_high := high
    bull_signal_active      := false
    bull_candle_count       := 0

if bull_active_candle == true
    bull_candle_count := bull_candle_count + 1

if bear_candle              == (look_back - 1)
    bear_active_candle      := true
    bear_active_candle_low  := low
    bear_active_candle_high := high
    bear_signal_active      := false
    bear_candle_count       := 0

if bear_active_candle == true
    bear_candle_count := bear_candle_count + 1

if close < bull_active_candle_low
    bull_active_candle := false

if close > bear_active_candle_high
    bear_active_candle := false

if bull_active_candle  == true and close > bull_active_candle_high and bull_signal_active == false and bull_candle_count <= (confirm_in+1) and barstate.isconfirmed
    bull_signal_active := true
    v_up := true

if bear_active_candle  == true and close < bear_active_candle_low and bear_signal_active == false and bear_candle_count <= (confirm_in+1) and barstate.isconfirmed
    bear_signal_active := true
    v_dn := true

plotshape(v_up, title = "V Up", style = shape.triangleup, location = location.belowbar, color = color.green, size = size.small)
plotshape(v_dn, title = "V Dn", style = shape.triangledown, location = location.abovebar, color = color.red, size = size.small)

alertcondition(v_up, "Bullish Revarsal", "Bullish Revarsal")
alertcondition(v_dn, "Bearish Revarsal", "Bearish Revarsal")






