Financial Metrics
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BasitAndrabi

//@version=5
indicator("FinMet 2.0", overlay = true)

// Financial Metrics (FinMet 2.0) - Fundamental Ratios and Quarterly Earnings
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                   INPUTS                                   |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|


// Symbol
s01Col = input.color(color.rgb(49, 126, 72),    title = "EPS", inline="s01Col", group="EPS & Sales bars")
s01Col2 = input.color(color.rgb(124, 46, 46),    title = "", inline="s01Col", group="EPS & Sales bars")
s01Cols = input.color((color.rgb(33, 117, 185)), title = "Sales", inline="s01cols", group="EPS & Sales bars") 

Period = input.string(title="Period", defval="FQ", options=["FQ", "TTM"], group="EPS & Sales bars")






// Plot Location
in_plot_pos  = input.string(title="Plot Location", defval= "Middle Right", 
     options =["Top Right" , "Middle Right" , "Bottom Right" , 
               "Top Center", "Middle Center", "Bottom Center", 
               "Top Left"  , "Middle Left"  , "Bottom Left" ], group= "Plot Setting")

// Bar Size
barHigh = input.float(0.3, "Bar Height", minval = 0.2, step= 0.01, group = "Plot Setting")
barWdth = input.float(0.8, "Bar Width" , minval = 0.2,   step= 0.01, group = "Plot Setting")

