Portfolio Analytics
Overview
Python-based program that delivers CAPM portfolio analytics and algorithmic trading analytics using time-series data (price/returns) for equities.
This project uses the Capital Asset Pricing Model and algorithmic trading analytics to gain insight into an equity portfolio. It allows a user to select a set of equities (by ticker), and plots a range of standard portfolio analytics (volatility, VaR, Expected Shortfall, Sharpe Ratio, etc), and algorithmic trading analytics (SMA, EMA, VWAP, TWAP). The GUI for this program is written in HTML5, using the Flask package to interface with Python.
Installation
The requirements file can be installed using the below commands in a terminal:
Conda: conda install --file requirements.txt
Pip: pip install -r requirements.txt
Current Analytics
- Portfolio R-Squared
- Portfolio Beta
- Portfolio Volatility
- Portfolio Alpha (calculated from the returns regression)
- Portfolio Alpha (based on risk-free rate)
- Portfolio Sharpe Ratio
- Portfolio Treynor Ratio
- Portfolio Tracking Error
- Analytical VaR (Normal Distribution)
- Analytical VaR (t-Distribution)
- Expected Shortfall (Normal Distribution)
- Expected Shortfall (t-Distribution)
- Historical VaR
Example Outputs
Using the equities: AAPL, GOOGL, BLK, and IBM (with respective portfolio wieghts of: 0.15, 0.6, 0.2, and 0.05), and the S&P 500 (^GSPC) as the benchmark, in the date range: 2019-06-20 to 2019-11-08, we obtain the below portfolio analytics.
- Portfolio R-Squared = 0.5713749319708816
- Portfolio Beta = 1.6823252286102592
- Portfolio Volatility = 0.001774262341931516
- Portfolio Alpha (calculated from the returns regression) = 0.043032257203536886
- Portfolio Alpha (based on risk-free rate) = 0.0052482979658219735
- Portfolio Sharpe Ratio = 3.354557106775981
- Portfolio Tracking Error = 0.030298260899050793
- Analytical VaR (Normal Distribution) = 7.390523472420238% at -1.0% of daily returns
- Analytical VaR (t-Distribution) = 49.62040090212909% at -1.0% of daily returns
- Expected Shortfall (Normal Distribution) at 5.0% level = 3.592204573450777
- Expected Shortfall (t-Distribution) at 5.0% level = 7.077446307947621
- Historical VaR = 4.0% at -1.0% of daily returns
Daily Return vs Time (Portfolio and Benchmark)
Analytical VaR (Normal Distribution)
Analytical VaR (t Distribution)
Graphical User Interface Guide
A sample of the Graphical User Interface is shown in the file: Portfolio Analytics Sample Page.png.
Get Prices Tab
This tab contains two buttons that are used to get the most recent 5 months of prices for a set of equities and the benchmark.
Variables Tab
This tab lists the variables that are used to generate the portfolio analytics (together with the price data). The explanation for each variable is given below:
- Moving-average Interval: the number of days to use in the calculation of the Simple Moving Average.
- Moving-average Alpha Value: represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. A higher alpha discounts older observations faster.
- Equity Ticker: The ticker of the equity to look at for algorithmic trading analytics.
- VaR Threshold (% Daily Returns): the % daily returns threshold to use in the calculation of VaR (Historical and Analytical). For example, entering -0.01 means that we will calculate the probability of losing more than 1% of the value of our portfolio (the Expected Shortfall will be calculated based on this value).
- VaR Threshold (Confidence Interval): the Confidence Interval to use in the calculation of the VaR (this is another way of specifying the VaR). For example, entering 0.05 means that we will calculate the % daily returns that we can expect to lose at 5% VaR (that is, the amount that we expect to lose/gain with a probability of 5%).
- Risk-free Rate: this is the risk-free rate that is used in the calculation of certain portfolio analytics (e.g. a 0.02 = 2%). This is often set as the interest rate on a 3 month U.S. Treasury Bill.
Example Output
This image shows an example of the text ouput of the program - all portfolio analytics are listed here. These can be used, together with the graphs produced by the program, to gain an understanding of an equity portfolio.
Limitations
- The project uses the Alpha Advantage package to obtain price data. Due to the limited number of server requests this API allows, we currently save the data into Excel files, which are then read in by the main program, Portfolio-Analytics.py for processing. The Alpha Advantage model only allows us to obtain the prices and volume for each equity for the last 100 days
Outstanding Enhancements
- Portfolio breakdown by sector
- Portfolio breakdown by country
- Portfolio breakdown by region
- Returns attribution by equity
- Additional high-frequency trading analytics
- Portfolio Optimizer
- Seaborn plots
- Inclusion of other asset classes
- Plot Expected Shortfall as an overlay on VaR plots
- Monte Carlo (MC) VaR and other MC-based analytics
- stress-testing scenarios
- Returns Attribution (Brinson, Frongello, risk-factor attribution)