IU SIP CALCULATOR
// 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 SIP", overlay = true )

///////// WARNING 
if timeframe.period != "M"
    runtime.error("Please Go To monthly(M) timeframe ")

///// SIP GREEN SIGNAL 
go_SIP = true 


//// USER INPUTS
Amount = input.int(5000, "Enter the SIP Amount")
start_month = input.int(1, "Starting Month ?", minval = 1, maxval = 12)
start_year = input.int(2022, "Startimg Year ?", minval = 1900, maxval = 2100)


ending_month = input.int(12, "Ending Month ?", minval = 1, maxval = 12)
ending_year = input.int(2022, "Ending Year ?", minval = 1900, maxval = 2100)

///// COLOR VARIABLES 
table_color = input.color(color.gray, 'Table Color')
text_color = input.color(color.white, "Text Color")
frame_color = input.color(color.green, "Frame Color ")
tranc = input.int(30, "Enter the transparency", minval = 0, maxval = 100)


var monthly_close = array.new<float>()
var monthly_open = array.new<float>()
var time_line = array.new<string>()

var string text1 = na
var string text2 = na 

monthly_Close = request.security(syminfo.tickerid, "M", close)

monthly_Open = request.security(syminfo.tickerid, "M", open)

var stocks = array.new<float>()


if go_SIP == true and timeframe.period == "M"
    array.push(monthly_close, monthly_Close)
    array.push(monthly_open, monthly_Open )
    text1 := str.tostring(month) + str.tostring(year)
    array.push(time_line, text1)
    array.push(stocks, (Amount / monthly_Open))

    
    
//// MATCHING INDEX VALUE WITH INPUTS 

string m1 = str.tostring(start_month) + str.tostring(start_year)

string m2 = str.tostring(ending_month) + str.tostring(ending_year)

///////  CREATING VARIABLES WITH na
int getting_index_start = na
int getting_index_end = na
float [] slicing_array1 = na
float open_value = na
float average = na
float amount_invt = na
float number_of_stocks = na
float profit_value = na
float portfolio_value = na
float profit_value_pec = na
float returns = na 
float end_close = na
float monthly_returns = na
float yearly_returns = na 

///// SEEING IF THE ARRAY CONTAIN THE RIGHT VALUE OR NOT 
float first_open = na
float last_open = na 

float [] slicing_stocks = na
float stocks_no = na

if year >= ending_year and month >= ending_month and barstate.isconfirmed
    //// GETTIMG THE INDEXOF VALUE 
    getting_index_start  := array.indexof(time_line, m1)


    getting_index_end := array.indexof(time_line, m2)

    ////// GETTING THE VALUES THROUGH SLICING ARRAYS 
    slicing_array1 :=  array.slice(monthly_open, (getting_index_start), (getting_index_end +1)) 
    slicing_stocks := array.slice(stocks, getting_index_start, (getting_index_end + 1))

    end_close := array.get(monthly_close, getting_index_end)

    average := array.avg(slicing_array1) 

    

    returns := end_close - average


    //// CALCULATING INVESTMENT VALUE 
    amount_invt := (array.size(slicing_array1)) *  Amount

    /// CAL PORTFOLIO VALUE 
    
    stocks_no := array.sum(slicing_stocks)

    profit_value := stocks_no * returns

    portfolio_value := amount_invt + profit_value

    profit_value_pec := profit_value / (amount_invt * 0.01)
    
    first_open := array.get(slicing_array1,0)
    last_open := array.last(slicing_array1)
    
    monthly_returns := profit_value_pec / (array.size(slicing_array1))
    
    yearly_returns := profit_value_pec / (array.size(slicing_array1) / 12)

    ///MAKING THE TABLE 


    var table mytable = table.new(position = position.top_right,columns = 8, rows = 8, bgcolor = color.new(table_color, tranc), border_width = 3, frame_color = color.new(table_color, tranc), frame_width = 5 )


    ///// CREARING TEXTS 
    text_1 = "Avrage Buy Price " + "\n " + str.tostring(math.round(average, 2))
    text_2 = "Number of Stocks " + "\n " + str.tostring(math.floor(stocks_no))
    text3 = "Profit Value" + "\n " + str.tostring(math.round(profit_value, 2))
    text4 = "Profit Value in % " + "\n " + str.tostring(math.round(profit_value_pec, 2)) + "%"
    text5 = "Amount Invested " + "\n " + str.tostring(amount_invt)
    text6 = "Portfolio value" + "\n " + str.tostring(math.round(portfolio_value, 2))
    text7 = "yearly_returns " + "\n " + str.tostring(math.round(yearly_returns, 2)) + "%"
    text8 = "monthly_returns " + "\n " + str.tostring(math.round(monthly_returns, 2)) + "%"
    text9 =  profit_value > 0 ? "Performace is : 😊 " : "Performace is : 😓 "

    table.cell(mytable, 1,1,text = "SIP OF " + str.tostring(syminfo.tickerid), text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 1,2,text = "", text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 1,3,text = "", text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 2,1,text = text_1, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 2,2,text = text_2, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 2,3,text = text3, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 3,1,text = text4, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 3,2,text = text5, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 3,3,text = text6, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 1,4,text = text7, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 2,4,text = text8, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.cell(mytable, 3,4,text = text9, text_color = text_color, bgcolor = profit_value > 0 ? color.new(frame_color, tranc) : color.new(color.red, tranc))
    table.merge_cells(mytable, start_column = 1, start_row = 1, end_column = 1, end_row = 3)

