Times-Revenue (Fundamental Metric)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jcode773

//@version=5
indicator("Times Revenue")
TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")
MarketCap = TSO * close
TRY = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FY")
TRQ = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")

TVY = MarketCap / TRY


plot(TVY, "Annual", color.red)
qftBarTime = timeframe.change('3M')

counter = 0
number = 1
max = 4
total= 0.0
latest = TRQ[0]
prices = array.new_float(4,-1)
array.set(prices, 0, latest)

while number < max
    if (na(TRQ[counter]))
        break
    else if TRQ[counter] != latest
        array.set(prices, number, TRQ[counter])        
        number:= number + 1
        latest := TRQ[counter]
        
    counter := counter + 1

TVQ = MarketCap / (array.get(prices,0) + array.get(prices,1) + array.get(prices,2) + array.get(prices,3))
plot(TVQ, "Past 4 Quarters", color.black)

//test = str.tostring(array.get(prices,3))
//test = str.tostring(bar_index)
//if (barstate.islast)
//    label.new(bar_index, 0, "ex: " +  test)

sTV = str.tostring(TVQ[0])
sTY = str.tostring(TVY[0])
if barstate.islast
    var label = label.new(x=bar_index, y=TVQ[0], yloc = yloc.price, color=color.black, textcolor=color.white, text=sTV)
    label.set_style(label, label.style_label_lower_left)
    var red = label.new(x=bar_index, y=TVY[0], yloc = yloc.price, color=color.black, textcolor=color.red, text=sTY)
    label.set_style(red, label.style_label_upper_right)
