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
import datetime as dt
symbols = ['INTC','VZ','AAPL', 'AMZN', 'MSFT', 'CAT', 'AXP', 'BA', 'GE', 'CVX', 'HD', 'NKE', 'PFE', 'MMM', 'TRV', 'UTX', 'MRK', 'DIS', 'DWDP', 'JNJ']
allocations = [25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25]
start_date = dt.date(2017,01,03)
bench_symbol = "SPY"
#RF Syntax: 6 MO, 2 YR etc.
rate = '1 YR'
rf_start_date = dt.date(2017, 1, 3)
api_key = "YOUR KEY HERE"
#Dirctory Input For Data and Reports
root_path = "C:/Users/DupuisF/Portfolio Tracker"
end_date = dt.date.today()
if __name__ == '__main__':
#1.) Import the module
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!
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.
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.
Violin Chart (Monthly Returns Plot):
The performance module allows the user to track the short term and long term performance of a portfolio.
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.
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.