Valuation Metrics Table (P/S, P/E, etc.)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jvorndran311

//@version=5
indicator("Valuation Metrics Table", overlay = true)

yearsLookbackPS = input.int(defval = 5, title = "Amount of years to calculate median")
i_PS = input.bool(defval = true, title = 'Price to sales')
i_PSM = input.bool(defval = true, title = 'Price to sales median')
i_PSD = input.bool(defval = true, title = 'Price to sales discount to median')
i_PE = input.bool(defval = true, title = 'Price to earnings')
i_PEM = input.bool(defval = true, title = 'Price to earnings median')
i_PED = input.bool(defval = true, title = 'Price to earnings discount to median')
i_FCF = input.bool(defval = true, title = 'Free cash flow per share')
i_DE = input.bool(defval = true, title = 'Debt to Equity')
i_PR = input.bool(defval = false, title = 'Payout Ratio')
i_EV = input.bool(defval = true, title = 'EV/EBITDA')


var table myTable = table.new(position = position.bottom_right, columns = 10, rows = 10)

table.set_border_color(myTable, color.white)
table.set_border_width(myTable, 2)
table.set_frame_color(myTable, color.white)
table.set_frame_width(myTable, 2)


float rev = request.financial(symbol = syminfo.tickerid, financial_id = "TOTAL_REVENUE", period = 'TTM')

float totalSharesOut = request.financial(symbol = syminfo.tickerid, financial_id = "TOTAL_SHARES_OUTSTANDING", period = "FY")

float mktCap = close * totalSharesOut

float priceSalesRatio = ((close * totalSharesOut) / rev)

//252 trading days a year
float medianPriceSales = ta.median(priceSalesRatio, 252 * yearsLookbackPS)
float meanPriceSales = math.sum(priceSalesRatio, 252 * yearsLookbackPS) / (252 * yearsLookbackPS)
float std = ta.stdev(priceSalesRatio, 252 * yearsLookbackPS)

float discountMedian = (priceSalesRatio / medianPriceSales)
float discountMean = (priceSalesRatio / meanPriceSales)

// plot(priceSalesRatio, color = color.white, style = plot.style_line, linewidth = 2)
// plot(medianPriceSales, color = color.aqua, linewidth = 2)
// plot(std + meanPriceSales, color = color.orange, linewidth = 2)
// plot(meanPriceSales - std, color = color.orange, linewidth = 2)
if i_PSD
    table.cell(myTable, 0, 2, text = "P/S Discount To 5yr Median", text_color = color.white)
    table.cell(myTable, 1, 2, str.tostring(discountMedian < 1 ? 100 - (discountMedian * 100) : (discountMedian * 100), format = '#.###') + '%', text_color = color.white)

if i_PS
    table.cell(myTable, 0, 0, text = "P/S", text_color = color.white )
    table.cell(myTable, 1, 0, str.tostring(priceSalesRatio, format = '#.###'), text_color = color.white)
if i_PSM
    table.cell(myTable, 0, 1, text = "P/S Median (Default 5 yrs)", text_color = color.white)
    table.cell(myTable, 1, 1, str.tostring(medianPriceSales, format = '#.###'), text_color = color.white)

//P/E


float PE = close / request.financial(syminfo.tickerid, financial_id = 'EARNINGS_PER_SHARE_DILUTED', period = 'TTM')

float medianPE = ta.median(PE, 252 * yearsLookbackPS)

float discountPE = PE / medianPE

discountPE := discountPE < 1 ? 100 - (discountPE * 100) : (discountPE * 100)

if i_PE
    table.cell(myTable, 0, 3, text = "P/E", text_color = color.white)
    table.cell(myTable, 1, 3, text = str.tostring(PE, '#.###'), text_color = color.white)

table.set_bgcolor(myTable, bgcolor = color.black)

if i_PEM
    table.cell(myTable, 0, 4, text = "P/E Median (Default 5 yrs)", text_color = color.white)
    table.cell(myTable, 1, 4, text = str.tostring(medianPE, '#.###'), text_color = color.white)

if i_PED
    table.cell(myTable, 0, 5, text = "P/E Discount To 5yr Median", text_color = color.white)
    table.cell(myTable, 1, 5, text = str.tostring(discountPE, '#.###') + '%', text_color = color.white)
//P/E

//FCF 

float FCFperShare = request.financial(syminfo.tickerid, 'FREE_CASH_FLOW', period = 'FQ') / totalSharesOut
if i_FCF
    table.cell(myTable, 0, 6, text = "FCF/Share", text_color = color.white)
    table.cell(myTable, 1, 6, text = str.tostring(FCFperShare, '#.###'), text_color = color.white)

//FCF 

// D/E

float DE = request.financial(syminfo.tickerid, 'DEBT_TO_EQUITY', period = 'FY')

if i_DE
    table.cell(myTable, 0, 7, text = "Debt/Equity", text_color = color.white)
    table.cell(myTable, 1, 7, text = str.tostring(DE, '#.###'), text_color = color.white)

// D/E


 // PEG

// EPS1YR = request.financial(syminfo.tickerid, 'EARNINGS_PER_SHARE_DILUTED', period = 'TTM')[252]
// EPS = request.financial(syminfo.tickerid, 'EARNINGS_PER_SHARE_DILUTED', period = 'TTM')

// PEG = (close / EPS) / (((EPS / EPS1YR)) * 100)

// plot(PEG)

// PEG

//Estimates

// float EPSest = request.financial(syminfo.tickerid, "EARNINGS_ESTIMATE", "FQ")
// float revEst = request.financial(syminfo.tickerid, "SALES_ESTIMATES", "FQ")

// plot(EPSest)

// float projEpsGrowth = (EPSest / request.financial(syminfo.tickerid, financial_id = 'EARNINGS_PER_SHARE_BASIC', period = 'FQ')) * 100

// table.cell(myTable, 0, 8, "Projected EPS Growth", text_color = color.white)
// table.cell(myTable, 1, 8,str.tostring(projEpsGrowth - 100, '#.###') + '%', text_color = color.white)

//Estimates


//Div Payout Ratio

float payoutRatio = request.financial(syminfo.tickerid, "DIVIDEND_PAYOUT_RATIO", period = "FQ")

if i_PR
    table.cell(myTable, 0, 8, text = "Payout Ratio", text_color = color.white)
    table.cell(myTable, 1, 8, str.tostring(payoutRatio, '#.###') + '%', text_color = color.white)

//Div Payout Ratio

// EV/EBITA

float EV = request.financial(syminfo.tickerid, "ENTERPRISE_VALUE_EBITDA", period = "FQ")

if i_EV
    table.cell(myTable, 0, 9, "EV/EBITDA", text_color = color.white)
    table.cell(myTable, 1, 9, str.tostring(EV, '#.###'), text_color = color.white)

//EV/EBITA






