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

//@version=5
indicator(title = 'OHLC Break' , overlay = true , max_boxes_count = 500 , max_labels_count = 500 , max_bars_back = 5000 , max_lines_count = 500)

max_tf   = input.int(defval = 60 , title = 'Max TF Show' , inline = '0' , group = 'Input')
max_line = input.int(defval = 500 , title = 'Max Bars Back', inline = '0' , group = 'Input')

Timezone = 'America/New_York' 
DOM = (timeframe.multiplier <= max_tf) and (timeframe.isintraday)
newDay = ta.change(dayofweek)

//-----------------
// Function 
//-----------------
//Vertical Lines
vline(Start1, Color, linestyle, LineWidth) =>
    line.new(x1 = Start1 , y1 = low - ta.tr , x2 = Start1 , y2 = high + ta.tr , xloc = xloc.bar_time , extend = extend.both , color = Color , style = linestyle , width = LineWidth)

// Session String to int
SeshStartHour(Session) =>
    math.round(str.tonumber(str.substring(Session,0,2)))
SeshStartMins(Session) =>
    math.round(str.tonumber(str.substring(Session,2,4)))
SeshEndHour(Session) =>
    math.round(str.tonumber(str.substring(Session,5,7)))
SeshEndMins(Session) =>
    math.round(str.tonumber(str.substring(Session,7,9)))

// BgInSession
BarInSession(sess) =>
    time(timeframe.period, sess, Timezone) != 0
//-----------------
// End Function 
//-----------------

//---Input OHLC
show_OHLC  = input.bool(defval = false , title = 'Show OHLC Break     ' , inline = '0' , group = 'OHLC Break') 
candle_box = input.bool(defval = true  , title = 'Change color Candel OHLC', inline = '0' , group = 'OHLC Break') 
col_candle_box = input.color(defval = color.white , title = '' , inline = '0' , group = 'OHLC Break')

show_line = input.bool(defval = false , title = 'Show Line no Break    ', inline = '1' , group = 'OHLC Break')
show_lab  = input.bool(defval = false , title = 'Show Label' , inline = '1' , group = 'OHLC Break' , tooltip = 'Shows a Label on candles that respect the minimum of broken levels') 

show_box  = input.bool(defval = false , title = 'Show Box on the candle that broke at least', inline = '3' , group = 'OHLC Break') 
max_line_break = input.int(defval = 5 , title = '' , inline = '3' , group = 'OHLC Break' , tooltip = 'Choose the minimum of broken levels')

style_box = input.string(defval = '⎯⎯⎯' , title = '' , options = ['⎯⎯⎯' ,'----' , '····'] , inline = '4' , group = 'OHLC Break')
choise_style_box = style_box == '····' ? line.style_dotted : style_box == '----' ? line.style_dashed : line.style_solid
color_box = input.color(defval = color.maroon , title = '' , inline = '4' , group = 'OHLC Break')

size_box  = input.int(defval = 1 , title = 'Size' , inline = '4' , group = 'OHLC Break')
max_Box   = input.int(defval = 5 , title = 'Tot. Box' , inline = '4' , group = 'OHLC Break' , tooltip = 'Max Box Show')


show_lv = show_line ? color.black : na
linebx  = choise_style_box
colobx  = color_box

var line[] _lowLiqLines  = array.new_line()
var line[] _highLiqLines  = array.new_line()
var box[] boxArray = array.new_box()
//Function to Calculate Line Length
_controlLine(_lines, __high, __low) =>
    if array.size(_lines) > 0
        var bool isLineDashed = false
        var int count = 0
        var bool changeCandle = na

        for i = array.size(_lines) - 1 to 0 by 1
            _line = array.get(_lines, i)
            _lineLow = line.get_y1(_line)
            _lineHigh = line.get_y2(_line)
            _lineRight = line.get_x2(_line)
            
            if na or (bar_index == _lineRight and not((__high > _lineLow and __low < _lineLow) or (__high > _lineHigh and __low < _lineHigh)))
                line.set_x2(_line, bar_index + 1)
                line.set_extend(_line, extend.right)

            if _lineRight > bar_index[5] and _lineRight < bar_index[0] 
                line.set_color(_line, color.red)
                line.set_style(_line, line.style_dashed)

            if _lineRight < bar_index
                line.delete(_line)

            var bool isPreviousBarRight = false    
            for j = bar_index[0] - 1 to bar_index[1] + 1 by 1
                prevBarIndex = j
                
                if prevBarIndex == _lineRight
                    isPreviousBarRight := true
                break

            // Increase count if line is dashed
            if isPreviousBarRight and _lineRight < bar_index
                isLineDashed := true
                count += 1 

        // Add the label only once after the loop completes
        if count >= max_line_break 
            changeCandle := true
            if show_lab
                a = label.new(x = bar_index[1] , y = high[1] , text = str.tostring(count), color = color.rgb(255, 255, 255, 78), textcolor = color.black, style = label.style_label_down)
            if show_box
                bx = box.new(left = bar_index[1] , top = high[1] , right = bar_index , bottom = low[1] , border_color = colobx , border_width = size_box , border_style = linebx , extend = extend.right , bgcolor = na )
                if  array.size(boxArray) >= max_Box
                    box.delete(array.shift(boxArray))
                array.push(boxArray, bx)
        else
            changeCandle := false

        if barstate.isconfirmed and isLineDashed
            count := 0
        changeCandle

