Scale Ability [TrendX_]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TrendX_

//@version=5
//      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                              xxxxxxxxxxxxxxxxxxxxxxxxxx
//              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxx
//                                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxx
//                                          XXX.....X....X....X..XX:X...XX      xx 
//                                              X:XXX:..:X:...X:X.X:X:XX.XXXXX  
//                                          XXXXX:XXX:  .X:...X:XX..X:..XX      xx
//                                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxx
//                                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxx
//                              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                  xxxxxxxxxxxxxxxxxxxxxxxxxx
//          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                          xxxxxxxxxxxxxxxxxxxxxxxxxx
//      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                  xxxxxxxxxxxxxxxxxxxxxxxxxx
//

indicator("Scale Ability [TrendX_]", overlay = false)



//// ____ Scale ability setting ____ ////

//Input
bg_trans                 = input.int(   defval = 90  , title = "Background Transparency"                         )
Type_period              = input.string(defval = "FQ", title = "Type of Financial Period", options = ["FY", "FQ"])
var float EBITDA_current = na
var float Equity_current = na
var float CFIA_current   = na
var float TA_current     = na
var float TD_current     = na
var float NI_current     = na

//Get Financial data
EBITDA_request           = request.financial(syminfo.tickerid, "EBITDA"                     , Type_period)
Equity_request           = request.financial(syminfo.tickerid, "TOTAL_EQUITY"               , Type_period)
CFIA_request             = request.financial(syminfo.tickerid, "CASH_F_INVESTING_ACTIVITIES", Type_period)
TA_request               = request.financial(syminfo.tickerid, "TOTAL_ASSETS"               , Type_period)
TD_request               = request.financial(syminfo.tickerid, "TOTAL_DEBT"                 , Type_period)
NI_request               = request.financial(syminfo.tickerid, "DILUTED_NET_INCOME"         , Type_period)

//Define Financial data
if (not na(EBITDA_request)) 
    EBITDA_current      := EBITDA_request
if (not na(Equity_request)) 
    Equity_current      := Equity_request
if (not na(CFIA_request)) 
    CFIA_current        := CFIA_request
if (not na(TA_request)) 
    TA_current          := TA_request
if (not na(TD_request)) 
    TD_current          := TD_request
if (not na(NI_request)) 
    NI_current          := NI_request

CFIATA                   = CFIA_current   / TA_current
NITD                     = NI_current     / TD_current
EBITDAE                  = EBITDA_current / Equity_current

//Calculate Scaling index
Scale_ability            = math.avg(CFIATA, NITD, EBITDAE) * 100

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//// ____ Plotting ____ ////

//Define Coloring functions
ColorFunc(Scale_ability) =>
    if Scale_ability >= 43
        color.rgb(90, 0, 147)
    else if Scale_ability >= 40 and Scale_ability < 43
        color.rgb(0, 4, 133)
    else if Scale_ability >= 36 and Scale_ability < 40
        color.rgb(0, 25, 247)
    else if Scale_ability >= 31 and Scale_ability < 36
        color.rgb(4, 83, 0)
    else if Scale_ability >= 26 and Scale_ability < 31
        color.rgb(6, 123, 0)
    else if Scale_ability >= 21 and Scale_ability < 26
        color.rgb(9, 163, 1)
    else if Scale_ability >= 16 and Scale_ability < 21
        color.rgb(11, 224, 0)
    else if Scale_ability >  11 and Scale_ability < 16
        color.rgb(13, 255, 0)
    else if Scale_ability == 11
        color.rgb(111, 247, 0)
    else if Scale_ability <  11 and Scale_ability > 10
        color.rgb(247, 194, 0)
    else if Scale_ability <= 10 and Scale_ability >  9
        color.rgb(247, 161, 0)
    else if Scale_ability <=  9 and Scale_ability >  8
        color.rgb(247, 124, 0)
    else if Scale_ability <=  8 and Scale_ability >  7
        color.rgb(207, 117, 0)
    else if Scale_ability <=  7 and Scale_ability >  6
        color.rgb(247, 99, 0)
    else if Scale_ability <=  6 and Scale_ability >  5
        color.rgb(223, 68, 1)
    else if Scale_ability <=  4 
        color.rgb(100, 0, 0)

