Financial Ratios Fundamental Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
//exlux99

strategy('Financial Ratios', overlay=true,initial_capital = 1000000, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.1)


//Current ratio: Current Assets / Current Liabilities
TOTAL_CURRENT_ASSETS = request.financial(syminfo.tickerid, "TOTAL_CURRENT_ASSETS", "FQ")
TOTAL_CURRENT_LIABILITIES = request.financial(syminfo.tickerid, "TOTAL_CURRENT_LIABILITIES", "FQ")
current_ratio =  TOTAL_CURRENT_ASSETS/ TOTAL_CURRENT_LIABILITIES
// plot(current_ratio)

//Quick ratio (Acid-test ratio): (Current Assets – Inventories – Prepaid Expenses) / Current Liabilities
PREPAID_EXPENSES = request.financial(syminfo.tickerid, "PREPAID_EXPENSES", "FQ")
INVENTORY_FINISHED_GOODS = request.financial(syminfo.tickerid, "INVENTORY_FINISHED_GOODS", "FQ")
quick_ratio = (TOTAL_CURRENT_ASSETS - INVENTORY_FINISHED_GOODS - PREPAID_EXPENSES) / TOTAL_CURRENT_LIABILITIES
// plot(quick_ratio, color=color.white)

//Cash ratio: Cash and cash equivalents / Current Liabilities
CASH_N_EQUIVALENTS = request.financial(syminfo.tickerid, "CASH_N_EQUIVALENTS", "FQ")
cash_ratio = CASH_N_EQUIVALENTS / TOTAL_CURRENT_LIABILITIES
// plot(cash_ratio, color=color.orange)


long_liquidity =  current_ratio > current_ratio[1] and quick_ratio > quick_ratio[1]  //and cash_ratio > cash_ratio[1]
long_exit_liquidity =  current_ratio < current_ratio[1] and quick_ratio < quick_ratio[1] //or cash_ratio < cash_ratio[1]
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Leverage ratios
//Debt ratio: Total Debt / Total Assets
TOTAL_DEBT= request.financial(syminfo.tickerid, "TOTAL_DEBT", "FQ")
TOTAL_ASSETS = request.financial(syminfo.tickerid, "TOTAL_ASSETS", "FQ")
debt_ratio = TOTAL_DEBT / TOTAL_ASSETS
//plot(debt_ratio)

//Debt to equity ratio: Total Debt / Total Equity
TOTAL_EQUITY= request.financial(syminfo.tickerid, "TOTAL_EQUITY", "FQ")
debt_equity_ratio = TOTAL_DEBT / TOTAL_EQUITY
//plot(debt_equity_ratio,color=color.white)

//Interest coverage ratio: EBIT / Interest expenses
EBIT = request.financial(syminfo.tickerid, "EBIT", "FQ")
INTEREST_EXPENSE_ON_DEBT = request.financial(syminfo.tickerid, "INTEREST_EXPENSE_ON_DEBT", "FQ")

interest_ratio = EBIT / INTEREST_EXPENSE_ON_DEBT
//plot(interest_ratio, color=color.orange)


long_leverage =  debt_ratio < debt_ratio[1] and debt_equity_ratio< debt_equity_ratio[1] //or interest_ratio < interest_ratio[1]
long_exit_leverage =  debt_ratio > debt_ratio[1] and debt_equity_ratio > debt_equity_ratio[1] //or interest_ratio > interest_ratio[1] 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Efficiency ratios

//Asset turnover ratio: Net sales / Average total assets
SALES_OF_BUSINESS =request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")
asset_turnover_ratio = SALES_OF_BUSINESS / TOTAL_ASSETS
// plot(asset_turnover_ratio)

//Inventory turnover: Cost of goods sold / Average value of inventory
// COST_OF_GOODS= request.financial(syminfo.tickerid, "COST_OF_GOODS", "FQ")
// TOTAL_INVENTORY = request.financial(syminfo.tickerid, "TOTAL_INVENTORY", "FQ")
// inventory_turnover = COST_OF_GOODS/ TOTAL_INVENTORY
INVENT_TO_REVENUE = request.financial(syminfo.tickerid, "INVENT_TO_REVENUE", "FQ")
// plot(INVENT_TO_REVENUE,color=color.white)

//Payables turnover ratio: Cost of Goods sold (or net credit purchases) / Average Accounts Payable
COST_OF_GOODS = request.financial(syminfo.tickerid, "COST_OF_GOODS", "FQ")
ACCOUNTS_PAYABLE= request.financial(syminfo.tickerid, "ACCOUNTS_PAYABLE", "FQ")
payable_turnover = COST_OF_GOODS / ACCOUNTS_PAYABLE

// plot(payable_turnover,color=color.orange)