//Pivot Low Line Plotting 
var bool changeCandle_1 = na
var bool changeCandle_2 = na
if show_OHLC
    _lowPVT = line.new(x1 = bar_index - 1 , y1 = low[1] , x2 = bar_index , y2 = low[1] , extend = extend.none , color = show_lv, style = line.style_solid)
    if array.size(_lowLiqLines) >= max_line
        line.delete(array.shift(_lowLiqLines))
    array.push(_lowLiqLines, _lowPVT)

    //Pivot High Line Plotting
    _highPVT = line.new(x1 = bar_index - 1 , y1 = high[1] , x2 = bar_index , y2 = high[1] , extend = extend.none , color = show_lv, style = line.style_solid)
    if array.size(_highLiqLines) >= max_line
        line.delete(array.shift(_highLiqLines))
    array.push(_highLiqLines, _highPVT)

    changeCandle_1 := _controlLine(_lowLiqLines, high, low) 
    changeCandle_2 := _controlLine(_highLiqLines, high, low)
    
barcolor(candle_box and changeCandle_1 ? col_candle_box : na , offset = -1)
barcolor(candle_box and changeCandle_2 ? col_candle_box : na , offset = -1)

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
// Session 
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

show_BR_Sess = input.bool(defval = false , title = 'Session' , inline = 'Show'  , group = 'Session')
Show_OND     = input.bool(defval = true , title = 'Show Only Today' , inline = 'Show'  , group = 'Session')

show_Sess_1 = input.bool(defval = false , title = '' , inline = '1'  , group = 'Session')
Sess_1      = input.session(defval = '0200-0500' , title = '' , inline = '1'  , group = 'Session')
col_Sess_1  = input.color(defval = color.blue , title = '' , inline = '1'  , group = 'Session')

show_Sess_2 = input.bool(defval = false , title = '' , inline = '2'  , group = 'Session')
Sess_2      = input.session(defval = '0830-1100' , title = '' , inline = '2'  , group = 'Session')
col_Sess_2  = input.color(defval = color.red , title = '' , inline = '2'  , group = 'Session')

show_Sess_3 = input.bool(defval = false , title = '' , inline = '3'  , group = 'Session')
Sess_3      = input.session(defval = '0900-1200' , title = '' , inline = '3'  , group = 'Session')
col_Sess_3  = input.color(defval = color.yellow , title = '' , inline = '3'  , group = 'Session')

show_Sess_4 = input.bool(defval = false , title = '' , inline = '4'  , group = 'Session')
Sess_4      = input.session(defval = '1330-1600' , title = '' , inline = '4'  , group = 'Session')
col_Sess_4  = input.color(defval = color.purple , title = '' , inline = '4'  , group = 'Session')

// Time Periods
Sess_Start_Time_1 = timestamp(Timezone, year, month, dayofmonth, SeshStartHour(Sess_1), SeshStartMins(Sess_1), 00)
Sess_End_Time_1   = timestamp(Timezone, year, month, dayofmonth, SeshEndHour(Sess_1), SeshEndMins(Sess_1), 00)
//
Sess_Start_Time_2 = timestamp(Timezone, year, month, dayofmonth, SeshStartHour(Sess_2), SeshStartMins(Sess_2), 00)
Sess_End_Time_2   = timestamp(Timezone, year, month, dayofmonth, SeshEndHour(Sess_2), SeshEndMins(Sess_2), 00)
//
Sess_Start_Time_3 = timestamp(Timezone, year, month, dayofmonth, SeshStartHour(Sess_3), SeshStartMins(Sess_3), 00)
Sess_End_Time_3   = timestamp(Timezone, year, month, dayofmonth, SeshEndHour(Sess_3), SeshEndMins(Sess_3), 00)
// 
Sess_Start_Time_4 = timestamp(Timezone, year, month, dayofmonth, SeshStartHour(Sess_4), SeshStartMins(Sess_4), 00)
Sess_End_Time_4   = timestamp(Timezone, year, month, dayofmonth, SeshEndHour(Sess_4), SeshEndMins(Sess_4), 00)