// Plot Color
pltCol  = input.color(#2a3133,      title="Background", group = "Plot Setting")
borCol  = input.color(color.rgb(53, 64, 124), title="Border", group = "Plot Setting")
txtCol  = input.color(color.white,   title="Text",      group = "Plot Setting")

// Financial Ratio limits
lim_PE = input.float(15, "PE", minval = 0, step= 1, group = "Set Limits", inline = "lim_PE")
lim_PEG = input.float(1, "PEG", minval = 0, step= 0.1, group = "Set Limits", inline = "lim_PE")
lim_PS = input.float(2, "P/S", minval = 0, step= 0.1, group = "Set Limits", inline = "lim_PS")
lim_PB = input.float(2, "P/B", minval = 0, step= 0.1, group = "Set Limits", inline = "lim_PB")
lim_EVFCF = input.float(10, "EV/FCF", minval = 0, step= 1, group = "Set Limits", inline = "lim_PS")
lim_OPM = input.float(15, "OPM", minval = 0, step= 1, group = "Set Limits", inline = "lim_OPM")
lim_DE = input.float(1, "D/E", minval = 0, step= 0.1, group = "Set Limits", inline = "lim_OPM")
lim_ROE = input.float(15, "ROE", minval = 0, step= 1, group = "Set Limits", inline = "lim_ROE")
lim_DVY = input.float(2, "Div_Yield", minval = 0, step= 0.1, group = "Set Limits", inline = "lim_ROE")



// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                             CALCULATION                                    |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// Calculation

// Arrays initialised............................................................. 
sym01_arr = array.new_float(12)
QOQ_arr = array.new_float(12)
YOY_arr = array.new_float(12)

sym01S_arr = array.new_float(12)
QOQs_arr = array.new_float(12)
YOYs_arr = array.new_float(12)

//EPS = request.earnings(syminfo.tickerid, earnings.actual, ignore_invalid_symbol=true)

EPS = request.earnings(syminfo.tickerid, earnings.standardized, ignore_invalid_symbol=true)

SALES = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")

var earning_ticker      = "ESD_FACTSET:" + syminfo.prefix + ";" + syminfo.ticker + ";EARNINGS"


ResultDate = request.security(earning_ticker, "D", time, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true) //earning report date

//    
EPSBarsNumber = ta.barssince(EPS != EPS[1]) == 0
// Actual EPS
actualEPS  = EPS
actualEPS1 = ta.valuewhen(EPSBarsNumber, EPS, 1) // With "1" to search the previous EPS value, etc
actualEPS2 = ta.valuewhen(EPSBarsNumber, EPS, 2)
actualEPS3 = ta.valuewhen(EPSBarsNumber, EPS, 3)
actualEPS4 = ta.valuewhen(EPSBarsNumber, EPS, 4)
actualEPS5 = ta.valuewhen(EPSBarsNumber, EPS, 5)
actualEPS6 = ta.valuewhen(EPSBarsNumber, EPS, 6)
actualEPS7 = ta.valuewhen(EPSBarsNumber, EPS, 7)
actualEPS8 = ta.valuewhen(EPSBarsNumber, EPS, 8)
actualEPS9 = ta.valuewhen(EPSBarsNumber, EPS, 9)
actualEPS10 = ta.valuewhen(EPSBarsNumber, EPS, 10)
actualEPS11 = ta.valuewhen(EPSBarsNumber, EPS, 11)
actualEPS12 = ta.valuewhen(EPSBarsNumber, EPS, 12)
actualEPS13 = ta.valuewhen(EPSBarsNumber, EPS, 13)
actualEPS14 = ta.valuewhen(EPSBarsNumber, EPS, 14)
actualEPS15 = ta.valuewhen(EPSBarsNumber, EPS, 15)


// QOQ EPS growth.................................................................
QOQ0 = (actualEPS-actualEPS1)/math.abs(actualEPS1)*100
QOQ1 = (actualEPS1-actualEPS2)/math.abs(actualEPS2)*100
QOQ2 = (actualEPS2-actualEPS3)/math.abs(actualEPS3)*100
QOQ3 = (actualEPS3-actualEPS4)/math.abs(actualEPS4)*100
QOQ4 = (actualEPS4-actualEPS5)/math.abs(actualEPS5)*100
QOQ5 = (actualEPS5-actualEPS6)/math.abs(actualEPS6)*100
QOQ6 = (actualEPS6-actualEPS7)/math.abs(actualEPS7)*100
QOQ7 = (actualEPS7-actualEPS8)/math.abs(actualEPS8)*100
QOQ8 = (actualEPS8-actualEPS9)/math.abs(actualEPS9)*100
QOQ9 = (actualEPS9-actualEPS10)/math.abs(actualEPS10)*100
QOQ10 = (actualEPS10-actualEPS11)/math.abs(actualEPS11)*100
QOQ11 = (actualEPS11-actualEPS12)/math.abs(actualEPS12)*100


// YOY EPS growth.................................................................
YOY0 = (actualEPS-actualEPS4)/math.abs(actualEPS4)*100
YOY1 = (actualEPS1-actualEPS5)/math.abs(actualEPS5)*100
YOY2 = (actualEPS2-actualEPS6)/math.abs(actualEPS6)*100
YOY3 = (actualEPS3-actualEPS7)/math.abs(actualEPS7)*100
YOY4 = (actualEPS4-actualEPS8)/math.abs(actualEPS8)*100
YOY5 = (actualEPS5-actualEPS9)/math.abs(actualEPS9)*100
YOY6 = (actualEPS6-actualEPS10)/math.abs(actualEPS10)*100
YOY7 = (actualEPS7-actualEPS11)/math.abs(actualEPS11)*100
YOY8 = (actualEPS8-actualEPS12)/math.abs(actualEPS12)*100
YOY9 = (actualEPS9-actualEPS13)/math.abs(actualEPS13)*100
YOY10 = (actualEPS10-actualEPS14)/math.abs(actualEPS14)*100
YOY11 = (actualEPS11-actualEPS15)/math.abs(actualEPS15)*100


// Set Array Elements....................................................................
if  Period == "FQ"
    array.set(sym01_arr, 11, actualEPS)
    array.set(sym01_arr, 10, actualEPS1)
    array.set(sym01_arr, 9, actualEPS2)
    array.set(sym01_arr, 8, actualEPS3)
    array.set(sym01_arr, 7, actualEPS4)
    array.set(sym01_arr, 6, actualEPS5)
    array.set(sym01_arr, 5, actualEPS6)
    array.set(sym01_arr, 4, actualEPS7)
    array.set(sym01_arr, 3, actualEPS8)
    array.set(sym01_arr, 2, actualEPS9)
    array.set(sym01_arr, 1, actualEPS10)
    array.set(sym01_arr, 0, actualEPS11)

else 
    array.set(sym01_arr, 11, actualEPS+actualEPS1+actualEPS2+actualEPS3)
    array.set(sym01_arr, 10, actualEPS1+actualEPS2+actualEPS3+actualEPS4)
    array.set(sym01_arr, 9, actualEPS2+actualEPS3+actualEPS4+actualEPS5)
    array.set(sym01_arr, 8, actualEPS3+actualEPS4+actualEPS5+actualEPS6)
    array.set(sym01_arr, 7, actualEPS4+actualEPS5+actualEPS6+actualEPS7)
    array.set(sym01_arr, 6, actualEPS5+actualEPS6+actualEPS7+actualEPS8)
    array.set(sym01_arr, 5, actualEPS6+actualEPS7+actualEPS8+actualEPS9)
    array.set(sym01_arr, 4, actualEPS7+actualEPS8+actualEPS9+actualEPS10)
    array.set(sym01_arr, 3, actualEPS8+actualEPS9+actualEPS10+actualEPS11)
    array.set(sym01_arr, 2, actualEPS9+actualEPS10+actualEPS11+actualEPS12)
    array.set(sym01_arr, 1, actualEPS10+actualEPS11+actualEPS12+actualEPS13)
    array.set(sym01_arr, 0, actualEPS11+actualEPS12+actualEPS13+actualEPS14)

//


array.set(QOQ_arr, 11, QOQ0)
array.set(QOQ_arr, 10, QOQ1)
array.set(QOQ_arr, 9, QOQ2)
array.set(QOQ_arr, 8, QOQ3)
array.set(QOQ_arr, 7, QOQ4)
array.set(QOQ_arr, 6, QOQ5)
array.set(QOQ_arr, 5, QOQ6)
array.set(QOQ_arr, 4, QOQ7)
array.set(QOQ_arr, 3, QOQ8)
array.set(QOQ_arr, 2, QOQ9)
array.set(QOQ_arr, 1, QOQ10)
array.set(QOQ_arr, 0, QOQ11)

array.set(YOY_arr, 11, YOY0)
array.set(YOY_arr, 10, YOY1)
array.set(YOY_arr, 9, YOY2)
array.set(YOY_arr, 8, YOY3)
array.set(YOY_arr, 7, YOY4)
array.set(YOY_arr, 6, YOY5)
array.set(YOY_arr, 5, YOY6)
array.set(YOY_arr, 4, YOY7)
array.set(YOY_arr, 3, YOY8)
array.set(YOY_arr, 2, YOY9)
array.set(YOY_arr, 1, YOY10)
array.set(YOY_arr, 0, YOY11)

float QOQ = na
float YOY = na


SalesBarsNumber = ta.barssince(SALES != SALES[1]) == 0
// Actual SALES
salesA  = SALES
sales1A = ta.valuewhen(SalesBarsNumber, SALES, 1) // With "1" to search the previous SALES value, etc
sales2A = ta.valuewhen(SalesBarsNumber, SALES, 2)
sales3A = ta.valuewhen(SalesBarsNumber, SALES, 3)
sales4A = ta.valuewhen(SalesBarsNumber, SALES, 4)
sales5A = ta.valuewhen(SalesBarsNumber, SALES, 5)
sales6A = ta.valuewhen(SalesBarsNumber, SALES, 6)
sales7A = ta.valuewhen(SalesBarsNumber, SALES, 7)
sales8A = ta.valuewhen(SalesBarsNumber, SALES, 8)
sales9A = ta.valuewhen(SalesBarsNumber, SALES, 9)
sales10A = ta.valuewhen(SalesBarsNumber, SALES, 10)
sales11A = ta.valuewhen(SalesBarsNumber, SALES, 11)
sales12A = ta.valuewhen(SalesBarsNumber, SALES, 12)
sales13A = ta.valuewhen(SalesBarsNumber, SALES, 13)
sales14A = ta.valuewhen(SalesBarsNumber, SALES, 14)
sales15A = ta.valuewhen(SalesBarsNumber, SALES, 15)

// Sales in Crores
sales        = (salesA/10000000)
sales1        = (sales1A/10000000)
sales2        = (sales2A/10000000)
sales3        = (sales3A/10000000)
sales4        = (sales4A/10000000)
sales5        = (sales5A/10000000)
sales6        = (sales6A/10000000)
sales7        = (sales7A/10000000)
sales8        = (sales8A/10000000)
sales9        = (sales9A/10000000)
sales10        = (sales10A/10000000)
sales11        = (sales11A/10000000)
sales12        = (sales12A/10000000)
sales13       = (sales13A/10000000)
sales14       = (sales14A/10000000)
sales15       = (sales15A/10000000)



// QOQ Sales growth.................................................................
QOQs0 = (sales-sales1)/math.abs(sales1)*100
QOQs1 = (sales1-sales2)/math.abs(sales2)*100
QOQs2 = (sales2-sales3)/math.abs(sales3)*100
QOQs3 = (sales3-sales4)/math.abs(sales4)*100
QOQs4 = (sales4-sales5)/math.abs(sales5)*100
QOQs5 = (sales5-sales6)/math.abs(sales6)*100
QOQs6 = (sales6-sales7)/math.abs(sales7)*100
QOQs7 = (sales7-sales8)/math.abs(sales8)*100
QOQs8 = (sales8-sales9)/math.abs(sales9)*100
QOQs9 = (sales9-sales10)/math.abs(sales10)*100
QOQs10 = (sales10-sales11)/math.abs(sales11)*100
QOQs11 = (sales11-sales12)/math.abs(sales12)*100


// YOY Sales growth.................................................................
YOYs0 = (sales-sales4)/math.abs(sales4)*100
YOYs1 = (sales1-sales5)/math.abs(sales5)*100
YOYs2 = (sales2-sales6)/math.abs(sales6)*100
YOYs3 = (sales3-sales7)/math.abs(sales7)*100
YOYs4 = (sales4-sales8)/math.abs(sales8)*100
YOYs5 = (sales5-sales9)/math.abs(sales9)*100
YOYs6 = (sales6-sales10)/math.abs(sales10)*100
YOYs7 = (sales7-sales11)/math.abs(sales11)*100
YOYs8 = (sales8-sales12)/math.abs(sales12)*100
YOYs9 = (sales9-sales13)/math.abs(sales13)*100
YOYs10 = (sales10-sales14)/math.abs(sales14)*100
YOYs11 = (sales11-sales15)/math.abs(sales15)*100




// Set Array Elements
if  Period == "FQ"
    array.set(sym01S_arr, 11, sales)
    array.set(sym01S_arr, 10, sales1)
    array.set(sym01S_arr, 9, sales2)
    array.set(sym01S_arr, 8, sales3)
    array.set(sym01S_arr, 7, sales4)
    array.set(sym01S_arr, 6, sales5)
    array.set(sym01S_arr, 5, sales6)
    array.set(sym01S_arr, 4, sales7)
    array.set(sym01S_arr, 3, sales8)
    array.set(sym01S_arr, 2, sales9)
    array.set(sym01S_arr, 1, sales10)
    array.set(sym01S_arr, 0, sales11)

else 
    array.set(sym01S_arr, 11, sales+sales1+sales2+sales3)
    array.set(sym01S_arr, 10, sales1+sales2+sales3+sales4)
    array.set(sym01S_arr, 9, sales2+sales3+sales4+sales5)
    array.set(sym01S_arr, 8, sales3+sales4+sales5+sales6)
    array.set(sym01S_arr, 7, sales4+sales5+sales6+sales7)
    array.set(sym01S_arr, 6, sales5+sales6+sales7+sales8)
    array.set(sym01S_arr, 5, sales6+sales7+sales8+sales9)
    array.set(sym01S_arr, 4, sales7+sales8+sales9+sales10)
    array.set(sym01S_arr, 3, sales8+sales9+sales10+sales11)
    array.set(sym01S_arr, 2, sales9+sales10+sales11+sales12)
    array.set(sym01S_arr, 1, sales10+sales11+sales12+sales13)
    array.set(sym01S_arr, 0, sales11+sales12+sales13+sales14)

//



array.set(QOQs_arr, 11, QOQs0)
array.set(QOQs_arr, 10, QOQs1)
array.set(QOQs_arr, 9, QOQs2)
array.set(QOQs_arr, 8, QOQs3)
array.set(QOQs_arr, 7, QOQs4)
array.set(QOQs_arr, 6, QOQs5)
array.set(QOQs_arr, 5, QOQs6)
array.set(QOQs_arr, 4, QOQs7)
array.set(QOQs_arr, 3, QOQs8)
array.set(QOQs_arr, 2, QOQs9)
array.set(QOQs_arr, 1, QOQs10)
array.set(QOQs_arr, 0, QOQs11)

array.set(YOYs_arr, 11, YOYs0)
array.set(YOYs_arr, 10, YOYs1)
array.set(YOYs_arr, 9, YOYs2)
array.set(YOYs_arr, 8, YOYs3)
array.set(YOYs_arr, 7, YOYs4)
array.set(YOYs_arr, 6, YOYs5)
array.set(YOYs_arr, 5, YOYs6)
array.set(YOYs_arr, 4, YOYs7)
array.set(YOYs_arr, 3, YOYs8)
array.set(YOYs_arr, 2, YOYs9)
array.set(YOYs_arr, 1, YOYs10)
array.set(YOYs_arr, 0, YOYs11)

float QOQs = na
float YOYs = na





//Financial Ratios-----------------------------------------------------------------------------------------
ROA = request.financial(syminfo.tickerid,"RETURN_ON_ASSETS", "FY")
ROE = request.financial(syminfo.tickerid,"RETURN_ON_EQUITY", "FY")
EPSt = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "TTM")
TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")
TR = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "TTM")
GR = request.financial(syminfo.tickerid, "REVENUE_ONE_YEAR_GROWTH", "FY")
Cash = request.financial(syminfo.tickerid, "FREE_CASH_FLOW", "FY")/1000000
DY = request.financial(syminfo.tickerid, "DIVIDENDS_YIELD", "FY")
DTE = request.financial(syminfo.tickerid, "DEBT_TO_EQUITY", "FY")
PEGratio = request.financial(syminfo.tickerid, "PEG_RATIO", "FY")
RevGrowth = request.financial(syminfo.tickerid, "REVENUE_ONE_YEAR_GROWTH", "FY")/100
EnterpriseValtoEBITDA = request.financial(syminfo.tickerid, "ENTERPRISE_VALUE_EBITDA", "FY")
EBITDA = request.financial(syminfo.tickerid, "EBITDA", "FY")
OPM = request.financial(syminfo.tickerid, "OPERATING_MARGIN", "FY")
BV = request.financial(syminfo.tickerid, "BOOK_VALUE_PER_SHARE", "FY")

