top of page

Script Maximising Dividend Payments

Hello, my fellow dreamers.


Part of my coding projects involves the stock market. So if you don't want to read about what is considered to be many to be a very dry subject, I understand. Still, this was a script I wrote a while ago, and I wrote an article on it on notd.io. Course, no one reads there, so here we go.


Problem Statement


A dividend is a passive income you can earn from simply holding certain stocks. Periodically these stocks give a percentage of the share value to shareholders. How much and how often varies from stock to stock. Generally speaking, they pay out about once a month at 2% of the share value. So, yes, for this to be large enough to be a second income you would have to have a lot of money locked up in your portfolio.


So, how can you maximize the amount of money you drop on stocks to get the biggest return on dividends? Trying to figure this out is a real headache, especially since most trading apps make it hard to compare stocks to each other. None of them also readily list if they even pay a dividend, never mind searching for this.


Enter my Script



To run it you will need the following dependencies:

  • python-dotenv

  • pandas

You will also need an API key from Financial Modeling Prep placed into a .env file inside the working directory. While there are paid plans for more hardcore operations, the free plan is enough for what this script will be doing. Goes without saying, but you also need a working internet connection.


What the script does while it runs is the following:

  1. Sends a request to Financial Modeling for a list of stocks available for trade today

  2. Gets the response and builds a pandas DataFrame from it

  3. Repeats for the dividend calendar

  4. Joins the Dataframes on the stock symbol

  5. Aggregates the amount paid per share (some pay out more than one in the selected period) and sums it.

  6. Divide the dividend payout by the current share price (it is money paid out per share, not shares per amount)

  7. The resultant DataFrame is sorted in Descending Order and saved as a CSV in the working directory.

  8. In the console, the top 5 with the highest dividend to share price will be displayed.


The script does not take any input from a GUI or command line (yet). However, there are spots you might want to change.

  • Line 12 is where the date range is being calculated. It is set to 30 days. You can increase or decrease the payout range by modifying the highlighted:

next30days = today+dt.timedelta(days=30)
  • Line 33 ensures that the resultant table only returns stocks that are available on the Toronto Stock Exchange. You can change the exchange by modifying the highlighted. You can also just comment it out if you want all the stocks from every exchange.

tsx_stocks = stocks[stocks['exchangeShortName']=='TSX']

Future Plans


One day I would like to train AI to trade stock, or failing that at least have these little scripts that can make investment recommendations. The current script will be modified to make changing certain variables easier. I would like to also place this, along with others, onto a Flask server to be accessible by users who might not want to get their hands dirty in code.


Perhaps it sounds like a lack of focus from other projects, but at some point I need income, and I need to figure out where my best options are. One look is stocks - does bankers rather well.


Now, trading stocks isn't without risk. A penny stock could go up in price, or go junk. While not as risky as crypto trading, there is still risk. Many liken stock trading to gambling, where there is no guarantee of winnings, the house always wins, and if you even win depends on RNG.


There is a rise in machine learning and AI giving promise to creating code that can accurately trade on the stock market. A coveted piece of code for some time. Course, in trying to create, one has to ask themselves "Why does this not already exist?" I have some ideas, but time will tell.


Stay tuned!

	

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
loz_wallpaper.png

Join Our Community

bottom of page