Initial Balance |ASE|
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AseAlgo

//@version=5
indicator("Initial Balance |ASE|", "IB |ASE|", overlay = true)
updatethis = "      "

showIB = input(true, 'Show Initial Balance', group = "Main IB Settings", inline="1")
showFill = input(true, 'Fill IB Range', group = "Main IB Settings", inline="2")
extendlvls  = input(false, "Extend IB Level's", group = "Main IB Settings", inline="2")
showExtension_1 = input(true, "Show 1x Extensions", group = "Main IB Settings", inline="3")
showExtension_2 = input(true, "Show 2x Extensions", group = "Main IB Settings", inline="3")
showExtension_3 = input(true, "Show 3x Extensions", group = "Main IB Settings", inline="4")
showExtension_4 = input(true, "Show 4x Extensions", group = "Main IB Settings", inline="4")
showMidlevels = input(false, "Show Middle Levels", group = "Main IB Settings")
deletePrevDay = input(false, "Delete Previous Day's Levels", group = "Main IB Settings")
session = input.session("0930-1030", "Session", group = "Main IB Settings")
hoursOffsetInput = input.int(-4, "Timezone offset (in UTC)", minval = -12, maxval = 14, step = 1)
IBHColor = input.color(color.green, "IB High Color", inline="1", group = "Line Settings")
IBHEColor = input.color(color.green, "Extension Lvls Color",inline="1", group = "Line Settings" )
IBLColor = input.color(color.red, "IB Low Color", inline="2", group = "Line Settings")
IBLEColor = input.color(color.red, "Extension Lvls Color", inline="2", group = "Line Settings")
IBFillColor = input.color(color.orange, "IB Fill Color", inline="3", group = "Line Settings")
IBMidColor = input.color(color.orange, "IB Mid Level Colors", inline="3", group = "Line Settings")  
MS_IN_1H = 1000 * 60 * 60
msOffsetInput = hoursOffsetInput * MS_IN_1H                         
inSession = time(timeframe.period, session, hoursOffsetInput >= 0 ? "UTC+" + str.tostring(hoursOffsetInput) : "UTC" + str.tostring(hoursOffsetInput)) //"America/New_York"

int IBLineWidth = input.int(1, "IBH/IBL Line Width", group = "Line Settings")
int EXTLineWidth = input.int(1, "Ext. Line Width", group = "Line Settings")
int MIDLineWidth = input.int(1, "Mid Line Width", group = "Line Settings")
string ext_level_style = input.string("••••••••", "Mid Line Style", options=["••••••••","- - - - - -","Solid"], group = "Line Settings")


mainColor = input.color(color.gray, "Text Color", group = "General Settings")


var float IB_High = 0
var float IB_Low = 0
var float IB_Ext_Val = 0

var line IBh = na
var line IBl = na
var line IBhextend = na
var line IBlextend = na
var line IBm = na
var line IBh_5 = na
var line IBh_10 = na
var line IBl_5 = na
var line IBl_10 = na
var line IBh_15 = na
var line IBh_20 = na
var line IBl_15 = na 
var line IBl_20 = na 

var line IBh_25 = na
var line IBh_30 = na
var line IBl_25 = na 
var line IBl_30 = na 
var line IBh_35 = na
var line IBh_40 = na
var line IBl_35 = na 
var line IBl_40 = na 

var linefill IB_Fill = na
var label IBh_label = na
var label IBl_label = na
var label IBh_10_label = na
var label IBl_10_label = na
var label IBh_20_label = na
var label IBl_20_label = na

var label IBh_30_label = na
var label IBl_30_label = na
var label IBh_40_label = na
var label IBl_40_label = na

var mls = ext_level_style == "••••••••" ? line.style_dotted : ext_level_style == "- - - - - -" ? line.style_dashed : line.style_solid

//Set session High & Low
if not inSession[1] and inSession
    IB_High := 0
    IB_Low := 0
    IB_Ext_Val := 0