bgFunc(Scale_ability) =>
    if Scale_ability >= 43
        color.rgb(90, 0, 147, bg_trans)
    else if Scale_ability >= 40 and Scale_ability < 43
        color.rgb(0, 4, 133, bg_trans)
    else if Scale_ability >= 36 and Scale_ability < 40
        color.rgb(0, 25, 247, bg_trans)
    else if Scale_ability >= 31 and Scale_ability < 36
        color.rgb(4, 83, 0, bg_trans)
    else if Scale_ability >= 26 and Scale_ability < 31
        color.rgb(6, 123, 0, bg_trans)
    else if Scale_ability >= 21 and Scale_ability < 26
        color.rgb(9, 163, 1, bg_trans)
    else if Scale_ability >= 16 and Scale_ability < 21
        color.rgb(11, 224, 0, bg_trans)
    else if Scale_ability >  11 and Scale_ability < 16
        color.rgb(13, 255, 0, bg_trans)
    else if Scale_ability == 11
        color.rgb(111, 247, 0, bg_trans)
    else if Scale_ability <  11 and Scale_ability > 10
        color.rgb(247, 194, 0, bg_trans)
    else if Scale_ability <= 10 and Scale_ability >  9
        color.rgb(247, 161, 0, bg_trans)
    else if Scale_ability <=  9 and Scale_ability >  8
        color.rgb(247, 124, 0, bg_trans)
    else if Scale_ability <=  8 and Scale_ability >  7
        color.rgb(207, 117, 0, bg_trans)
    else if Scale_ability <=  7 and Scale_ability >  6
        color.rgb(247, 99, 0, bg_trans)
    else if Scale_ability <=  6 and Scale_ability >  5
        color.rgb(223, 68, 1, bg_trans)
    else if Scale_ability <=  4 
        color.rgb(100, 0, 0, bg_trans)

//Plotting Scaling index

index_color = ColorFunc(Scale_ability)
bg_color    = bgFunc(   Scale_ability)
plot(Scale_ability, title = "Scale Ability index line", color = index_color, style = plot.style_stepline, linewidth = 3)
bgcolor(color = bg_color)

//Plotting average level
hline(11, "Average level", color=color.yellow, linestyle = hline.style_dashed, linewidth = 1)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//// ____ Table ____ ////

//Input
var table scaleTable = na

//Table display
scaleTable := table.new(position.middle_right, 2, 6, color.rgb(15, 2, 16), frame_color = color.rgb(226, 251, 218), frame_width = 1, border_color = color.rgb(226, 251, 218), border_width = 1)

table.cell(scaleTable, 0, 0, "Exellent"     , text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 0, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability >= 43                       ) ? color.rgb(90, 0, 147)  : na, text_size = size.auto)
table.cell(scaleTable, 0, 1, "Good"         , text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 1, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability >= 31 and Scale_ability < 43) ? color.rgb(4, 83, 0)    : na, text_size = size.auto)
table.cell(scaleTable, 0, 2, "Above Average", text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 2, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability >= 11 and Scale_ability < 31) ? color.rgb(13, 255, 0)  : na, text_size = size.auto)
table.cell(scaleTable, 0, 3, "Below Average" , text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 3, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability >   7 and Scale_ability < 11) ? color.rgb(207, 117, 0) : na, text_size = size.auto)
table.cell(scaleTable, 0, 4, "Poor"          , text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 4, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability >   4 and Scale_ability <= 7) ? color.rgb(185, 0, 0)   : na, text_size = size.auto)
table.cell(scaleTable, 0, 5, "Very Poor"     , text_color = color.white, text_size = size.auto)
table.cell(scaleTable, 1, 5, str.tostring(math.round(Scale_ability)) + "%", text_color = (Scale_ability <=  4                       ) ? color.rgb(185, 0, 0)   : na, text_size = size.auto)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////