Automating your Stocks Portfolio’s Daily Analysis Report using Python

Suhail Saqan
Nov 7, 2020 · 4 min read

We live in times when almost everything is automated. People no longer have to do the basic stuff which could be easily done by computers. For example, anyone could write a script to send them reports on their current stocks portfolio status or even suggestions for good stocks to buy.

Algorithmic Trading

“I predict that, because of artificial intelligence and its ability to automate certain tasks that in the past were impossible to automate, not only will we have a much wealthier civilization, but the quality of work will go up very significantly and a higher fraction of people will have callings and careers relative to today.” ~Jeff Bezos, founder of Amazon.

This article demonstrates a simple Python script for automating a daily stock portfolio report. It will be generating a report about your portfolio and emailing it to you. The report will contain information like todays return and top movers.

Automating your Stocks Portfolio Report

We are going to be using Python 3.8 for this guide. For anyone who has little to no experience I recommend reading this article for a quick step by step tutorial on installing it.

We are also going to be using the following libraries which you could install using Pip (check the article above for more information):

  1. Yahoo Finance: For getting stock related data.
  2. Pandas: Makes working with big data a lot easier with Python
  3. DateTime: For getting and calculating current dates and time.
  4. OS and Shutil: Accessing local folders and files on your computer.
  5. Smtplib and SSL: For sending emails using Python.
All Imports Necessary for this Script

The first step is to store each ticker in your portfolio, its relative weight, and its purchase date all in an Excel file. From there we could read the file and use that data for our calculations. You need to make a file that looks something like this representing your portfolio:

Example of My Portfolio Excel File (the screenshot was cut to save space)

In order to get the historical data for each stock, we will be using the Yahoo Finance library to request all that data for free. First, we will need to read the Excel file to get the stock tickers and their weight. After that, it will store them in a Pandas DataFrame to use them for the calculations later. In order to do that, we need to use a while loop to iterate through each ticker and request its historical data. We will then store that data locally in a .csv file. The script will automatically make you the folder to store them in. You might also realize that I added a sleep timer which slows down the loop. I did that in order to not get banned from Yahoo Finance for performing requests too fast.

Downloading the Historical Data of Each Stock in Our Portfolio

Now we will be performing the analysis calculations that will be used in the report. It will include the top movers on that day, the return on that day, and the total return since purchase. However you could always add more if you wanted to.

The first step is to add all the data files to an array so that we could iterate through them. I also added a ticker that is called ‘Nothing’. That is the rest of the portfolio that is in cash so we also assumed its value will stay the same overtime. Next, we want to perform the calculations needed while we iterate through each of the stock’s data. This process is as easy as dividing the current price of the stock by the price you want to compare it to. We will do that for each stock and store everything to one Pandas DataFrame.

Most Calculations Needed for Our Portfolio’s Analysis

Up to now we only have the return rates of each stock without regard to its weight. Instead, we need to calculate how much effect each stock has had in the portfolio. This can be done by multiplying its return rate by its weight in the portfolio. After that we just add them to the original data frame.

You may also want to know the stocks that made you the most on that day. As I called it ‘top_movers’ in the script. To calculate that, we order the data frame from the smallest to the greatest of today’s return. Next, we just get the last five rows and it should be your ‘top movers’ for the day.

Calculate the Weighted Returns

The last step in all of this is to send the report to yourself or the recipient through email. Since we have all the numbers from the calculations done all that is left is to input them into the body and send it. We can use the SMTP and SSL Python libraries to send our email. You will have to specify the sender and receiver emails as well as the password to login.

If you are not using a Gmail account you also have to change the value of ‘port’ in your script to your email hosts SMTP port number. Gmail’s port number is 465 as an example.

Emailing the Portfolio Report

If everything is done right, you will receive an email that will look something like this:

Example of the Email I Received (the screenshot was cut to save space in this article)

Finally…

You could always add more calculations to this program to give you what you want. You could also use PythonAnywhere (there are different services as well) to host your script and have it email you your report everyday.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Suhail Saqan

Written by

write as i learn

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Suhail Saqan

Written by

write as i learn

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store