Finding High Dividend Stocks with Python

High dividend stocks are a great investment strategy for all investors looking to get an almost safe inflow of cash every year. We are going to build a Python script to find companies in the NASDAQ exchange with the highest dividend yields.

High Dividend Stocks with Python
Photo by Karolina Grabowska on Pexels.com

What is the Dividend Yield?

Dividend yield is the ratio of a company annual dividend compared to its stock price. That is the amount that a company paid annually to shareholders compared to the stock price. For instance, imagine company ABX paying $5 in annual dividends every year. If the current stock price of ABX is $100, then the dividend yield will be of 5%.

Right now, with stock prices going down quickly due to coronavirus, it is a good opportunity to find very good stocks at a real bargain price. For investors interested in high dividend stocks, now may be a good opportunity to find potential investment opportunities in the stock market.

As we analysed in one of my previous posts, since the coronavirus started, companies are trading at a discount of around 10%, 20% or even bigger. Check out here to learn how to calculate the impact of coronavirus on stock prices with Python.

We are going to build a Python script to go through all the stocks in the Nasdaq exchange and compute the dividend yield for each of them. Note that a lot of growing companies, like Amazon, are not paying dividends out since they reinvest all the earnings into growing the business. Therefore, the dividend yield for these firms will be zero.

To find high dividends stocks, we are going to apply below dividend yield formula. Then, we will sort by the returned yield in order to find the best dividends stocks.

Dividend Yield = Annual Dividend / Share Price

Calculating Dividend Yields with Python

Calculating Dividend Yields is a pretty easy task to do using a financial API called fmpcloud and Python. This API offers a few free calls per day upon registration. Let’s have a look at the steps to calculate dividend yields:

1. First thing is to extract a list of tickers for all stocks for which we are going to calculate the dividend yield. These will be the stocks where we will later find high dividend stocks. In our case, we are going to get all stocks of companies trading in Nasdaq and add them to a list call symbols:

import requests

demo = 'your_api'

tickers = requests.get(f'https://fmpcloud.io/api/v3/symbol/available-nasdaq?apikey={demo}')

tickers = tickers.json()
symbols = []
for ticker in tickers:
    symbols.append(ticker['symbol'])
#print(symbols)
['SMMCW', 'VOD', 'TRMD', 'TRMB', 'NBL', 'EMMA',...]

2. Once we have our list of stocks trading in Nasdaq, we are going to loop through each of them and make an http request to the API end point to retrieve company financial information.

Note that we parse the response to extract the last annual dividend, price, market capitalisation, etc. Then, we calculate the dividend yield and add all these variables into a Python dictionary called DivYield:

DivYield = {}
for company in symbols:
  try:
    companydata = requests.get(f'https://fmpcloud.io/api/v3/profile/{company}?apikey={demo}')
    companydata = companydata.json()
    latest_Annual_Dividend = companydata[0]['lastDiv']
    price = companydata[0]['price']
    market_Capitalization = companydata[0]['mktCap']
    name = companydata[0]['companyName']
    exchange = companydata[0]['exchange']

    dividend_Yield= latest_Annual_Dividend/price
    DivYield[company] = {}
    DivYield[company]['Dividend_Yield'] = dividend_Yield
    DivYield[company]['latest_Price'] = price
    DivYield[company]['latest_Dividend'] = latest_Annual_Dividend
    DivYield[company]['market_Capit_in_M'] = market_Capitalization/1000000
    DivYield[company]['company_Name'] = name
    DivYield[company]['exchange'] = exchange
  except:
    pass
print(DivYield)
#outcome
{'VOD': {'Dividend_Yield': 0.07571881188118812, 'latest_Price': 14.14, 'latest_Dividend': 1.070664, 'market_Capit_in_M': 37427.024, 'company_Name': 'Vodafone Group Plc', 'exchange': 'Nasdaq Global Select'}, 'TRMD': {'Dividend_Yield': 0.0, 'latest_Price': 7.69....

3. Finally, we have all data extracted from the API. Now we will present the results in a nice Pandas DataFrame and sort the Dividend Yield values in ascending order to have high dividend stocks at the top.

import pandas as pd
DivYield_dataframe = pd.DataFrame.from_dict(DivYield, orient='index')

DivYield_dataframe = DivYield_dataframe.sort_values(['Dividend_Yield'], ascending=[False])
DivYield_dataframe
Dividend Yield - High Dividend Stocks
Dividend Yield – High Dividend Stocks

Looking at the highest dividend stock

Above Pandas DateFrame is a sample of the results, you may have obtained different results since I made the analysis a few days ago and the price of the companies is quite volatile right now.

The highest dividend stock in my results was Martin Midstream Partners L.P. with a 200% dividend Yield. Watch out with companies with such a high dividend yield. Since it may be that the stock price has recently crashed pushing up the dividend yield and the company may be experienced financial troubles.

Remember that dividend yield is a backward looking measure. E.g. It looks at the last year annual dividend. This does not actually mean that the company will keep the same dividend amount during next years. Specially if they are having financial troubles.

Wrapping Up and Additional Comments

We have built a script to find stocks paying the best dividends in terms of dividend yield ratio. We have analysed all stocks in Nasdaq. Feel free to repeat the task with other exchanges such as the New York Stock Exchange (NYSE).

Before making any investment decision based on the results, a further analysis is required:

  • There are a lot of companies which are currently suspending dividends due to the economic impact of coronavirus. Therefore, we may find a company with a very high dividend yield in the past, which may not be paying any dividend in 2020.
  • Also, pay attention to the market capitalisation. It is much safer to invest in well consolidated companies with a sounded market capitalisation.
  • I would really not go for companies with a dividend yield of more than 20%, especially if they do not have a solid balance sheet and plenty of cash to be able to meet short term obligations.
  • I recommend making a liquidity analysis of high dividends companies as shown in my previous post.

Hope that you have enjoy the post. Looking forward to your feedback in my Twitter.

Below is the whole code for your reference:

import requests
demo = 'your api'

tickers = requests.get(f'https://fmpcloud.io/api/v3/symbol/available-nasdaq?apikey={demo}')

tickers = tickers.json()
symbols = []
for ticker in tickers:
    symbols.append(ticker['symbol'])
  
print(symbols)
DivYield = {}

for company in symbols:
  try:
    companydata = requests.get(f'https://fmpcloud.io/api/v3/profile/{company}?apikey={demo}')
    companydata = companydata.json()
    latest_Annual_Dividend = companydata[0]['lastDiv']
    price = companydata[0]['price']
    market_Capitalization = companydata[0]['mktCap']
    name = companydata[0]['companyName']
    exchange = companydata[0]['exchange']

    dividend_Yield= latest_Annual_Dividend/price
    DivYield[company] = {}
    DivYield[company]['Dividend_Yield'] = dividend_Yield
    DivYield[company]['latest_Price'] = price
    DivYield[company]['latest_Dividend'] = latest_Annual_Dividend
    DivYield[company]['market_Capit_in_M'] = market_Capitalization/1000000
    DivYield[company]['company_Name'] = name
    DivYield[company]['exchange'] = exchange
  except:
    pass
print(DivYield)

import pandas as pd
DivYield_dataframe = pd.DataFrame.from_dict(DivYield, orient='index')

DivYield_dataframe = DivYield_dataframe.sort_values(['Dividend_Yield'], ascending=[False])
DivYield_dataframe