CROCE
// 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("CROCE", 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]

cur_assets = na(financial(syminfo.tickerid, "TOTAL_CURRENT_ASSETS", "FQ")) ? financial(syminfo.tickerid, "TOTAL_CURRENT_ASSETS", "FY") : financial(syminfo.tickerid, "TOTAL_CURRENT_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")
cash_stinv = na(financial(syminfo.tickerid, "CASH_N_SHORT_TERM_INVEST", "FQ")) ? financial(syminfo.tickerid, "CASH_N_SHORT_TERM_INVEST", "FY") : financial(syminfo.tickerid, "CASH_N_SHORT_TERM_INVEST", "FQ")
st_debt = na(financial(syminfo.tickerid, "SHORT_TERM_DEBT", "FQ")) ? financial(syminfo.tickerid, "SHORT_TERM_DEBT", "FY") : financial(syminfo.tickerid, "SHORT_TERM_DEBT", "FQ")
ppe = na(financial(syminfo.tickerid, "PPE_TOTAL_NET", "FQ")) ? financial(syminfo.tickerid, "PPE_TOTAL_NET", "FY") : financial(syminfo.tickerid, "PPE_TOTAL_NET", "FQ")
debt = na(financial(syminfo.tickerid, "TOTAL_DEBT", "FQ")) ? financial(syminfo.tickerid, "TOTAL_DEBT", "FY") : financial(syminfo.tickerid, "TOTAL_DEBT", "FQ")
min_interest = na(financial(syminfo.tickerid, "MINORITY_INTEREST", "FQ")) ? financial(syminfo.tickerid, "MINORITY_INTEREST", "FY") : financial(syminfo.tickerid, "MINORITY_INTEREST", "FQ")
cash = na(financial(syminfo.tickerid, "CASH_N_EQUIVALENTS", "FQ")) ? financial(syminfo.tickerid, "CASH_N_EQUIVALENTS", "FY") : financial(syminfo.tickerid, "CASH_N_EQUIVALENTS", "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")
capital_employed = max(0 , ((cur_assets[0]-cash_stinv[0]) - (cur_liabilities[0]-st_debt))) + ppe[0]
croce = (fcf_ttm / capital_employed)*100
mkt_cap = tot_shares * close
ev = mkt_cap + debt + min_interest - cash
cash_yield = (fcf_ttm / ev)*100

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

plot(croce, color=c, style=plot.style_area, histbase=0)
plot(cash_yield, linewidth=2)