// Creating Variables
var Sess_Start_Vline_1 = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_End_Vline_1   = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_Fill_1        = linefill.new(Sess_Start_Vline_1, Sess_End_Vline_1, color.new(#787b86, 90))
//
var Sess_Start_Vline_2 = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_End_Vline_2   = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_Fill_2        = linefill.new(Sess_Start_Vline_2, Sess_End_Vline_2, color.new(#787b86, 90))
//
var Sess_Start_Vline_3 = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_End_Vline_3   = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_Fill_3        = linefill.new(Sess_Start_Vline_3, Sess_End_Vline_3, color.new(#787b86, 90))
//
var Sess_Start_Vline_4 = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_End_Vline_4   = line.new(x1 = na ,  y1 = na ,  x2 = na  ,  xloc = xloc.bar_time ,  y2 = close ,  color = color.rgb(255,255,255,100) , width = 1)
var Sess_Fill_4        = linefill.new(Sess_Start_Vline_4, Sess_End_Vline_4, color.new(#787b86, 90))

// When a New Day Starts, Start Drawing all lines
if Show_OND and newDay and dayofweek != dayofweek.sunday

    if (show_BR_Sess and show_Sess_1 and DOM)
        Sess_Start_Vline_1 := vline(Sess_Start_Time_1 , color.rgb(255,255,255,100) , line.style_solid , 1)
        Sess_End_Vline_1   := vline(Sess_End_Time_1 , color.rgb(255,255,255,100)  , line.style_solid , 1)
        Sess_Fill_1 := linefill.new(Sess_Start_Vline_1, Sess_End_Vline_1, color.new(col_Sess_1, 90))
        line.delete(Sess_Start_Vline_1[1])
        line.delete(Sess_End_Vline_1[1])
        linefill.delete(Sess_Fill_1[1])

    if (show_BR_Sess and show_Sess_2 and DOM)
        Sess_Start_Vline_2 := vline(Sess_Start_Time_2 , color.rgb(255,255,255,100) , line.style_solid , 1)
        Sess_End_Vline_2   := vline(Sess_End_Time_2 , color.rgb(255,255,255,100)  , line.style_solid , 1)
        Sess_Fill_2 := linefill.new(Sess_Start_Vline_2, Sess_End_Vline_2, color.new(col_Sess_2, 90))
        line.delete(Sess_Start_Vline_2[1])
        line.delete(Sess_End_Vline_2[1])
        linefill.delete(Sess_Fill_2[1])

    if (show_BR_Sess and show_Sess_3 and DOM)
        Sess_Start_Vline_3 := vline(Sess_Start_Time_3 , color.rgb(255,255,255,100) , line.style_solid , 1)
        Sess_End_Vline_3   := vline(Sess_End_Time_3 , color.rgb(255,255,255,100)  , line.style_solid , 1)
        Sess_Fill_3 := linefill.new(Sess_Start_Vline_3, Sess_End_Vline_3, color.new(col_Sess_3, 90))
        line.delete(Sess_Start_Vline_3[1])
        line.delete(Sess_End_Vline_3[1])
        linefill.delete(Sess_Fill_3[1])    

    if (show_BR_Sess and show_Sess_4 and DOM)
        Sess_Start_Vline_4 := vline(Sess_Start_Time_4 , color.rgb(255,255,255,100) , line.style_solid , 1)
        Sess_End_Vline_4   := vline(Sess_End_Time_4 , color.rgb(255,255,255,100)  , line.style_solid , 1)
        Sess_Fill_4 := linefill.new(Sess_Start_Vline_4, Sess_End_Vline_4, color.new(col_Sess_4, 90))
        line.delete(Sess_Start_Vline_4[1])
        line.delete(Sess_End_Vline_4[1])
        linefill.delete(Sess_Fill_4[1])


chartColour1 = not Show_OND and DOM and show_BR_Sess and show_Sess_1 and BarInSession(Sess_1) ? color.new(col_Sess_1 , 90) : na
bgcolor(color = chartColour1 , title = 'Session 1' , transp = 95 )
chartColour2 = not Show_OND and DOM and show_BR_Sess and show_Sess_2 and BarInSession(Sess_2) ? color.new(col_Sess_2 , 90) : na
bgcolor(color = chartColour2 , title = 'Session 2' , transp = 95)
chartColour3 = not Show_OND and DOM and show_BR_Sess and show_Sess_3 and BarInSession(Sess_3) ? color.new(col_Sess_3 , 90) : na
bgcolor(color = chartColour3 , title = 'Session 3' , transp = 95)
chartColour4 = not Show_OND and DOM and show_BR_Sess and show_Sess_4 and BarInSession(Sess_4) ? color.new(col_Sess_4 , 90) : na
bgcolor(color = chartColour4 , title = 'Session 3' , transp = 95)