IU Average move 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shivammandrai

//@version=5
indicator("IU Average move ",overlay=true)

///// CREATING CUSTOME SESSION 
custome_session = input.session("0915-1000", title='Enter your Session')


///// CHECKING SESSIONS 
into_the_session = time(timeframe.period, custome_session)
next_session(R, S) =>
    Time = time(R, S)
    na(Time[1]) and not na(Time) or Time[1] < Time

New_session = next_session("1440", custome_session)

///// CHECKING NEW SESSIONS
next_candle(R,S) =>
    Time = time(R,S)
    not na(Time) and (na(Time[1]) or Time > Time[1])
new = (next_candle("1440", custome_session) ? 1 : 0)


//// STORING HIGH AND LOW 
var float Low = close
var float High = close

Low := New_session? low : into_the_session ? math.min(low, Low[1]) : na
High := New_session  ? high : into_the_session ? math.max(high, High[1]) : na




//// GETTING FINAL HIGH AND LOW 
extend_low = ta.valuewhen((Low == low),low,0)

extend_high = ta.valuewhen((High == high),high,0)

///// CREATING CONDITIONS FOR GETTING END SESSION 
start_of_session_value = ta.valuewhen((new==1),high,0)
end_session_condition = start_of_session_value !=start_of_session_value[1]?0: into_the_session?1:0 

////// CREARING ARRAYS TO STORE VALUES 
var range_array = array.new<float>()
var range_pec_array = array.new<float>()

//// GETTING THE FIRST BAR OUTBOF THE SESSION 
starting_point = time[1] <= into_the_session and time > into_the_session

//// CREATING RANGE VARIABLE
var float range_ = na
var float range_pec = na


///// CALCULATING RANGE AND STORING ITS VALUE
if end_session_condition == 0
    range_ := extend_high - extend_low
    array.push(range_array,range_)
    range_pec := (extend_high - extend_low) / (((extend_low + extend_high ) / 2)/100)
    array.push(range_pec_array, range_pec)

//// GETTING THE AVRAGE OF THAT ARRAY    
avg = math.round(array.avg(range_array), precision = 2)

avg_pec = math.round(array.avg(range_pec_array), precision = 3)

///// CREATING TABLE
bgcolor = input.color(color.blue, "BG Color")
bgcolor_tr = input.int(50, "Enter the color Transparancy")
text_color = input.color(color.green, "Enter The text color  ")
var table array_table = table.new(position.top_right, 3,3 , bgcolor = color.new(bgcolor, bgcolor_tr))

text1 = "The Average Move " + "\n " + str.tostring(avg)
text2 = "The % Average Move " + "\n " + str.tostring(avg_pec)

//// SETTING TABLE VALUES 

table.cell(array_table, 1,1,text = text1, text_color = text_color)
table.cell(array_table, 1,2,text = text2, text_color = text_color)
