[Python] Eietn artifact: How to use it efficiently when making an A-share investment portfolio?


Preface

In the previous article: "Eiten, a good helper for building a U.S. stock portfolio", we explained Eiten, an open source toolkit, and how to use it to build a U.S. stock portfolio.

The so-called portfolio optimization is to determine the weight distribution ratio of your stock pool. This step is performed after the stock selection is completed. Regarding stock selection, you can read our previous article: Alphalens, a single-factor backtesting tool for quantitative investment.

In this article we will introduce how to use Eiten to optimize A-share investment portfolios. The stocks in this article are randomly selected, please do not refer to them.
Insert image description here

1. Preparation

Before you start, you need to make sure that Python and pip have been successfully installed on your computer. If not, you can visit this article: Super Detailed Python Installation Guide to install it.

(Optional 1) If you use Python for data analysis, you can directly install Anaconda: Anaconda, a good helper for Python data analysis and mining, has built-in Python and pip.

(Optional 2) In addition, it is recommended that you use the VSCode editor, which has many advantages: The best partner for Python programming—VSCode Detailed Guide.

Please choose any of the following methods to enter the command to install dependencies:

  1. Open Cmd (Start-Run-CMD) in Windows environment.
  2. Open Terminal in MacOS environment (command+space to enter Terminal).
  3. If you are using the VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.
git clone https://github.com/tradytics/eiten.git
cd eiten
pip install -r requirements.txt
pip install yfinance --upgrade --no-cache-dir

Insert image description here

2. How to use - A shares

Write the list of candidate stocks you want to build your portfolio into stocks/stocks.txt. The stock code format of A shares is as follows:

For the Shanghai market, the stock code suffix is ​​.SS, such as: 600519.SS and 688111.SS

For Shenzhen market, the stock code suffix is ​​.SZ, such as: 000858.SZ and 300498.SZ

For example, I put the following 10 stocks (randomly selected, please do not refer to them) in stocks/stocks.txt for portfolio optimization:

600519.SS
601318.SS
600036.SS
000858.SZ
601012.SS
000333.SZ
600276.SS
002415.SZ
601166.SS
601888.SS

Enter the following command in the terminal to run and try the effect:

python portfolio_manager.py --is_test 1 --future_bars 20 --data_granularity_minutes 3600 --history_to_use 250 --apply_noise_filtering 1 --only_long 1 --eigen_portfolio_number 3 --stocks_file_path stocks/stocks.txt

Parameter Description:

is_test: This value determines whether the program should retain some data for future testing. When this value is True, the value of future_bars should be greater than 5.

future_bars: The most recent n K-lines that will be excluded when constructing the portfolio. This is also called out-of-sample data.

data_granularity_minutes: How often do you want data to build your portfolio. For a long term portfolio you should use daily data (3600) but for a short term strategy you can use minute data (60, 30, 15, 5, 1).

history_to_use: Whether to use a specific amount of data or use all the data we downloaded from Yahoo Finance. For minute-level data, we only downloaded one month of historical data. For the daily line, we downloaded 5 years of historical data. If you want to use all available data, the value should be all, but if you want to use a smaller amount of data, you can set it to an integer, such as 100, which will only use the last 100 K-lines to build the investment combination. In the example of this article, we only use 250 K lines because the CSI 300 Index on Yahoo Finance only saves data for one and a half years.

apply_noise_filtering: It uses random matrix theory to filter out the covariance matrix of randomness, resulting in better portfolios. A value of 1 will enable it.

market_index: Which index do you want to use as the benchmark for your investment portfolio? Here I use the CSI 300 Index (000300.SS).

only_long: Whether to only do long.

eigen_portfolio_number: For the Eigen strategy, the smaller the number, the lower the risk and reward.

stocks_file_path: The list of stocks you want to use to build your portfolio.

After execution, you will first see the weight assigned to each stock by all strategies output in the terminal:

-------- Weights for Eigen Portfolio --------
Symbol: 000333.SZ, Weight: 0.3399
Symbol: 000858.SZ, Weight: 0.0496
Symbol: 002415.SZ, Weight: -0.0787
Symbol: 600036.SS, Weight: 0.3179
Symbol: 600276.SS, Weight: 0.1612
Symbol: 600519.SS, Weight: 0.0292
Symbol: 601012.SS, Weight: 0.7539
Symbol: 601166.SS, Weight: 0.3149
Symbol: 601318.SS, Weight: 0.2433
Symbol: 601888.SS, Weight: -1.1312

-------- Weights for Minimum Variance Portfolio (MVP) --------
Symbol: 000333.SZ, Weight: -0.0335
Symbol: 000858.SZ, Weight: -0.0812
Symbol: 002415.SZ, Weight: 0.1281
Symbol: 600036.SS, Weight: -0.2021
Symbol: 600276.SS, Weight: 0.0767
Symbol: 600519.SS, Weight: 0.2759
Symbol: 601012.SS, Weight: 0.1913
Symbol: 601166.SS, Weight: 0.3773
Symbol: 601318.SS, Weight: 0.3735
Symbol: 601888.SS, Weight: -0.1058

-------- Weights for Maximum Sharpe Portfolio (MSR) --------
Symbol: 000333.SZ, Weight: 1.6382
Symbol: 000858.SZ, Weight: 0.1264
Symbol: 002415.SZ, Weight: 1.0846
Symbol: 600036.SS, Weight: -0.5394
Symbol: 600276.SS, Weight: 0.2878
Symbol: 600519.SS, Weight: -1.3160
Symbol: 601012.SS, Weight: 0.4310
Symbol: 601166.SS, Weight: 0.7743
Symbol: 601318.SS, Weight: -1.2865
Symbol: 601888.SS, Weight: -0.2004

-------- Weights for Genetic Algo (GA) --------
Symbol: 000333.SZ, Weight: -0.1276
Symbol: 000858.SZ, Weight: -0.8724
Symbol: 002415.SZ, Weight: -1.0129
Symbol: 600036.SS, Weight: -1.5845
Symbol: 600276.SS, Weight: -0.3169
Symbol: 600519.SS, Weight: 1.7996
Symbol: 601012.SS, Weight: 0.0641
Symbol: 601166.SS, Weight: 0.9515
Symbol: 601318.SS, Weight: 0.4069
Symbol: 601888.SS, Weight: 0.2969

Insert image description here
In the second picture, you can see the backtest effect of each strategy. You can see that the GA strategy for the combination of these 10 stocks will have a better effect than the CSI 300: In the third picture, we
Insert image description here
set The last 20 trading days were used for testing. This is the test result. Since the market has been in a downward trend recently, these 10 stocks have also experienced violent fluctuations, and the effect is average.
Insert image description here
The fourth picture is an estimate of the future and is not of much reference.
Insert image description here

3. Principles of the Four Strategies

3. Principles of the four strategies

You can see that the output report contains 4 strategies:

Eigen Portfolios Characteristic Portfolio (Blue)

These portfolios typically have low correlation to the market and generate relatively high returns and alpha. However, because they are not highly correlated with the market, they can also carry significant risks. The lower the number, the lower the risk and reward.

Minimum Variance Portfolio (MVP) Minimum Variance Portfolio (orange)

MVP attempts to minimize the return variance of a portfolio. These portfolios offer the lowest risk and return.

Maximum Sharpe Ratio Portfolio (MSR) Maximum Sharpe Ratio Portfolio (green)

MSR attempts to maximize the Sharpe ratio of a portfolio. It uses past returns in the optimization process, which means that if past returns differ from future returns, future results may be different.

Genetic Algorithm (GA) based Portfolio Genetic Algorithm (GA) based Portfolio (red)

This is a GA based portfolio implemented within the Eiten module. Often provides a stronger portfolio than other strategies.

Summarize

Our article ends here. If you like today’s quantitative investment content, please continue to pay attention to Erqi Al Quantitative.

Guess you like

Origin blog.csdn.net/liaozp88/article/details/132814376