Dynamic Levels Breakouts [Angel Algo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// Angel Algo
//@version=5
indicator("Dynamic Levels Breakouts", overlay = true)

// Script inputs

length = input.int(defval=20, title="Period", minval=2, maxval=30) // 20 or 15


// Calculate maximum and minimum price in the rolling window

max_level = ta.highest(high, length)

min_level = ta.lowest(low, length)


// Define condtions for dynamic support and resistance levels detection and plotting

max_color_cond = (max_level[1]==max_level[2]) and (close[1]<=max_level[1]) 
   and (close[1]>=min_level[1])

min_color_cond = (min_level[1]==min_level[2]) and (close[1]<=max_level[1]) 
   and (close[1]>=min_level[1])


// Define conditional coloring for dynamic support and resistance levels

max_level_color = max_color_cond ? color.new(color.red,70):na

min_level_color = min_color_cond ? color.new(color.green,70):na


// Define bullish and bearish breakouts condition

bullish_breakout = ta.crossover (close, max_level[1]) 

bearish_breakout = ta.crossunder (close, min_level[1]) 


//Plot dynamic support and resistance levels

plot(max_level[1], color = max_level_color,linewidth=2)

plot(min_level[1], color = min_level_color,linewidth=2)


// Define a current market regime condition

not_bullish = ta.barssince(bullish_breakout[1])>ta.barssince(bearish_breakout[1])

not_bearish = ta.barssince(bullish_breakout[1])<ta.barssince(bearish_breakout[1])


// Plot dynamic support and resistance levels breakout signals

plotshape(bullish_breakout and not_bullish, text="▲", style=shape.labelup, 
   color=color.green, textcolor=color.white, location=location.belowbar, size=size.small)

plotshape(bearish_breakout and not_bearish, text="▼", style=shape.labeldown, 
   color=color.red, textcolor=color.white, location=location.abovebar, size=size.small)