if inSession and timeframe.in_seconds(timeframe.period) <= timeframe.in_seconds('30')
    if high > IB_High
        IB_High := high
    if IB_Low == 0
        IB_Low := low
    else if low < IB_Low
        IB_Low := low
    IB_Ext_Val := (IB_High - IB_Low)
    if not inSession[1] and inSession and showIB
        IBh := line.new(time, IB_High, time, IB_High, style = line.style_dotted, width= IBLineWidth, color=IBHColor, xloc=xloc.bar_time)
        IBh_label := label.new(time, IB_High,"IBH - " + str.tostring(IB_High),xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
        IBl := line.new(time, IB_Low, time, IB_Low, style = line.style_dotted, width= IBLineWidth, color=IBLColor, xloc=xloc.bar_time)
        IBl_label := label.new(time, IB_Low, "IBL - " + str.tostring(IB_Low),xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor =mainColor)
        IBm := line.new(time, IB_High-(IB_Ext_Val/2), time, IB_High-(IB_Ext_Val/2), style = line.style_dotted, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
        if showExtension_1
            IBh_10 := line.new(time, IB_High + IB_Ext_Val, time, IB_High + IB_Ext_Val, style=line.style_dotted, width= EXTLineWidth , color=IBHEColor, xloc=xloc.bar_time)
            IBh_10_label := label.new(time, IB_High + IB_Ext_Val,"1x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            IBl_10 := line.new(time, IB_Low - IB_Ext_Val, time, IB_Low - IB_Ext_Val, style=line.style_dotted, width= EXTLineWidth, color=IBLEColor, xloc=xloc.bar_time)
            IBl_10_label := label.new(time, IB_Low - IB_Ext_Val,"1x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            if showMidlevels
                IBh_5 := line.new(time, IB_High + (IB_Ext_Val/2), time, IB_High + (IB_Ext_Val/2), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
                IBl_5 := line.new(time, IB_Low - (IB_Ext_Val/2), time, IB_Low - (IB_Ext_Val/2), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
        if showExtension_2
            IBh_20 := line.new(time, IB_High + 2*(IB_Ext_Val), time, IB_High + 2*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBHEColor, xloc=xloc.bar_time)
            IBh_20_label := label.new(time, IB_High + 2*(IB_Ext_Val),"2x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            IBl_20 := line.new(time, IB_Low - 2*(IB_Ext_Val), time, IB_Low - 2*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBLEColor, xloc=xloc.bar_time)
            IBl_20_label := label.new(time, IB_Low - 2*(IB_Ext_Val),"2x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            if showMidlevels
                IBh_15 := line.new(time, IB_High + 1.5*(IB_Ext_Val), time, IB_High + 1.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
                IBl_15 := line.new(time, IB_Low - 1.5*(IB_Ext_Val), time, IB_Low - 1.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
        if showExtension_3
            IBh_30 := line.new(time, IB_High + 3*(IB_Ext_Val), time, IB_High + 3*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBHEColor, xloc=xloc.bar_time)
            IBh_30_label := label.new(time, IB_High + 3*(IB_Ext_Val),"3x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            IBl_30 := line.new(time, IB_Low - 3*(IB_Ext_Val), time, IB_Low - 3*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBLEColor, xloc=xloc.bar_time)
            IBl_30_label := label.new(time, IB_Low - 3*(IB_Ext_Val),"3x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            if showMidlevels
                IBh_25 := line.new(time, IB_High + 2.5*(IB_Ext_Val), time, IB_High + 2.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
                IBl_25 := line.new(time, IB_Low - 2.5*(IB_Ext_Val), time, IB_Low - 2.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
        if showExtension_4
            IBh_40 := line.new(time, IB_High + 4*(IB_Ext_Val), time, IB_High + 4*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBHEColor, xloc=xloc.bar_time)
            IBh_40_label := label.new(time, IB_High + 4*(IB_Ext_Val),"4x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            IBl_40 := line.new(time, IB_Low - 4*(IB_Ext_Val), time, IB_Low - 4*(IB_Ext_Val), style=line.style_dotted, width= EXTLineWidth, color=IBLEColor, xloc=xloc.bar_time)
            IBl_40_label := label.new(time, IB_Low - 4*(IB_Ext_Val),"4x",xloc = xloc.bar_time, style = label.style_none, size = size.small, textcolor = mainColor)
            if showMidlevels
                IBh_35 := line.new(time, IB_High + 3.5*(IB_Ext_Val), time, IB_High + 3.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)
                IBl_35 := line.new(time, IB_Low - 3.5*(IB_Ext_Val), time, IB_Low - 3.5*(IB_Ext_Val), style =mls, width= MIDLineWidth, color=IBMidColor, xloc=xloc.bar_time)


            
    else if showIB 
        line.set_x2(IBh, time)
        line.set_y1(IBh, IB_High)
        line.set_y2(IBh, IB_High)
        line.set_x2(IBl, time)
        line.set_y1(IBl, IB_Low)
        line.set_y2(IBl, IB_Low)
        line.set_x2(IBm, time)
        line.set_y1(IBm, IB_High-(IB_Ext_Val/2))
        line.set_y2(IBm, IB_High-(IB_Ext_Val/2))

        label.set_y(IBh_label,IB_High)
        label.set_y(IBl_label,IB_Low)
        label.set_text(IBh_label, "IBH - " + str.tostring(IB_High))
        label.set_text(IBl_label, "IBL - " + str.tostring(IB_Low))
        if showExtension_1
            line.set_x2(IBh_10, time)
            line.set_y1(IBh_10, IB_High + IB_Ext_Val)
            line.set_y2(IBh_10, IB_High + IB_Ext_Val)
            line.set_x2(IBl_10, time)
            line.set_y1(IBl_10, IB_Low - IB_Ext_Val)
            line.set_y2(IBl_10, IB_Low - IB_Ext_Val)

            label.set_y(IBh_10_label,IB_High + IB_Ext_Val)
            label.set_y(IBl_10_label,IB_Low - IB_Ext_Val)
        if showMidlevels and showExtension_1
            line.set_x2(IBh_5, time)
            line.set_y1(IBh_5, IB_High + (IB_Ext_Val/2))
            line.set_y2(IBh_5, IB_High + (IB_Ext_Val/2))
            line.set_x2(IBl_5, time)
            line.set_y1(IBl_5, IB_Low - (IB_Ext_Val/2))
            line.set_y2(IBl_5, IB_Low - (IB_Ext_Val/2))
        if showExtension_2
            line.set_x2(IBh_20, time)
            line.set_y1(IBh_20, IB_High + 2*(IB_Ext_Val))
            line.set_y2(IBh_20, IB_High + 2*(IB_Ext_Val))
            line.set_x2(IBl_20, time)
            line.set_y1(IBl_20, IB_Low - 2*(IB_Ext_Val))
            line.set_y2(IBl_20, IB_Low - 2*(IB_Ext_Val))

            label.set_y(IBh_20_label,IB_High + 2*(IB_Ext_Val))
            label.set_y(IBl_20_label,IB_Low - 2*(IB_Ext_Val))
        if showMidlevels and showExtension_2
            line.set_x2(IBh_15, time)
            line.set_y1(IBh_15, IB_High + 1.5*(IB_Ext_Val))
            line.set_y2(IBh_15, IB_High + 1.5*(IB_Ext_Val))
            line.set_x2(IBl_15, time)
            line.set_y1(IBl_15, IB_Low - 1.5*(IB_Ext_Val))
            line.set_y2(IBl_15, IB_Low - 1.5*(IB_Ext_Val))
        if showExtension_3
            line.set_x2(IBh_30, time)
            line.set_y1(IBh_30, IB_High + 3*(IB_Ext_Val))
            line.set_y2(IBh_30, IB_High + 3*(IB_Ext_Val))
            line.set_x2(IBl_30, time)
            line.set_y1(IBl_30, IB_Low - 3*(IB_Ext_Val))
            line.set_y2(IBl_30, IB_Low - 3*(IB_Ext_Val))

            label.set_y(IBh_30_label,IB_High + 3*(IB_Ext_Val))
            label.set_y(IBl_30_label,IB_Low - 3*(IB_Ext_Val))
        if showMidlevels and showExtension_3
            line.set_x2(IBh_25, time)
            line.set_y1(IBh_25, IB_High + 2.5*(IB_Ext_Val))
            line.set_y2(IBh_25, IB_High + 2.5*(IB_Ext_Val))
            line.set_x2(IBl_25, time)
            line.set_y1(IBl_25, IB_Low - 2.5*(IB_Ext_Val))
            line.set_y2(IBl_25, IB_Low - 2.5*(IB_Ext_Val))
        if showExtension_4
            line.set_x2(IBh_40, time)
            line.set_y1(IBh_40, IB_High + 4*(IB_Ext_Val))
            line.set_y2(IBh_40, IB_High + 4*(IB_Ext_Val))
            line.set_x2(IBl_40, time)
            line.set_y1(IBl_40, IB_Low - 4*(IB_Ext_Val))
            line.set_y2(IBl_40, IB_Low - 4*(IB_Ext_Val))

            label.set_y(IBh_40_label,IB_High + 4*(IB_Ext_Val))
            label.set_y(IBl_40_label,IB_Low - 4*(IB_Ext_Val))
        if showMidlevels and showExtension_4
            line.set_x2(IBh_35, time)
            line.set_y1(IBh_35, IB_High + 3.5*(IB_Ext_Val))
            line.set_y2(IBh_35, IB_High + 3.5*(IB_Ext_Val))
            line.set_x2(IBl_35, time)
            line.set_y1(IBl_35, IB_Low - 3.5*(IB_Ext_Val))
            line.set_y2(IBl_35, IB_Low - 3.5*(IB_Ext_Val))




// after IB Close

if inSession[1] and not inSession and showIB

    line.set_style(IBh, line.style_solid)
    line.set_style(IBl, line.style_solid)
    line.set_style(IBh_10, line.style_solid)
    line.set_style(IBl_10, line.style_solid)
    line.set_style(IBh_20, line.style_solid)
    line.set_style(IBl_20, line.style_solid)
    line.set_style(IBh_30, line.style_solid)
    line.set_style(IBl_30, line.style_solid)
    line.set_style(IBh_40, line.style_solid)
    line.set_style(IBl_40, line.style_solid)

    if showFill
        IB_Fill := linefill.new(IBh,IBl,color.new(IBFillColor, 85))

    //Extension Level extend and set solid
    if extendlvls
        line.set_x2(IBm, time_close("D"))
        line.set_x2(IBh_5, time_close("D"))
        line.set_x2(IBl_5, time_close("D"))
        line.set_x2(IBh_10, time_close("D"))
        line.set_x2(IBl_10, time_close("D"))

        line.set_x2(IBh_15, time_close("D"))
        line.set_x2(IBl_15, time_close("D"))
        line.set_x2(IBh_20, time_close("D"))
        line.set_x2(IBl_20, time_close("D"))

        line.set_x2(IBh_25, time_close("D"))
        line.set_x2(IBl_25, time_close("D"))
        line.set_x2(IBh_30, time_close("D"))
        line.set_x2(IBl_30, time_close("D"))

        line.set_x2(IBh_35, time_close("D"))
        line.set_x2(IBl_35, time_close("D"))
        line.set_x2(IBh_40, time_close("D"))
        line.set_x2(IBl_40, time_close("D"))

        IBhextend := line.new(time, IB_High, time_close("D"), IB_High, style = line.style_solid, width= IBLineWidth, color=IBHColor, xloc=xloc.bar_time)
        IBlextend := line.new(time, IB_Low, time_close("D"), IB_Low, style = line.style_solid, width= IBLineWidth, color=IBLColor, xloc=xloc.bar_time)

if deletePrevDay and ta.change(time("D"))
    line.delete(IBh)
    line.delete(IBl)
    if extendlvls
        line.delete(IBhextend)
        line.delete(IBlextend) 
    line.delete(IBm)
    line.delete(IBh_5)
    line.delete(IBh_10)
    line.delete(IBl_5)
    line.delete(IBl_10)
    line.delete(IBh_15)
    line.delete(IBh_20)
    line.delete(IBl_15)
    line.delete(IBl_20)

    line.delete(IBh_25)
    line.delete(IBh_30)
    line.delete(IBl_25)
    line.delete(IBl_30)
    line.delete(IBh_35)
    line.delete(IBh_40)
    line.delete(IBl_35)
    line.delete(IBl_40)

    label.delete(IBh_label)
    label.delete(IBl_label)
    label.delete(IBh_10_label)
    label.delete(IBl_10_label)
    label.delete(IBh_20_label)
    label.delete(IBl_20_label)
    label.delete(IBh_30_label)
    label.delete(IBl_30_label)
    label.delete(IBh_40_label)
    label.delete(IBl_40_label)



    




        

// last bar of session - inSession[1] and not inSession
// first bar session - not inSession[1] and inSession