You are welcome to subscribe to Python In Action: Building a Stock-based Quantitative Trading System. This booklet will include a series of columns that will expand on the topics covered in the booklet and make it easier for readers to access the information.

preface

There is always uncertainty in return and risk in stock investment. For rational investors, they tend to reach the equilibrium point between risk and return. For a single stock, sharpe ratio can be used in backtesting to comprehensively consider the relationship between return and risk. For a multi-stock portfolio, however, it introduces a question fund managers often have to consider — how to allocate positions?

In this paper, we will share a classic model — Markowitz mean-variance portfolio theory model to understand how to use Markowitz model to determine the optimal investment proportion in a variety of assets.

Introduction to Markowitz model

In 1952, Markowitz, an economist at the University of Chicago, applied the concepts of mean and variance in mathematics to define the two key factors of income and risk in asset Portfolio, thus systematically expounded the problem of asset Portfolio and choice, marking the Modern Portfolio Theory. MPT for short).

According to this theory, since the risk of asset investment lies in the uncertainty of returns, the return rate can be regarded as a random variable, the expected return of the portfolio is the expected value of the random variable, and the volatility of returns is expressed by the variance/standard deviation of the random variable to measure the risk of the portfolio.

Thus, a curve is formed by describing various optimized portfolios in a two-dimensional plane where volatility is the abscissa and return rate is the ordinate. The part of the curve above the minimum square margin is known as the “Efficient Frontier” of Markowitz’s portfolio, which corresponds to an Efficient portfolio.

According to the portfolio theory, we can carry out the following optimization process for the portfolio allocation of multiple stocks (assets) :

  • Obtain data of multiple stocks and analyze the return rate and volatility of stocks
  • Generate a large random-weighted return/risk portfolio
  • Find the least risky portfolio
  • Find the effective frontier
  • Find the random combination with high sharpe ratio, i.e., the return – risk equilibrium

Calculate the return rate of individual stocks

First of all, we select 002372 Weixing New Material, 000876 New Hope, 000851 Gaohong shares, 600797 Zhejiang University Wangxin, 000651 Gree Electric Appliances these five stocks for analysis, and store the code name in the list format. Download the closing prices of these 5 stocks and merge them into the table.

 """002372 000876 000851 600797 000651 trade_date 2019-01-02 14.63 7.40 5.03 7.30 35.80 2019-01-03 14.25 7.42 5.12 7.26 35.92 2019-01-04 14.49 7.52 5.49 7.51 36.65 2019-01-07 14.90 7.82 5.79 7.77 36.48 2019-01-08 15.18 7.60 5.79 7.67 36.34 The 2019-01-09 15.30 7.73 5.80 7.69 37.51 15.37 7.60 5.84 7.61 37.76 2019-01-10... . . . . . The 2019-01-11 15.49 7.64 5.67 7.74 37.73 2019-01-11 15.45 7.77 5.74 7.68 37.68 15.87 7.77 5.58 7.99 38.78 2019-01-15 2019-01-16 16.07 7.64 5.46 7.86 39.31"""
Copy the code

Then calculate the return rate of each stock, which is the logarithmic return rate. For details, please refer to the section of “Stock Trading Strategy: Return and Risk Dimensions measure The Effect of Strategy” in the booklet.

"""002372 000876 000851 600797 000651 trade_date 2019-01-03-0.026317 0.002699 0.017734-0.005495 0.003346 2019-01-04" trade_date 2019-01-03-0.026317 0.002699 0.017734-0.005495 0.003346 2019-01-04 0.016702 0.013387 0.069774 0.033856 0.020119 2019-01-07 0.027902 0.039118 0.053204 0.034035-0.004649 2019-01-08 0.018618-0.028536 0.000000-0.012954-0.003845 2019-02-20 0.040290 0.023878-0.009285-0.012136 0.008258... . . . . . 2019-11-18 0.015429 0.047883 0.003650 0.032485 0.002206 2019-11-19 0.034841 0.029960 0.032261 0.010899 0.007936 2019-11-20-0.018854-0.018676-0.012423-0.038679-0.010142 "2019-11-20-0.018854-0.018676-0.012423-0.038679-0.010142"""
Copy the code

Let’s visualize the histogram of each stock’s return rate to get a sense of the distribution. It can be seen that the distribution shape of each stock is myopic normal distribution, but there is still a “fat tail” phenomenon.

Of course, the daily superimposed yield curve of individual stocks can also be visualized to understand the yield trend in a year, as shown below:

Compute the covariance matrix