//Calculation-----------------------------------------------------------------------------------------
PriceEarningsRatio = close/EPSt
MarketCap = TSO*close
PriceSalesRatio = MarketCap/TR
PriceBookValueRatio = close/BV
EV = EnterpriseValtoEBITDA*EBITDA
EVtoFCF = EV/(Cash*1000000)


//Financial ratio Definition---------------------------------------------------------------------------
string def_PE = "The price-to-earnings (P/E) ratio is the ratio for valuing a company that measures its current share price relative to its per-share earnings."
string def_PEG = "The price/earnings to growth ratio (PEG ratio) is a stock's price-to-earnings (P/E) ratio divided by the growth rate of its earnings (%) for a specified time period."
string def_PS = "The price-to-sales (P/S) ratio is a valuation ratio that compares a company’s stock price to its revenues."
string def_PB = "Price-to-book value (P/B) is the ratio of the market value of a company's shares (share price) over its book value of equity. The book value of equity, in turn, is the value of a company's assets expressed on the balance sheet."
string def_EVFCF = "The Enterprise Value (EV) to Free Cash Flow (FCF) compares company valuation with its potential to create positive cash flow statements."
string def_OPM = "Operating profit margin (OPM) is a measure of a company's operating efficiency. It is calculated by dividing operating income by net sales. "
string def_DE = "Debt-to-equity (D/E) ratio is used to evaluate a company’s financial leverage and is calculated by dividing a company’s total liabilities by its shareholder equity. "
string def_ROE = "Return on equity (ROE) is a measure of financial performance calculated by dividing net income by shareholders' equity. "
string def_DY = "Dividend Yield represents the ratio of a company's current annual dividend compared to its current share price."

