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

//@version=5
indicator('Price Ratios', overlay=true)

Position = input.string(title='Table Position', defval=position.top_center, options=[position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center, position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right], group='Table Settings', inline='1')
textSize = input.string(title='Text Size', defval=size.small, options=[size.huge, size.large, size.normal, size.small, size.tiny, size.auto], group='Table Settings', inline='1')

headerBG = input.color(color.teal, title='Header', group='Colors', inline='gh')
headerText = input.color(color.white, title='', group='Colors', inline='gh')
valueBG = input.color(color.silver, title='Value', group='Colors', inline='gh')
valueText = input.color(color.black, title='', group='Colors', inline='gh')

f_secureSecurity(_symbol, _res, _src, _offset) =>
    request.security(_symbol, _res, _src[_offset], lookahead=barmerge.lookahead_on)
f_getFinancials(financial_id, period) =>
    request.financial(syminfo.tickerid, financial_id, period)
f_getColorByScore(score) =>
    score == 2 ? color.green : score == 1 ? color.lime : score == 0 ? color.silver : score == -1 ? color.orange : score == -2 ? color.red : color.purple

var headerArray = array.new_string(0)
var valueArray = array.new_float(0)

add_financial_to_array(header, value) =>
    array.push(headerArray, header)
    array.push(valueArray, value)

f_getOptimalFinancialTTM(financial_id) =>
    f_getFinancials(financial_id, syminfo.currency == 'USD' ? 'TTM' : 'FY')

f_getOptimalFinancialBasic(financial_id) =>
    f_getFinancials(financial_id, syminfo.currency == 'USD' ? 'FQ' : 'FY')

optimalReso = syminfo.currency == 'USD' ? '3M' : '12M'
eps = f_getOptimalFinancialTTM('EARNINGS_PER_SHARE_BASIC')
pe = close / eps

revenue = f_getOptimalFinancialTTM('TOTAL_REVENUE')
tso = request.financial(syminfo.tickerid, 'TOTAL_SHARES_OUTSTANDING', 'FQ')
MarketCap = tso * close
ps = MarketCap / revenue

bps = f_getOptimalFinancialBasic('BOOK_VALUE_PER_SHARE')
pb = close / eps

pfcf = f_getOptimalFinancialBasic('PRICE_TO_FREE_CASH_FLOW')
ptb = f_getOptimalFinancialBasic('PRICE_TO_TANGIBLE_BOOK')

pef = f_getOptimalFinancialBasic('PRICE_EARNINGS_FORWARD')
psf = f_getOptimalFinancialBasic('PRICE_SALES_FORWARD')

orientation = Position == position.bottom_center or Position == position.middle_center or Position == position.top_center ? 2 : Position == position.bottom_left or Position == position.middle_left or Position == position.top_left ? 1 : 3

if barstate.islast
    var financialTable = table.new(position=Position, columns=10, rows=10, border_width=1)

    columnHeaderStart = 0
    rowHeaderStart = 0

    columnValueStart = orientation == 2 ? 0 : 1
    rowValueStart = orientation == 2 ? 1 : 0

    columnIncrement = orientation == 2 ? 1 : 0
    rowIncrement = orientation == 2 ? 0 : 1

    add_financial_to_array('Price To Earnings', pe)
    add_financial_to_array('Price To Sales', ps)
    add_financial_to_array('Price To Free Cash Flow', pfcf)
    add_financial_to_array('Price To Book', pb)
    add_financial_to_array('Price To Tangible Book', ptb)
    add_financial_to_array('Price To Earnings Forward', pef)
    add_financial_to_array('Price To Sales Forward', psf)

    for i = 0 to array.size(headerArray) - 1 by 1
        table.cell(table_id=financialTable, column=columnHeaderStart + i * columnIncrement, row=rowHeaderStart + i * rowIncrement, text=array.get(headerArray, i), bgcolor=headerBG, text_color=headerText, text_size=textSize)
        table.cell(table_id=financialTable, column=columnValueStart + i * columnIncrement, row=rowValueStart + i * rowIncrement, text=str.tostring(math.round(array.get(valueArray, i), 2)), bgcolor=valueBG, text_color=valueText, text_size=textSize)