It is very important to calculate the covariance matrix of the portfolio in the process of constructing the optimal portfolio. The covariance matrix can be computed using the cov() method built into Pandas. As follows:

"""002372 000876 000851 600797 000651 002372 0.180281 0.040032 0.042272 0.022727 0.030380 000876 0.040032 0.293750 0.046080 0.085311 0.013274 000851 0.042272 0.046080 0.282092 0.161818 0.052123 600797 0.022727 0.085311 0.161818 0.245785 0.034793 000651 0.030380 0.013274 0.052123 0.034793 0.113990"""
Copy the code

Portfolio risk and return

The risk of a portfolio depends on the correlation of the return on assets in each portfolio. So in addition to calculating the covariance matrix, we also need to calculate the annualized rate of return. As follows:

"""002372-0.118489 000876 0.939151 000851 0.015777 600797 0.273381 000651 0.490995 DType: Float64"""
Copy the code

According to theory, risk needs to be spread, with each stock having a certain proportion of the investment weight. The return rate (mean value) of an asset portfolio is the sum of the weights of the return rate (mean value) of individual stocks in the portfolio.

When we calculate the expected return rate of the portfolio after the corresponding investment proportion is given, we first multiply the return rate of each stock by the corresponding weight, and then calculate the expected return of the portfolio after weighting, summing and averaging.

Then we can use Monte Carlo method to randomly generate multiple groups of weights, and calculate portfolio return rate and portfolio volatility according to the weights. It should be noted that the weight coefficients here range from 0 to 1, and the sum of the weight coefficients is 1.

Set this process to repeat 5000 times, that is to get 5000 kinds of portfolio return rate and volatility. When the sample number is larger, it can cover all kinds of weighted portfolio, including commonly used equal-weighted portfolio and market-value weighted portfolio. The data is then visualized as a scatter plot. As follows:

The horizontal axis represents the standard deviation of risk and the vertical axis is the rate of return. Each point represents a portfolio situation. According to Markowitz’s portfolio theory, rational investors ultimately make a choice between risk and return, always pursuing the maximization of expected return at a given level of risk, or the minimization of expected risk at a given level of return. So only the points on the efficient frontier are the most efficient portfolios.

Optimal portfolio

Then we as rational investors choose some point on the efficient frontier for our portfolio.

First we find the minimum risk portfolio (GMV portfolio) and plot it in a return – risk scatter plot. As follows:

Next, the Sharpe Ratio, a classic measure that takes both return and risk into account, is introduced.

Sharpe ratio, which measures the excess return generated for each unit of risk taken, is a standardized index for fund performance evaluation.

Next, we calculate the Sharpe ratio corresponding to the combination of the above Monte Carlo simulation and plot it as the third variable in the scatter plot of return and risk. Here, color, a visual cue, is used to represent the Sharpe ratio. As follows:

We find that the higher the upper left of the scatter plot, the higher the Sharpe ratio, and the higher the sharpe ratio, the better the weight distribution of the combination. Then start to find the portfolio with the largest Sharpe ratio (MSR) and plot it on a return – risk scatter plot. As follows:

We extract the corresponding weight of the maximum Sharpe ratio portfolio and convert it into a Numpy array as follows: [0.12617847 0.74273907 0.04905298 0.00580558 0.07622391]

Here is an example of five stocks. 12.6% of the weight to buy 002372 Weixing New Material, 74.3% of the weight to buy 000876 New hope, 4.9% of the weight to buy 000851 Gaohong shares, 0.5% of the weight to buy 600797 Zhejiang University Wangxin, 7.6% of the weight to buy 000651 Gree Electric Appliances. Based on previous data, such portfolio can be analyzed to obtain an annualized return rate of 72%, volatility of 42.6% and Sharpe ratio of 1.61.

conclusion

Although Markowitz’s portfolio theory involves many assumptions, the important thing is that it reveals the conclusion that “the expected return of an asset is determined by the size of its own risk”. At the same time, the effective boundary also proves that risks are proportional to returns, and higher returns require greater risks. However, the portfolio is the portfolio with the highest cost performance at the effective boundary.

The above strategies and the stocks involved are for educational purposes only and do not constitute any investment advice! Investment needs to be cautious, entering the market is risky!!

I am considering whether to add the content of this column to the booklet (including the implementation of the source code), interested friends welcome to give me comments! If the number is large, push more!

Subscribe for more quantitative trading contentSmall volumesRead!!!!!

You are also welcome to follow my wechat official account to learn more about Python quantitative trading