Automatic Order Block + Imbalance by D. Brigaglia
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dadoesploso

//@version=4

study(title="Automatic Order Block + Imbalance", overlay=true)

lenght = input(title="Rect Lenght", type=input.integer, defval=350)

//bullish OB

if close > (high[3] + 3.5 * atr(14)) and open[3] > close[3]
    
    box.new(left=bar_index[3], top=high[3], right=bar_index + lenght, bottom=low[3], 
	 border_color=color.blue, border_width=3, bgcolor=na)

//bearish OB

if close < (low[3] - 3.5 * atr(14)) and open[3] < close[3]
    
    box.new(left=bar_index[3], top=high[3], right=bar_index + lenght, bottom=low[3], 
	 border_color=color.red, border_width=3, bgcolor=na)



//buy side imbalance

if (high[2] < low[0]) and (close[1] - open[1] > 3 * atr(14))
    
    box.new(left=bar_index[2], top=low[0], right=bar_index + lenght, bottom=high[2], 
	 border_color=color.green, border_width=2, bgcolor=na)
	 


//sell side imbalance

if (low[2] > high[0]) and (open[1] - close[1] > 3 * atr(14))
    
    box.new(left=bar_index[2], top=low[2], right=bar_index + lenght, bottom=high[0], 
	 border_color=color.orange, border_width=2, bgcolor=na)