Support/Resistance With Breaks & Bounces [JacobMagleby]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ExoMaven

//@version=5
indicator("Support/Resistance With Breaks & Bounces[ExoMaven]", shorttitle = "Support/Resistance With Breaks & Bounces [V1]", overlay = true, max_lines_count = 500)


//user input
point_method       = input.string(title = "Pivot Method", defval = "Candle Wicks", options = ["Candle Wicks", "Candle Body"], group = "Main Settings")
left_bars          = input.int(title = "Sensitivity", defval = 5, minval = 2, inline = "Pivot Length", group = "Main Settings")
right_bars         = left_bars//input.int(title = "Rightbars", defval = 5, minval = 2, inline = "Pivot Length", group = "Main Settings")
line_width         = input.int(title = "Line Width", defval = 3, group = "Style Settings")


//global variables/arrays
high_source        = point_method == "Candle Body" ? (close > open ? close : open) : high
low_source         = point_method == "Candle Body" ? (close < open ? close : open) : low

fixed_pivot_high   = fixnan(ta.pivothigh(high_source, left_bars, right_bars))
fixed_pivot_low    = fixnan(ta.pivotlow(low_source, left_bars, right_bars))

green_candle       = close > open
red_candle         = close < open

green_engulf       = green_candle and red_candle[1] and close > open[1]
red_engulf         = red_candle and green_candle[1] and close < open[1]

var red_lines      = array.new_line()
var red_breakout   = array.new_int()
var red_bounce     = array.new_int()

var green_lines    = array.new_line()
var green_breakout = array.new_int()
var green_bounce   = array.new_int()

green_breakout_trigger     = false
green_rejection_trigger    = false
red_breakout_trigger       = false
red_rejection_trigger      = false


//main operation
if barstate.isconfirmed
    if array.size(red_lines) > 0
        for i = array.size(red_lines) - 1 to 0
            line.set_x2(array.get(red_lines, i), bar_index)
            
            if close > line.get_y1(array.get(red_lines, i)) and close[1] < line.get_y1(array.get(red_lines, i)) and array.get(red_breakout, i) == 0
                red_breakout_trigger := true
                array.set(red_breakout, i, 1)
            else
                if high >= line.get_y1(array.get(red_lines, i)) and close < line.get_y1(array.get(red_lines, i)) and red_engulf and array.get(red_bounce, i) == 0 and array.get(red_breakout, i) == 0
                    red_rejection_trigger := true
                    array.set(red_bounce, i, 1)
    
    if array.size(green_lines) > 0
        for i = array.size(green_lines) - 1 to 0
            line.set_x2(array.get(green_lines, i), bar_index)
            
            if close < line.get_y1(array.get(green_lines, i)) and close[1] > line.get_y1(array.get(green_lines, i)) and array.get(green_breakout, i) == 0
                green_breakout_trigger := true
                array.set(green_breakout, i, 1)
            else
                if low <= line.get_y1(array.get(green_lines, i)) and close > line.get_y1(array.get(green_lines, i)) and green_engulf and array.get(green_bounce, i) == 0 and array.get(green_breakout, i) == 0
                    green_rejection_trigger := true
                    array.set(green_bounce, i, 1)


    if fixed_pivot_high != fixed_pivot_high[1]
        if array.size(red_lines) > 0
            line.set_x2(array.get(red_lines, 0), bar_index - right_bars)
            array.clear(red_lines)
            array.clear(red_breakout)
            array.clear(red_bounce)
        array.unshift(red_lines, line.new(x1 = bar_index - right_bars, y1 = fixed_pivot_high, x2 = bar_index, y2 = fixed_pivot_high, xloc = xloc.bar_index, color = color.red, width = line_width))
        array.unshift(red_breakout, 0)
        array.unshift(red_bounce, 0)
        
    if fixed_pivot_low != fixed_pivot_low[1]
        if array.size(green_lines) > 0
            line.set_x2(array.get(green_lines, 0), bar_index - right_bars)
            array.clear(green_lines)
            array.clear(green_breakout)
            array.clear(green_bounce)
        array.unshift(green_lines, line.new(x1 = bar_index - right_bars, y1 = fixed_pivot_low, x2 = bar_index, y2 = fixed_pivot_low, xloc = xloc.bar_index, color = color.green, width = line_width))
        array.unshift(green_breakout, 0)
        array.unshift(green_bounce, 0)

        
//shapes
plotshape(series = red_breakout_trigger, title = "Red Breakout", style = shape.xcross, location = location.belowbar, color = color.blue, text = "Break", textcolor = color.red, size = size.tiny, show_last = 20000)
plotshape(series = red_rejection_trigger, title = "Red Rejection", style = shape.xcross, location = location.abovebar, color = color.blue, text = "Bounce", textcolor = color.red, size = size.tiny, show_last = 20000)

plotshape(series = green_breakout_trigger, title = "Green Breakout", style = shape.xcross, location = location.abovebar, color = color.blue, text = "Break", textcolor = color.green, size = size.tiny, show_last = 20000)
plotshape(series = green_rejection_trigger, title = "Green Rejection", style = shape.xcross, location = location.belowbar, color = color.blue, text = "Bounce", textcolor = color.green, size = size.tiny, show_last = 20000)


//alertconditions
alertcondition(condition = red_breakout_trigger, title = "Red Breakout")
alertcondition(condition = red_rejection_trigger, title = "Red Rejection/Bounce")
alertcondition(condition = green_breakout_trigger, title = "Green Breakout")
alertcondition(condition = green_rejection_trigger, title = "Green Rejection/Bounce")

        


        
        


