Portfolio Balancing with Historical Stock Data
Posted on Fri 19 April 2019 in Python • 4 min read
Following last weeks' post (Python for the Finance Industry). This post is to demonstrate a method of determining an optimized portfolio based on historical stock price data.
First of all while attempting to tackle this problem, I stumbled across many very informative articles in which based on what I learned throughout reading them, and trying to replicate their findings with the ASX stocks' data.
 Ricky Kim (Efficient Frontier Portfolio Optimisation in Python)
 Bernard Brenyah (Markowitz’s Efficient Frontier in Python)
Now I will not be going into how Markowit'z Efficient Frontier Portfolio Optimization & Sharpe Ratios works as these techniques are extremely well documented across this internet and very easily found. This post will be for implementing these techniques in Python to apply them to an ASX based portfolio.
Picking up from the end of the previous post, we had just plotted the percentage change over the time period for our stocks' data. For the sake of this post we will be using a technique called random optimization, where will be taking a number of random attempts and selecting the best one. Further posts will show a more detailed approach to this optimization problem.
Now there are multiple steps before we get to the desired outcome of a balanced portfolio.
 Generate X number of 'random' portfolios,
 Rate their performance against one another,
 Pick the desired solution.
To generate random portfolios, we define a function such that we can pass it differing variables as to tweak our outcomes in the future.
1 2 3 4 5 6 7 8 9 10 11 12 

To step through this function:
 Define empty location for our portfolio performance results to be stored along with recording weights so we can extract them once selected,
 For each portfolio to be generated, give a random 'weighting' for each of the company that we have historical data on (eg, 23% NAB.AX),
 Even out the distribution of the weights such that the sum of the weightings is 100% (eg, total budget),
 Record the weightings generated in our memory location,
 Determine the performance of our randomly generated portfolio (more on that soon),
 Fill in the portfolio performance results for this generated portfolio and repeat for X number of portfolios.
In step 5 above, we have to determine how to rank the generated portfolios against each other to work out how to filter our results. To do this, we calculate volatility of the portfolio using the following formula:
Bernard Brenyah, whom I mentioned at the beginning of the post, has provided a clear explanation of how the above formula can be expressed in matrix calculation in one of his blog posts. In which we just take the matrix calculation and multiply by 253 for number of trading days in Australia.
1 2 3 4 5 

Now that we have X number of randomly generated portfolios, all ranked against one another, it's time to plot so that our results can be visualized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 

Using the above function 'display_random_efficient_frontier', this will determine our max sharpe ratio portfolio generated and the minimum volatility portfolio with their respective returns. Now it is entirely up to the trader on how much risk they are willing to take on board with their portfolio. With the settings below in conjunction with the previously defined functions and stock data to generate the portfolios (risk free rate determined from this website).
1 2 3 4 5 6 7 

With the two portfolios determined, the one gives us the best riskadjusted (as long as the trader is prepared to take the risk) is the one with the maximum Sharpe ratio, allocating a 67% portion to WOW and 32% to BHP, as these stocks were quite volatile from the daily percentage change calculations.
On the other hand, the minimum volatility portfolio is reflecting the more stable of the stocks from the daily percentage change calculations distributing portions over NAB and TLS due to their stability from the percentage change calculations and reducing the portion to WOW.