//Truncate to Decimal---------------------------------------------------------------------------------
truncate(number, decimals) => 
    factor = math.pow(10, decimals)
    int(number * factor) / factor 
    
Trunc_1 = truncate(ROA, 2)
Trunc_2 = truncate(ROE, 2)
Trunc_4 = truncate(PriceEarningsRatio, 2)
Trunc_5 = truncate(PEGratio, 2)
Trunc_6 = truncate(PriceSalesRatio, 2)
Trunc_3 = truncate(PriceBookValueRatio, 2)
Trunc_7 = truncate(Cash, 2)
Trunc_8 = truncate(EVtoFCF, 2)
Trunc_9 = truncate(DY, 2)
Trunc_10 = truncate(DTE, 2)
Trunc_11 = truncate(OPM, 2)
//---------------------------------------------------------------------------------------------------------


//colors
color good = color.new(#5ac573, 50)
color bad = color.new(#df6464, 50)
color neutral = color.new(color.blue, 50)
color credits = color.new(color.blue, 50)




// Find the Maximum Number for Plot scale
coeff01  = math.max(math.abs(array.min(sym01_arr)), math.abs(array.max(sym01_arr)))

scalCoef = 30/math.max(coeff01, 0)

coeff01s  = math.max(math.abs(array.min(sym01S_arr)), math.abs(array.max(sym01S_arr)))

scalCoefs = 30/math.max(coeff01s, 0)



// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                   TABLE                                    |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// Get Table Position
table_pos(p) => 
    switch p
        "Top Right"     => position.top_right     
        "Middle Right"  => position.middle_right   
        "Bottom Right"  => position.bottom_right  
        "Top Center"    => position.top_center    
        "Middle Center" => position.middle_center
        "Bottom Center" => position.bottom_center
        "Top Left"      => position.top_left      
        "Middle Left"   => position.middle_left 
        => position.bottom_left


var plt = table.new(position.bottom_left, 1 , 1)

// Find Number of Columns
mxArr  = 12
numCol = mxArr * 2 + 1 < 26 ? 25 : mxArr * 2 + 1

// Set Up Plot Table
plt   := table.new(table_pos(in_plot_pos), numCol+2, 81,
         frame_width  = 0,  frame_color  = borCol,
         border_width = 0,  border_color = color.new(txtCol, 100))

// Plot Cell
pltCell (x, y, w, h, col, tt) =>
    table.cell(plt, x, y, width = w, height = h, bgcolor = col)
    table.cell_set_tooltip(plt, x, y, tt)
    

// Plot Columns eps
pltCol (x, y, k, col) =>
    pltCell(x, y > 0 ? 79 - k : y < 0 ? 79 + k : 79, barWdth, barHigh, 
     color.new(col,   0), "EPS: " + str.tostring(y, "#.## \n")+ "QOQ: "+ str.tostring(QOQ, "#.#")+"%\n"+ "YOY: " + str.tostring(YOY, "#.#") + "%")

// Plot Columns sales
pltCol2 (x, y, k, col) =>
    pltCell(x, y > 0 ? 43 - k : y < 0 ? 43 + k : 43, barWdth, barHigh, 
     color.new(col,   0), "Sales: " + str.tostring(y, "#.##")+"Cr. \n"+ "QOQ: "+ str.tostring(QOQs, "#.#")+"%\n"+ "YOY: " + str.tostring(YOYs, "#.#") + "%")


// Plot Financial Ratio Table
table.cell(plt, 0, 0, "FinMet © Basit", width = barWdth, text_size = "small", text_color = color.white, bgcolor = borCol)
table.merge_cells(plt, 0, 0, numCol+1, 0)
table.cell(plt, 0, 1, "P/E: " +str.tostring(Trunc_4), width = barWdth, text_size = "normal", text_color = color.white, bgcolor = (Trunc_4 > lim_PE) or (Trunc_4 < 0) ? bad:good, tooltip = def_PE)
table.merge_cells(plt, 0, 1, numCol+1, 1)
table.cell(plt, 0, 2, "PEG: " +str.tostring(Trunc_5), width = barWdth, text_color = color.white, bgcolor = (Trunc_5 > lim_PEG) or (Trunc_5 < 0) ? bad:good, tooltip = def_PEG)
table.merge_cells(plt, 0, 2, numCol+1, 2)
table.cell(plt, 0, 3, "P/S: " +str.tostring(Trunc_6), width = barWdth, text_color = color.white, bgcolor = (Trunc_6 > lim_PS) or (Trunc_6 < 0) ? bad:good, tooltip = def_PS)
table.merge_cells(plt, 0, 3, numCol+1, 3)
table.cell(plt, 0, 4, "P/B: " +str.tostring(Trunc_3), width = barWdth, text_color = color.white, bgcolor = (Trunc_3 > lim_PB) or (Trunc_3 < 0) ? bad:good, tooltip = def_PB)
table.merge_cells(plt, 0, 4, numCol+1, 4)
table.cell(plt, 0, 5, "EV/FCF: " +str.tostring(Trunc_8), width = barWdth, text_color = color.white, bgcolor = (Trunc_8 > lim_EVFCF) or (Trunc_8 < 0) ? bad:good, tooltip = def_EVFCF)
table.merge_cells(plt, 0, 5, numCol+1, 5)
table.cell(plt, 0, 6, "OPM: " +str.tostring(Trunc_11) + "%", width = barWdth, text_color = color.white, bgcolor = (Trunc_11 < lim_OPM) or (Trunc_11 < 0) ? bad:good, tooltip = def_OPM)
table.merge_cells(plt, 0, 6, numCol+1, 6)
table.cell(plt, 0, 7, "D/E: " +str.tostring(Trunc_10), width = barWdth, text_color = color.white, bgcolor = (Trunc_10 > lim_DE) or (Trunc_10 < 0) ? bad:good, tooltip = def_DE)
table.merge_cells(plt, 0, 7, numCol+1, 7)
table.cell(plt, 0, 8, "ROE: " +str.tostring(Trunc_2)+ "%", width = barWdth, text_color = color.white, bgcolor = (Trunc_2 < lim_ROE) or (Trunc_2 < 0) ? bad:good, tooltip = def_ROE)
table.merge_cells(plt, 0, 8, numCol+1, 8)
table.cell(plt, 0, 9, "D_Yield: " +str.tostring(Trunc_9)+"%", width = barWdth, text_color = color.white, bgcolor = (Trunc_9 < lim_DVY) or (Trunc_9 < 0) ? bad:good, tooltip = def_DY)
table.merge_cells(plt, 0, 9, numCol+1, 9)


   
// Plot Sales and EPS bars

if barstate.islast
    if mxArr > 0
    
        // Reset Table. 
        for i = 0 to numCol + 1
            for j = 9 to 80
                pltCell(i, j, 0.001, 0.001, color.new(txtCol, 100), "")

//    

        w= 0
        for i = 2 to mxArr * 2 + 1 by 2
            
            //Plot eps Columns 
            if math.round(scalCoef * array.get(sym01_arr, w)) > 0
                for k = 0 to math.round(scalCoef * array.get(sym01_arr, w))
                    QOQ = array.get(QOQ_arr, w)
                    YOY = array.get(YOY_arr, w)
                    pltCol(i, array.get(sym01_arr, w), k, s01Col)
            

            else if math.round(scalCoef * array.get(sym01_arr, w)) < 0    
                for k = 0 to math.round(scalCoef * array.get(sym01_arr, w))
                    QOQ = array.get(QOQ_arr, w)
                    YOY = array.get(YOY_arr, w)
                    pltCol(i, array.get(sym01_arr, w), k, s01Col2)
                
            else 
                QOQ = array.get(QOQ_arr, w)
                YOY = array.get(YOY_arr, w)
                pltCol(i, array.get(sym01_arr, w), 0, color.new(s01Col, 0))

            //plot sales columns
            if math.round(scalCoefs * array.get(sym01S_arr, w)) > 0 
                for k = 0 to math.round(scalCoefs * array.get(sym01S_arr, w))
                    QOQs = array.get(QOQs_arr, w)
                    YOYs = array.get(YOYs_arr, w)
                    pltCol2(i, array.get(sym01S_arr, w), k, s01Cols)
                

            else if math.round(scalCoefs * array.get(sym01S_arr, w)) < 0    
                for k = 0 to math.round(scalCoefs * array.get(sym01S_arr, w))
                    QOQs = array.get(QOQs_arr, w)
                    YOYs = array.get(YOYs_arr, w)
                    pltCol2(i, array.get(sym01S_arr, w), k, s01Col2)
                
            else 
                QOQs = array.get(QOQs_arr, w)
                YOYs = array.get(YOYs_arr, w)
                pltCol2(i, array.get(sym01S_arr, w), 0, color.new(s01Cols, 0))
            
           
            
           
            
            w := w + 1

        

        // Draw X Axis Line (X==0)
       // for i = 2 to numCol 
           // pltCell(i, 43, 0.4, 0.005, borCol, "")

       // for i = 2 to numCol
           // pltCell(i, 78, 0.4, 0.005, borCol, "")

        for i = 10 to 80
            pltCell(0, i, 0.3, 0.005, borCol, "")
        for i = 10 to 80
            pltCell(numCol+1, i, 0.3, 0.005, borCol, "")

if  Period == "FQ"
    table.cell(plt, 0, 44, "Sales (FQ)" , width = barWdth, text_size = "small", text_color = color.white, bgcolor = borCol)
    table.merge_cells(plt, 0, 44, numCol+1, 44)
else
    table.cell(plt, 0, 44, "Sales (TTM)", width = barWdth, text_size = "small", text_color = color.white, bgcolor = borCol)
    table.merge_cells(plt, 0, 44, numCol+1, 44)
    
if  Period == "FQ"  
    table.cell(plt, 0, 80, "EPS (FQ)" + str.format_time(ResultDate, " [dd-MM]" ), width = barWdth, text_size = "small", text_color = color.white, bgcolor = borCol)
    table.merge_cells(plt, 0, 80, numCol+1, 80)
else
    table.cell(plt, 0, 80, "EPS (TTM)" + str.format_time(ResultDate, " [dd-MM]" ) , width = barWdth, text_size = "small", text_color = color.white, bgcolor = borCol)
    table.merge_cells(plt, 0, 80, numCol+1, 80)


       

    
        

   