Income Ratio
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © morzor61

//@version=5
indicator("Income Ratio", format=format.volume)


// Revenue & Gross Profit
data01   =      'Total revenue'	                                           // 'TOTAL_REVENUE'
data02   =      ' Cost of goods'	                                       // 'COST_OF_GOODS'
data03   =      '  Deprecation and amortization'	                       // 'DEP_AMORT_EXP_INCOME_S'
data04   =      '  Other COGS'	                                       // 'COST_OF_GOODS_EXCL_DEP_AMORT'
data05   =      'Gross profit'	                                           // 'GROSS_PROFIT'

// Total Operating Income / Expense ------------------------------------------------
data06   =      'Operating expenses (excl. COGS)'	                    // 'OPERATING_EXPENSES'
data07   =      '  Selling/general/admin expenses, total'	            //     'SELL_GEN_ADMIN_EXP_TOTAL'
data08   =      '    Selling/general/admin expenses, other'	            //         'SELL_GEN_ADMIN_EXP_OTHER'
data09   =      '    Research & development'	                        //         'RESEARCH_AND_DEV'
data10   =      '  Other operating expenses, total'	                    //     'OTHER_OPER_EXPENSE_TOTAL'
data11   =      'Operating income'	                                    // 'OPER_INCOME'

// Non-Operating Income / Expense ----------------------------------------------
data12   =      'Non-operating interest income'	                          // 'NON_OPER_INTEREST_INCOME'
data13   =      '  Interest expense, net of interest capitalized'	      // 'NON_OPER_INTEREST_EXP'
data14   =      '    Interest expense on debt'	                          // 'INTEREST_EXPENSE_ON_DEBT'
data15   =      '    Interest capitalized'	                              // 'INTEREST_CAPITALIZED'
data16   =      '  Non-operating income, excl. interest expenses'	      // 'NON_OPER_INCOME'
data17   =      '    Non-operating income, total'	                      // 'TOTAL_NON_OPER_INCOME'
data18   =      '    Pretax equity in earnings'	                          //     'PRETAX_EQUITY_IN_EARNINGS'
data19   =      '    Miscellaneous non-operating expense'	              // 'OTHER_INCOME'
data20   =      '  Unusual income/expense'	                              // 'UNUSUAL_EXPENSE_INC'


data21   =      'Pretax income'	                                           // 'PRETAX_INCOME'
data22   =      '  Equity in earnings'	                                   // 'EQUITY_IN_EARNINGS'
data23   =      '  Taxes'	                                               // 'INCOME_TAX'
data24   =      '  Non-controlling/minority interest'	                   // 'MINORITY_INTEREST_EXP'
data25   =      'After tax other income/expense'	                       // 'AFTER_TAX_OTHER_INCOME'
data26   =      'Net income before discontinued operations'	               // 'NET_INCOME_BEF_DISC_OPER'
data27   =      '  Discontinued operations'	                               // 'DISCONTINUED_OPERATIONS'
data28   =      'Net income'	                                           // 'NET_INCOME'
data29   =      '  Dilution adjustment'	                                   // 'DILUTION_ADJUSTMENT'
data30   =      '  Preferred dividends'	                                   // 'PREFERRED_DIVIDENDS'
data31   =      '  Diluted net income available to common stockholders'	   // 'DILUTED_NET_INCOME'
data32   =      'Basic EPS'	                                               // 'EARNINGS_PER_SHARE_BASIC'
data33   =      'Diluted EPS'	                                           // 'EARNINGS_PER_SHARE_DILUTED'
data34   =      'Average basic shares outstanding'	                       // 'BASIC_SHARES_OUTSTANDING'
data35   =      'Diluted shares outstanding'	                           // 'DILUTED_SHARES_OUTSTANDING'
data36   =      'EBITDA'	                                               // 'EBITDA'
data37   =      'EBIT'	                                                   // 'EBIT'
data38   =      'Total operating expenses'	                               // 'TOTAL_OPER_EXPENSE'
data39   =       'Total Share'

data_type   =   input.string('Percent',title ='Data', options=['Percent','Value, M'] )
per         =   input.string('FY', options=['FQ', 'FY','TTM'])
f_fin(_id, _period)=> 
     request.financial(syminfo.tickerid, _id, _period, gaps=barmerge.gaps_off, ignore_invalid_symbol=true)

item_01   = f_fin(  'TOTAL_REVENUE'                 , per) 
item_02   = f_fin(  'COST_OF_GOODS'                 , per) 
item_03   = f_fin(  'DEP_AMORT_EXP_INCOME_S'        , per)
// item_04   = f_fin(  'COST_OF_GOODS_EXCL_DEP_AMOR'   , per)
item_05   = f_fin(  'GROSS_PROFIT'                  , per) 

item_06   = f_fin(  'OPERATING_EXPENSES'            , per)
// item_07   = f_fin(  'SELL_GEN_ADMIN_EXP_TOTAL'      , per)
// item_08   = f_fin(  'SELL_GEN_ADMIN_EXP_OTHER'      , per)
// item_09   = f_fin(  'RESEARCH_AND_DEV'              , per)
// item_10   = f_fin(  'OTHER_OPER_EXPENSE_TOTAL'      , per)
item_11   = f_fin(  'OPER_INCOME'                   , per)

