Tracking a Portfolio With Python

Link to Project: https://github.com/fdupuis659/Portfolio-Tracker

The following Python project allows a user to track their stock portfolio in Python. Since this is a beta version and will continually need adjustments, any changes suggested will be considered. You can also find this project on my GitHub page as well. This program assumes long-only positions (No shorts) for investment. With later iterations of this project, short-selling will likely be a feature added in.

There are many features of this project that can be used independently:

  • Tracking performance (daily as well as historical)
  • Rebalancing (Obtaining original weightings of assets over time)
  • Portfolio optimization (generating the minimum variance portfolio)
  • Risk-adjusted metrics (Sharpe Ratio, Alpha, etc.)
  • Portfolio Fundamentals (Beta, P/E Ratio, Dividend Yield, etc.)
  • Graphing abilities (Correlation Heatmaps, etc.)
  • Geographical and industry exposure
  • Reporting abilities (generate and automated daily fund report)

The idea of this project was to be able to break the functions listed above into separate modules that the user can activate at their discretion. The main feature of this project is the reporting module, which will automatically generate a daily fund report using ReportLab. Let’s walk through how to use this project to start tracking your own portfolio!

Step One: Getting Data

In order to track your portfolio, you will need to set up a Quandl account in order to obtain free historical end of day data for the securities you choose. It’s completely free, and once you set your account up you will have access to your own API key here: https://www.quandl.com/account/api

Copy and paste this API Key into the string variable located in the main module of the Portfolio Tracker project. After that, you are ready to get your data! We must use the data module to get the data before we run any other function!

Within the main module you also need:

  • A list of ticker symbols
  • Allocations (share amounts) for each symbol
  • A start date for your historical data
  • A benchmark symbol
  • A start date for benchmark historical data
  • A risk-free rate following the syntax given
  • A start date for the risk-free data
  • The root path of the location of the portfolio tracker project to store the data
  • An end date (today’s date) to update the data

This will store all the data you will need in a folder within the Portfolio Tracker project main directory! Now we can run some other modules within the project!

Portfolio Optimization: 

The goal of portfolio optimization is to find the correct weights to obtain a certain portfolio (typically Minimum Variance). This module will allow the user to obtain the weightings of a minimum variance portfolio (with their selected assets) and maximum Sharpe portfolio (or tangent portfolio). The module also outputs a graph of the efficient frontier.

Plotting Capabilities:

The plots module has the ability to output different summarizing charts for your portfolio. Below are some example graphs that were generated with the module.

Correlation Heatmap:

Violin Chart (Monthly Returns Plot):

Risk-Return Plot:

Performance:

The performance module allows the user to track the short term and long term performance of a portfolio.

Portfolio Metrics:

This module allows the user to output risk-adjusted metrics as well as weighted fundamental metrics for a portfolio. Note for the fundis function you will need to supply a rate (if not done already) and a method if desired. There are two methods, an average of the risk-free or the current risk-free rate.

Reporting Abilities:

This is the central module of this project. This module will essentially group the modules within this project to create a formatted daily report for a portfolio. Check out the PDF below to see an example of the reporting tool! Note the reporting module is a class, thus we must initialize an object instance of the class within the main file. Note a report name is already supplied within the object instance of the class.

Daily Report 2018-02-08

 

 

 

 

About the author

programmingforfinance

Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

3 Comments

  • Thanks. Really good article.

    Just one question: data.py takes data from Quandl WIKI dataset. Now this data feed is no longer actively supported by Quandl. So, any ideas from another reliable data source?

  • Hello ! Very interresting ….
    But , I meet any difficulties to launch your files .

    For example , I have this message

    You have not downloaded the necessary data to run the rebalance module. Please make sure to run the port_data module.

    In wich order , have I to launch the files … ? The first is probably the file “main.py”

    Otherwise , I suggest you add a main Window with Tkinter or Qt Designer .

Leave a Reply

Your email address will not be published. Required fields are marked *