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

// =======================================================================
// This indicator shows a summary of financial data of the selected stock,
// over a selectable period of fiscal years
// =======================================================================

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

// -----------------------------------------
// inputs for indicator
// 
var metricGrp   = " === Select Metric to display === "
var fontSizeGrp = " === Select Font size for table === "

period       = input.int(defval=5, title="Fiscal Years") // number of fiscal periods to  evaluate
showPE       = input.bool(true,"P/E","Price to earnings ratio","r1",metricGrp)
showPS       = input.bool(true,"P/S","Price to sales ratio","r1",metricGrp)
showPFCF     = input.bool(true,"P/FCF","price to FreeCashFlow ratio","r1",metricGrp)
showEY       = input.bool(true,"Earnigs Yield","","r2",metricGrp)
showSALES    = input.bool(true,"Sales","","r2",metricGrp)
showNI       = input.bool(true,"Net income","","r3",metricGrp)
showNI2Sales = input.bool(true,"Net income / Sales","","r3",metricGrp)
showLTD      = input.bool(true,"Long term debt","","r4",metricGrp)
showLTD2FCF  = input.bool(true,"LTD / FCF","Long term debt / Free cash flow","r4",metricGrp)
showShares   = input.bool(true,"Shares outstanding","Number of shares outstanding. Decreasing number is a positive sign for investors","r5",metricGrp)
showFCF      = input.bool(true,"Free Cash Flow","","r5",metricGrp)

fs = input.string(size.auto,"Table Font Size",[size.auto,size.tiny,size.small,size.normal,size.large,size.huge ],"",fontSizeGrp)

// ===========================================
// Display array cells in table columns
//
// Parameters:
// T   : reference to the table
// ar  : array containing the data to display
// N   : number of columns to display 
// R   : Number of rows in table
// C   : Start column for display
// TS  : Text size 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
arr2col(T,ar,N,R,C,TS) =>
    var string s =""
    for i = 1 to N
        val = array.get(ar,array.size(ar)-i)
        s := str.tostring( val,"####.##")
        var cc = color.yellow
        if val < 0
            cc := color.orange
        else
            cc := color.yellow
        table.cell(table_id=T, column=C-1+i, row=R, text=s, bgcolor=cc,text_halign=text.align_right,text_size=TS )
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// =======================================================================================================================
// the table containing the data to visualize
//
var T_VV = table.new(position.top_right,period+2,14,bgcolor = color.yellow, border_width = 1,border_color=color.gray)
var float marker = 0

// -------------------------------------------------------------
// arrays containing data to evaluate, one entry per fiscal year
//
var float[] p2e     = array.new_float(0)
var float[] p2s     = array.new_float(0)
var float[] p2fcf   = array.new_float(0)
var float[] fcf     = array.new_float(0)
var float[] tso     = array.new_float(0)
var float[] n2s     = array.new_float(0)
var float[] tr      = array.new_float(0)
var float[] p       = array.new_float(0)
var float[] y       = array.new_float(0)
var float[] ey      = array.new_float(0)
var float[] ni      = array.new_float(0)
var float[] ltd     = array.new_float(0)
var float[] ltd2fcf = array.new_float(0)

P = 0.5 * (open + close) // evaluate mean value of close,open price

LTD          = request.financial(syminfo.tickerid, "LONG_TERM_DEBT", "FY")
FCF          = request.financial(syminfo.tickerid, "FREE_CASH_FLOW", "FY")
FCFttm       = request.financial(syminfo.tickerid, "FREE_CASH_FLOW", "TTM")
TSO          = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FY")
TR           = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FY")
TRttm        = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "TTM")
ROIC         = request.financial(syminfo.tickerid, "RETURN_ON_INVESTED_CAPITAL", "FY")
NetIncome    = request.financial(syminfo.tickerid, "NET_INCOME", "FY")
NetIncomeTTM = request.financial(syminfo.tickerid, "NET_INCOME", "TTM")
EPS          = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "FY")
EPSttm       = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "TTM")

EarningsYield          = (EPS / P) * 100
EarningsYieldTTM       = (EPSttm / P) * 100
PriceEarningsRatio     = P / EPS
PriceEarningsRatioTTM  = P / EPSttm
PriceSalesRatio        = P / ( TR / TSO)

