Trading Stocks Using Portfolio Optimization

It's only fair to share...Share on Facebook0Share on LinkedIn0Share on Google+0Tweet about this on Twitter0

It’s an understatement to say technology has transformed the way people conduct their financial objectives. Not only has technology has increased the ease of investing and made it cheaper, but there are also tons of free information available for the average investor. There is so much information on the internet related utilizing technology with finance so the average person can use the same portfolio management techniques once only available to financial institutions.

I’ve been spending a lot of time discovering how to utilize programming techniques for financial analysis. In this post, I will share how I managed to leverage some suitable programming languages, such as Python.

Some of the techniques I will go through involves the use of the Jupyter Notebook, or an Integrated Development Environment (IDE). You don’t actually need to install Python to run these codes. You can use the Jupyterlabto follow along.


There are some things to note: I don’t trade stocks professionally. Most of my involvement in equities involves typical research. However, I have some background in trading; mostly involving derivatives such as options and Foreign Exchange (FOREX).

Overall, I do have some experience with constructing portfolios. One of the most common portfolio management strategies involves the Modern Portfolio Theory.

Modern Portfolio Theory

The Modern Portfolio Theory (MPT) is an investment theory developed by Harry Markowitz and published under the title “Portfolio Selection” in the Journal of Finance in 1952.

It’s a pretty vast financial theory, but intuitively, it’s pretty simple. It’s based on the concept that there is no such thing as a free lunch, as all assets have a risk-return trade-off (higher risk associated with the higher return, and vice-versa).

MPT assumes that investors are risk-averse, meaning that given two portfolios that offer the same expected return, investors will prefer the less risky one. Therefore, an investor will only take on more risk only if higher expected returns compensate the risk.

Diversification comes into play in MPT, which most people already understand; basically, avoid putting all of your eggs in one basket. Investors can reap the benefits of diversification by investing in more than on stock.

Starting the Code

The code will be explained as we go along. To optimize your own portfolio, you’ll need to install a few libraries for your module. For extracting financial data, the platforms that I find the most helpful or Quandl and Robinhood. They’re not necessarily the best, but they are the most useful. For this tutorial, I’ll use Quandl.

If you haven’t installed Quandl before, you should install the package in your command terminal. Before you can use it, you need to get an API key on Quandl’s website (it’s free, don’t worry).

The following block of code requires your Quandl API code. The stocks selected for this post is Facebook, Amazon, Netflix, and Google. These are pretty known as the FANG stocks. We’ll work with two-year price data: from 2016 to 2018.

The output should look like a simple data frame.

We have all the data we need, but it’s constructed in a way that makes it more difficult to process. Let’s clean the data so that it’s easier to work with.

Cool. Now let’s look at the historical price data

It looks like that Amazon and Google’s stock price relative more expensive. However, looking at only the price isn’t very useful for our purpose. Instead, we should be looking at daily stock returns. By looking at the returns (instead of individual stock prices), we can also look at overall stock volatility.

Randomly Generated Portfolios

We have four stocks in our portfolio and a finite amount of funds (presumably). The question then changes from “what stocks do we add in our portfolio,” to “how do we allocate our four stocks?” The weight of which individual stock must weight equal 1 (or 100%).

For example, we could allocate the portfolio equally. That is since we have four stocks, each stock would consist of 25% of the overall portfolio (FB: 25%; AMZN: 25%; NFLX: 25%; GOOGL: 25%).

That might seem obvious, but it may not be the best way to allocate a portfolio. As you can see, some of these stocks have very different risk profiles, despite having about the same return. Given this issue, what is the best allocation for these four stocks?

In Python, we’re going to create a couple of functions. The first function will be “portfolio_annualized_performance” which calculates the portfolio returns and volatility. We multiply by 252 (the square root of 252 for the volatility) for annualization.

Why 252? Because there are roughly 252 trading days in a year (excluding most holidays).For the “random_portfolio” will generate random weights for each stock. Using the num_portfolios argument, we can decide how many portfolios we want to generate.

Sharpe Ratio

If we’re going to continue with portfolio optimization, we also need to include the Sharpe Ratio. The Sharpe ratio is based on the concept of risk-adjusted return, which is calculated by measuring how much risk is involved in producing that return. The Sharpe ratio is probably the most widely used metric for calculating these returns.

