FCF ROCE
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
//
// This is a rough version of the Return on Capital Employed

//@version=4
study("FCF ROCE", precision=2)

// 
var fcf_ttm = float(na)
var fcf_q1 = 0.0
var fcf_q2 = 0.0
var fcf_q3 = 0.0
var fcf_q4 = 0.0

fcf_series_filled = financial(syminfo.tickerid, "FREE_CASH_FLOW", "FQ", false)
fcf_series_fq = financial(syminfo.tickerid, "FREE_CASH_FLOW", "FQ", true)
fcf_series_fy = financial(syminfo.tickerid, "FREE_CASH_FLOW", "FY", false)

if not na(fcf_series_fq[0])
    fcf_q4 := fcf_q3
    fcf_q3 := fcf_q2
    fcf_q2 := fcf_q1
    fcf_q1 := fcf_series_fq[0]
    fcf_ttm := fcf_q1 + fcf_q2 + fcf_q3 + fcf_q4
else if na(fcf_series_filled[0]) and not na(fcf_series_fy[0])
    fcf_ttm := fcf_series_fy[0]

tot_assets = na(financial(syminfo.tickerid, "TOTAL_ASSETS", "FQ")) ? financial(syminfo.tickerid, "TOTAL_ASSETS", "FY") : financial(syminfo.tickerid, "TOTAL_ASSETS", "FQ")
cur_liabilities = na(financial(syminfo.tickerid, "TOTAL_CURRENT_LIABILITIES", "FQ")) ? financial(syminfo.tickerid, "TOTAL_CURRENT_LIABILITIES", "FY") : financial(syminfo.tickerid, "TOTAL_CURRENT_LIABILITIES", "FQ")
tot_shares = na(financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")) ? financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FY") : financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")
stock = close
capital_employed = tot_assets[0] - cur_liabilities[0]
fcf_share = (fcf_ttm / tot_shares)
fcf_yield = (fcf_share/stock)*100

fcf_roce = (fcf_ttm / capital_employed)*100

c = if fcf_roce >= 20
    color.new(color.green, 75)
else if fcf_roce > 0
    color.new(color.yellow, 75)
else
    color.new(color.red, 75)

plot(fcf_roce, color=c, style=plot.style_area, histbase=0)
plot(fcf_yield)