// ------------------------------------
// memorize data if fiscal year changes
//
if(marker != EPS)
    marker := EPS
    array.push(p2e,PriceEarningsRatio)
    array.push(p2s,PriceSalesRatio)
    array.push(p2fcf,P/FCF*TSO)
    array.push(fcf,FCF*0.000001)
    array.push(tso,TSO*0.000001)
    array.push(n2s,NetIncome/TR)
    array.push(tr,TR * 0.000001)
    array.push(p,P)
    array.push(y,year)
    array.push(ey,EarningsYield)
    array.push(ni,NetIncome*1.0e-6)
    array.push(ltd2fcf,LTD/FCF)
    array.push(ltd,LTD*1.0e-6)

// --------------------------------------------
// render Table only when last bar is evaluated
// and time frame is greater or equal "DAY"
//
if timeframe.in_seconds() < timeframe.in_seconds("D")
    table.cell(table_id = T_VV, column = 0, row = 0, text = "ValueView: Timeframe must be greater or equal DAY", text_color=color.white, bgcolor = color.red, text_halign=text.align_left, text_size=size.large )
else if barstate.islast
    var numY    = array.size(y)
    var numCols = period + 1
    var k = 0
    
    array.push(p2e,PriceEarningsRatioTTM)
    array.push(p2s,PriceEarningsRatio)
    array.push(p2fcf,P/FCF*TSO)
    array.push(fcf,FCFttm*0.000001)
    array.push(tso,TSO*0.000001)
    array.push(n2s,NetIncomeTTM/TRttm)
    array.push(tr,TRttm * 0.000001)
    array.push(p,P)
    array.push(y,year)
    array.push(ey,EarningsYieldTTM)
    array.push(ni,NetIncomeTTM*1.0e-6)
    array.push(ltd2fcf,LTD/FCFttm)
    array.push(ltd,LTD*1.0e-6)

    
    if numY <= period
        numCols := numY
        
    table.cell(table_id = T_VV, column = 0, row = k, text = "Metric", text_halign=text.align_left, text_size=fs )
    arr2col(T_VV,y,numCols,k,1,TS=fs)
    table.cell(table_id = T_VV, column = 1, row = k, text = "TTM", text_halign=text.align_center, text_size=fs )
    k := k+1
    table.cell(table_id = T_VV, column = 0, row = k, text = "Price (FY end)", text_halign=text.align_left, text_size=fs  )        
    arr2col(T_VV,p,numCols,k,1,TS=fs)
    k := k+1

    if showPE
        table.cell(table_id = T_VV, column = 0, row = k, text = "P / E", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,p2e,numCols,k,1,TS=fs)
        k := k+1
        
    if showPS
        table.cell(table_id = T_VV, column = 0, row = k, text = "P / S", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,p2s,numCols,k,1,TS=fs)
        k := k+1
    
    if showPFCF
        table.cell(table_id = T_VV, column = 0, row = k, text = "P / FCF", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,p2fcf,numCols,k,1,TS=fs)
        k := k+1

    if showEY
        table.cell(table_id = T_VV, column = 0, row = k, text = "Earnings yield (%)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,ey,numCols,k,1,TS=fs)
        k := k+1

    if showSALES
        table.cell(table_id = T_VV, column = 0, row = k, text = "Sales (M)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,tr,numCols,k,1,TS=fs)
        k := k+1

    if showNI
        table.cell(table_id = T_VV, column = 0, row = k, text = "Net Income (M)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,ni,numCols,k,1,TS=fs)
        k := k+1
    
    if showNI2Sales
        table.cell(table_id = T_VV, column = 0, row = k, text = "Net Income/Sales", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,n2s,numCols,k,1,TS=fs)
        k := k+1
    
    if showLTD2FCF
        table.cell(table_id = T_VV, column = 0, row = k, text = "Debt / FCF", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,ltd2fcf,numCols,k,1,TS=fs)
        k := k+1

    if showLTD
        table.cell(table_id = T_VV, column = 0, row = k, text = "Long Term Debt (M)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,ltd,numCols,k,1,TS=fs)
        k := k+1
    
    if showFCF
        table.cell(table_id = T_VV, column = 0, row = k, text = "Free Cash Flow (M)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,fcf,numCols,k,1,TS=fs)
        k := k+1
    
    if showShares
        table.cell(table_id = T_VV, column = 0, row = k, text = "Shares out (M)", text_halign=text.align_left, text_size=fs  )        
        arr2col(T_VV,tso,numCols,k,1,TS=fs)
        k := k+1