There is some criticism with this formula; however, I won’t get into them here. The Sharpe ratio can help us find the most efficient portfolio relative to its level of risk.

We already calculated the average returns, the variance, and we have the number of portfolios we want to simulate. For the risk-free rate, I’m going to choose the current 52-week U.S. Treasury Bill, which is currently 2.65% as I write this.

The next function is going to generate random portfolios and collect the results (portfolio returns, volatility and Sharpe ratio) and weights for the corresponding result. Then the function locates the one with the highest Sharpe ratio portfolio. After, it will show the maximum Sharpe ratio portfolio as a red star symbol.

It also does a similar step for the minimum volatility portfolio and displays it as a green star on the plot. Randomly generated portfolios will be plotted with color applied to them based on the Sharpe ratio. The brighter (or bluer) the color, the higher the Sharpe Ratio.

The output should look something like this:

Unsurprisingly, Amazon was heavily allocated for the maximum risk portfolio. Then again, if you’re looking for a high return, it only makes sense to look at Amazon and Netflix. Oddly enough, Facebook has less than 1% allocation in the high-risk portfolio.

For minimum risk, slightly more than half of our budget should be allocated towards Google. Facebook and Amazon represent a fair amount of distribution in the minimum risk portfolio, while Netflix has roughly 2.5% of the portfolio.

The Efficient Frontier

As mentioned previously, Modern Portfolio Theory involves a strategy where the optimal combination of securities maximizes return for any given level of risk. For this reason, we call it the “Efficient Frontier.” Why is it efficient? Because points along the line will give you the lowest risk for a given target of return. All the other dots right to the line will give you a higher risk with the same returns. If the expected returns are the same, why would you take an extra risk when there’s an option with lower risk?

Portfolio managers utilize many different techniques to do this. Some advance excel users may use the solver function (for those of you who have taken Business Analytics) to help them find the optimal return. In Python, we need to do things a little differently.

In Scipy’s optimize function, there’s no ‘maximize’, so as an objective function you need to pass something that should be minimized. That is why the first “neg_sharpe_ratio” is computing the negative Sharpe ratio. Now we can use this as our objective function to minimize.

In “max_sharpe_ratio” function, you first define arguments (this should not include the variables you would like to change for optimization, in this case, “weights”). At first, the construction of constraints was a bit difficult for me to understand, due to the way it is stated.

We can also define an optimization function for calculating the minimum volatility in the portfolio. What exactly are we trying to minimize? We want to minimize volatility by trying different weights.

The next function is calculating the most efficient portffolio for a given target return. The second function is taking a range of target returns and computing the efficient portfolio for each level of return. Then, we draw a line that depicts the most efficient portfolios for any given amount of risk. This is the “efficient frontier.”

Now we plot the portfolio choices with the maximum Sharpe ratio and minimum volatility with the same number of randomly generated portfolios. What is different this time is that we’re going to calculate the optimal portfolios by using the Scipy’s minimize function, instead of the optimal portfolios generated previously.

The Output now shows the following:

Not much as really changed regarding the minimum volatility portfolio allocation. Our simulation is telling us that we should hold more Google and Facebook while holding less of Netflix.

However, looking at our maximum volatility portfolio allocation, we see that our simulation is telling us that we should put 68.19% Amazon, 31.81% Netflix, and 0% for Facebook and Google. So instead of holding FANG stocks and hoping for the optimal return, I should just own AN stocks.

Finally, instead of just plotting ever randomly generated portfolio, we can just plot each of the individual stocks on the plot with their corresponding returns and risk. This way we can see and compare how diversification is lowering the risk by optimizing the allocation.


So give the following risk and return profiles, it should be easy to see why our simulation (given that we want higher returns) would tell us to hold Amazon and Netflix over Google and Facebook. It should also be easier to see why the same simulation would tell us to hold more Amazon and Less Netflix. You get about the same level of return for much less risk.

There are a lot more techniques involving portfolio management and algorithmic investing/trading. I’ve only scratched the surface.

464 total views, 7 views today


Andre is a senior at Pace University, studying Finance and Economics. His topics of interests include Equity Valuation, Short Ideas, Macro, Monetary Economics, ETFs, Forex and Fixed Income. His sectors of choice involve Technology, Consumer Goods, and Financials. Andre has completed a number of internships at leading investment banks domestically and abroad.