//Receivables turnover ratio: Net credit sales / Average accounts receivable

long_Efficiency  = asset_turnover_ratio > asset_turnover_ratio[1]  and payable_turnover > payable_turnover[1] //and INVENT_TO_REVENUE > INVENT_TO_REVENUE[1]
long_exit_Efficiency = asset_turnover_ratio < asset_turnover_ratio[1] and payable_turnover < payable_turnover[1]//  and INVENT_TO_REVENUE < INVENT_TO_REVENUE[1]

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Profitability  ratios
//Gross margin: Gross profit / Net sales
GROSS_PROFIT = request.financial(syminfo.tickerid, "GROSS_PROFIT", "FQ")
SALES          = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ")
gross_margin =  GROSS_PROFIT / SALES
// plot(gross_margin)

//Operating margin: Operating income / Net sales
OPER_INCOME = request.financial(syminfo.tickerid, "OPER_INCOME", "FQ")
operating_margin = OPER_INCOME/ SALES
// plot(operating_margin, color=color.white)

//Return on assets (ROA): Net income / Total assets
NET_INCOME= request.financial(syminfo.tickerid, "NET_INCOME", "FQ")
roa = NET_INCOME/TOTAL_ASSETS
//plot(roa, color=color.orange)
 
//Return on equity (ROE): Net income / Total equity
roe = NET_INCOME / TOTAL_EQUITY
//plot(roe, color=color.purple)

long_profitability =  (gross_margin > gross_margin[1] and operating_margin>operating_margin[1] )//and roe > roe[1] and roa > roa[1] ) // or ( roe > roe[1] and roa > roa[1]  ) 
long_exit_profitability =  (gross_margin < gross_margin[1] and operating_margin<operating_margin[1] ) //or roe < roe[1] or roa < roa[1])  // or ( roe < roe[1] and roa < roa[1])

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//5)   Market Value ratios
//Earnings per share ratio (EPS): (Net Income – Preferred Dividends) / End-of-Period Common Shares Outstanding
PREFERRED_DIVIDENDS = request.financial(syminfo.tickerid, "PREFERRED_DIVIDENDS", "FQ")
TOTAL_SHARES_OUTSTANDING = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")
eps_ratio = (NET_INCOME - PREFERRED_DIVIDENDS)/TOTAL_SHARES_OUTSTANDING
// plot(eps_ratio)

// //Price earnings ratio (P/E): Share price / Earnings per share
// EPS = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE", "FQ")
// closex = request.security(syminfo.tickerid, "M", close)
// PE_RATIO = closex / eps_ratio
// plot(PE_RATIO,color=color.white) 

//Book value per share ratio: (Total Equity – Preferred Equity) / Total shares outstanding
COMMON_EQUITY_TOTAL = request.financial(syminfo.tickerid, "COMMON_EQUITY_TOTAL", "FQ")
preferred_equity = TOTAL_EQUITY - COMMON_EQUITY_TOTAL
TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")

book_value = (TOTAL_EQUITY - preferred_equity)/TSO
// plot(book_value, color=color.white)


//Dividend yield ratio: Dividend per share / Share price

DIVIDENDS_YIELD = request.financial(syminfo.tickerid, "DIVIDENDS_YIELD", "FQ")
// plot(DIVIDENDS_YIELD,color=color.orange)



long_market = DIVIDENDS_YIELD > DIVIDENDS_YIELD[1] and eps_ratio > eps_ratio[1] and book_value > book_value[1]
long_exit_market =   DIVIDENDS_YIELD < DIVIDENDS_YIELD[1] and eps_ratio < eps_ratio[1] and book_value < book_value[1]


//and (current_ratio > current_ratio[1] )
//and interest_ratio < interest_ratio[1]
//and payable_turnover > payable_turnover[1]
//and gross_margin > gross_margin[1]

//long_tot = (long_liquidity or long_leverage or long_Efficiency or long_profitability )   // or long_market 
long_tot = current_ratio > current_ratio[1] or interest_ratio < interest_ratio[1] or payable_turnover > payable_turnover[1] or gross_margin > gross_margin[1]
long_tot_exit =  current_ratio < current_ratio[1] or interest_ratio > interest_ratio[1] or payable_turnover < payable_turnover[1] or gross_margin > gross_margin[1]

// plot(current_ratio)
// plot(interest_ratio, color=color.white)
// plot(payable_turnover, color=color.orange)
// plot(gross_margin, color=color.purple)

// DEBT_TO_REVENUE = request.financial(syminfo.tickerid, "SPRINGATE_SCORE", "FQ")
// plot(DEBT_TO_REVENUE)


strategy.entry("long",strategy.long,when= ( long_tot or long_tot[1]  ))
strategy.close("long",when= long_tot_exit )


