Grenblatt Magic Formula
// 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("Magic Formula", precision=2)

var ebit_ttm = float(na)
var ebit_q1 = 0.0
var ebit_q2 = 0.0
var ebit_q3 = 0.0
var ebit_q4 = 0.0

ebit_series_filled = financial(syminfo.tickerid, "EBIT", "FQ", false)
ebit_series_fq = financial(syminfo.tickerid, "EBIT", "FQ", true)
ebit_series_fy = financial(syminfo.tickerid, "EBIT", "FY", false)

if not na(ebit_series_fq[0])
    ebit_q4 := ebit_q3
    ebit_q3 := ebit_q2
    ebit_q2 := ebit_q1
    ebit_q1 := ebit_series_fq[0]
    ebit_ttm := ebit_q1 + ebit_q2 + ebit_q3 + ebit_q4
else if na(ebit_series_filled[0]) and not na(ebit_series_fy[0])
    ebit_ttm := ebit_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]
roc = (ebit_ttm / capital_employed)*100
mkt_cap = tot_shares * close
ev = mkt_cap + debt + min_interest - cash
earnings_yield = (ebit_ttm / ev)*100

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

plot(roc, color=c, style=plot.style_area, histbase=0)
plot(earnings_yield, linewidth=2)