// item_12   = f_fin(  'NON_OPER_INTEREST_INCOME'  , per)
// item_13   = f_fin(  'NON_OPER_INTEREST_EXP'     , per)
// item_14   = f_fin(  'INTEREST_EXPENSE_ON_DEBT'  , per)
// item_15   = f_fin(  'INTEREST_CAPITALIZED'      , per)
// item_16   = f_fin(  'NON_OPER_INCOME'           , per)
// item_17   = f_fin(  'TOTAL_NON_OPER_INCOME'     , per)
// item_18   = f_fin(  'PRETAX_EQUITY_IN_EARNINGS' , per)
// item_19   = f_fin(  'OTHER_INCOME'              , per)
// item_20   = f_fin(  'UNUSUAL_EXPENSE_INC'       , per)
// item_21   = f_fin(  'PRETAX_INCOME'             , per)
// item_22   = f_fin(  'EQUITY_IN_EARNINGS'        , per)
item_23   = f_fin(  'INCOME_TAX'                , per)
// item_24   = f_fin(  'MINORITY_INTEREST_EXP'     , per)
// item_25   = f_fin(  'AFTER_TAX_OTHER_INCOME'    , per)
// item_26   = f_fin(  'NET_INCOME_BEF_DISC_OPER'  , per)
// item_27   = f_fin(  'DISCONTINUED_OPERATIONS'    , per)
item_28   = f_fin(  'NET_INCOME'                , per)
// item_29   = f_fin(  'DILUTION_ADJUSTMENT'        , per)
// item_30   = f_fin(  'PREFERRED_DIVIDENDS'       , per)
// item_31   = f_fin(  'DILUTED_NET_INCOME'        , per)
// item_32   = f_fin(  'EARNINGS_PER_SHARE_BASIC'   , per)
// item_33   = f_fin(  'EARNINGS_PER_SHARE_DILUTED' , per)
// item_34   = f_fin(  'BASIC_SHARES_OUTSTANDING'    , per)
// item_35   = f_fin(  'DILUTED_SHARES_OUTSTANDING' , per)
// item_36   = f_fin(  'EBITDA' , per)
// item_37   = f_fin(  'EBIT'  , per)
// item_38   = f_fin(  'TOTAL_OPER_EXPENSE'   , per)
// item_39   = f_fin(  'TOTAL_SHARES_OUTSTANDING' , per) 


f_ratio(_item, _type)=>
    percent =   _type == 'Percent' ? _item/item_01*100 : _item/1000000

per_01  =   f_ratio(item_01, data_type)
per_02  =   f_ratio(item_02, data_type)
per_03  =   f_ratio(item_03, data_type)
per_05  =   f_ratio(item_05, data_type)
per_06  =   f_ratio(item_06, data_type)
per_11  =   f_ratio(item_11, data_type)
per_23  =   f_ratio(item_23, data_type)
per_28  =   f_ratio(item_28, data_type)

// plot(per_01)
plot(per_01, 'Revenue')
plot(per_03, 'Deprecation and amortization',    style=plot.style_area, color=color.purple)
plot(per_02, 'COGS',                style=plot.style_area, color=color.new(color.orange, 50))
plot(per_11, 'Operating Income',    color=color.navy)
plot(per_06, 'Operating Expense',   color=color.red, linewidth=2)
plot(per_06+per_23, 'Income Tax',   color=color.lime)
plot(per_05, 'Gross Profit',        style=plot.style_area, color=color.new(color.green,50), linewidth=3)
plot(per_28, 'Net Income',          style=plot.style_circles, color=color.black, linewidth=3, join=true)

if barstate.islast 
    // label.new(bar_index+2, per_01,                  style=label.style_label_left, text='Revenue:'+str.tostring(per_01, '#,##0.0'))
    label.new(bar_index+2, per_02,          style=label.style_label_left, text='COGS:'+             str.tostring(per_02, '#,##0.0'), color= color.new(color.orange,50))
    label.new(bar_index+2, per_03,          style=label.style_label_left, text='Dep/Amor:'+         str.tostring(per_03, '#,##0.0'), color = color.new(color.purple, 50))
    label.new(bar_index+2, per_06,          style=label.style_label_left, text='Operating expense:'+str.tostring(per_06, '#,##0.0'), color = color.new(color.red, 50))
    label.new(bar_index+2, per_06+per_23,   style=label.style_label_left, text='Income Tax:'+       str.tostring(per_23, '#,##0.0'), color=color.new(color.lime,50))
    label.new(bar_index+2, per_05,          style=label.style_label_left, text='Gross Profit:'+     str.tostring(per_05, '#,##0.0'), color = color.new(color.green, 50))
    label.new(bar_index+2, per_28,          style=label.style_label_left, text='Net Income:'+       str.tostring(per_28, '#,##0.0'))
    label.new(bar_index+2, per_11,          style=label.style_label_left, text='Operating Income:'+ str.tostring(per_11, '#,##0.0'), color = color.new(color.navy, 50))  
    
    
    

