During this post, we are going to build an investment model to find out attractive stocks based on financial ratios using Python. We will screen all technology related stocks in the Nasdaq exchange. Then, we will get the main financial ratios for each stock. Finally, based on a custom ranking criteria, we will build a model to find the most attractive stocks.
Let’s start with the Python code to build our investment model:
As in my previous posts, I will be using financialmodelingprep API which offers a few free API calls per month upon registration. Below are the main steps that we will follow to build our investment model based on financial ratios:
As already covered in one of my previous posts (i.e. see how to find high dividend paying stocks), we will extract and append each of the company tickers trading in NASDAQ into a Python list. Note that you will need to create an account in financialmodelingprep in order to get an API with a few free API calls per day.
It is a good idea to select companies operating in the same sector and with similar size. That will ensure that the companies can be compared between them. Therefore, we select companies operating in the Technological sector with a market capitalisation of more than $10 billion.
import requests
import pandas as pd
companies = []
demo = 'your api key'
marketcap = str(1000000000)
url = (f'https://financialmodelingprep.com/api/v3/stock-screener?marketCapMoreThan={marketcap}&betaMoreThan=1&volumeMoreThan=10000§or=Technology&exchange=NASDAQ÷ndMoreThan=0&limit=1000&apikey={demo}')
#get companies based on criteria defined about
screener = requests.get(url).json()
print(screener)
[{'symbol': 'AAPL', 'companyName': 'Apple Inc.', 'marketCap': 1526030790000, 'sector': 'Technology', 'beta': 1.228499, 'pri
ce': 352.08, 'lastAnnualDividend': 3.08, 'volume': 42532806, 'exchange': 'Nasdaq Global Select', 'exchangeShortName': 'NASD
AQ'}, {'symbol': 'MSFT', 'companyName':....
Next, we extract all financial ratios from the Nasdaq companies that we obtained in the previous section. For our investment model, we will work with below selected financial ratios.
add selected companies to a list
for item in screener:
companies.append(item['symbol'])
#print(companies)
value_ratios ={}
#get the financial ratios
count = 0
for company in companies:
try:
if count <30:
count = count + 1
fin_ratios = requests.get(f'https://financialmodelingprep.com/api/v3/ratios/{company}?apikey={demo}').json()
value_ratios[company] = {}
value_ratios[company]['ROE'] = fin_ratios[0]['returnOnEquity']
value_ratios[company]['ROA'] = fin_ratios[0]['returnOnAssets']
value_ratios[company]['Debt_Ratio'] = fin_ratios[0]['debtRatio']
value_ratios[company]['Interest_Coverage'] = fin_ratios[0]['interestCoverage']
value_ratios[company]['Payout_Ratio'] = fin_ratios[0]['payoutRatio']
value_ratios[company]['Dividend_Payout_Ratio'] = fin_ratios[0]['dividendPayoutRatio']
value_ratios[company]['PB'] = fin_ratios[0]['priceToBookRatio']
value_ratios[company]['PS'] = fin_ratios[0]['priceToSalesRatio']
value_ratios[company]['PE'] = fin_ratios[0]['priceEarningsRatio']
value_ratios[company]['Dividend_Yield'] = fin_ratios[0]['dividendYield']
value_ratios[company]['Gross_Profit_Margin'] = fin_ratios[0]['grossProfitMargin']
#more financials on growth:https://financialmodelingprep.com/api/v3/financial-growth/AAPL?apikey=demo
growth_ratios = requests.get(f'https://financialmodelingprep.com/api/v3/financial-growth/{company}?apikey={demo}').json()
value_ratios[company]['Revenue_Growth'] = growth_ratios[0]['revenueGrowth']
value_ratios[company]['NetIncome_Growth'] = growth_ratios[0]['netIncomeGrowth']
value_ratios[company]['EPS_Growth'] = growth_ratios[0]['epsgrowth']
value_ratios[company]['RD_Growth'] = growth_ratios[0]['rdexpenseGrowth']
except:
pass
print(value_ratios)
Note that we have included an if statement to limit to 30 the number of companies for which we will include in our model. Feel free to remove this restriction.
And below we have a sample of the obtained response containing a list of financial ratios for all of our Nasdaq companies. We have added them to a Python dictionary in order to be able to access them easily.
Value Ratio Dictionary outcome:
{'AAPL': {'ROE': 0.6106445053487756, 'ROA': 0.16323009842961633, 'Debt_Ratio': 0.7326921031797611, 'Interest_Coverage': 18.382829977628635, 'Payout_Ratio': 0.25551976255972203, 'Dividend_Payout_Ratio': 0.25551976255972203, 'PB': 12.709658271815046, 'PS': 4.420393881402446, 'PE': 20.81351450883162, 'Dividend_Yield': 0.012276627402416805, 'Gross_Profit_Margin': 0.3781776810903472, 'Revenue_Growth': -0.020410775805267418, 'NetIncome_Growth': -0.07181132519191681, 'EPS_Growth': -0.003330557868442893, 'RD_Growth': 0.1391542568137117}, 'MSFT': {'ROE': 0.3834652594547054, 'ROA': 0.13693658482111698, 'Debt_Ratio': 0.6428970253632798, 'Interest_Coverage': 5.881980640357408, 'Payout_Ratio':0.35196228338430174, 'Dividend_Payout_Ratio': 0.3519622833843017, 'PB': 10.52384979966774, 'PS': 8.557532401484389, 'PE': 27.444076197757386, 'Dividend_Yield': 0.012824708722134454, 'Gross_Profit_Marg
#data-science #investment #python