Setting up a quantitative system | removing and restoring rights of stock data

In the market software, we often see the options of ex right and re right. We choose different options, and the price of the stock in the software will change accordingly.

In the quantitative trading, we have developed a trading strategy, which needs to be tested back in the historical market data, so we should choose to remove the right or restore the right, which form of market data?

Ex right is usually the abbreviation of ex right and ex dividend. In these two cases, there will be different degrees of falling gap in the trend chart, which we call the ex right gap.

Ex dividend will make investors mistakenly think that it is a downward short gap, as follows:

If it is obviously wrong to calculate the rise and fall of the stock according to the ex right share price, it is - 52.49% shown in the software of tonghuashun. Similarly, the calculated indicators are of no reference value.

In order to make the trend chart truly reflect the trend of each stock price, the price after ex right and ex dividend is comparable only after re right processing.

Take the example just now. On June 27, 2016, the new hope closing price was 17.64 yuan. On June 28, 2016, every ten shares were converted into 10 shares, with a dividend of 5.5 yuan. Then, the closing price of the stock after ex right should be (17.64 – 0.55) * 10 / (10 + 10) = 8.55 yuan. On June 28, the closing price was 8.38, and the real rise and fall should be 8.38 / 8.55-1 = - 1.99%, instead of - 52.49% shown on the software.

When we get the price of the first day of the stock, we can calculate the right recovery price of each day after that by multiplying the real rise and fall, which is called the right recovery price. Similarly, if you know the last day's price of the stock, then the reverse processing can also calculate the price of the previous day, which is called the pre reversion price.

Next, we use the interface to obtain the market data of "new hope" ex right, pre right and post right, as follows:

# "Ex right"
"""
             High    Low   Open  Close     Volume
Date                                             
2010-01-04  14.38  13.94  13.94  14.17  216924.78
2010-01-05  14.28  13.85  14.22  14.18  107631.41
2010-01-06  14.31  13.87  14.15  13.88   89684.00
2010-01-07  13.97  13.38  13.80  13.48   85856.01
2010-01-08  13.64  13.35  13.43  13.62   58864.09
...           ...    ...    ...    ...        ...
2019-12-25  18.79  18.44  18.59  18.60  207776.34
2019-12-26  18.76  18.46  18.69  18.60  189935.42
2019-12-27  19.43  18.58  18.74  19.28  504214.70
2019-12-30  19.50  18.92  19.24  19.38  379296.95
2019-12-31  20.31  19.55  19.55  19.95  562873.40

[2244 rows x 5 columns]
"""
# "Post restoration right"
"""
              High     Low    Open   Close     Volume
Date                                                 
2010-01-04   75.15   72.85   72.85   74.05  216924.78
2010-01-05   74.63   72.38   74.32   74.11  107631.41
2010-01-06   74.79   72.49   73.95   72.54   89684.00
2010-01-07   73.01   69.93   72.12   70.45   85856.01
2010-01-08   71.28   69.77   70.19   71.18   58864.09
...            ...     ...     ...     ...        ...
2019-12-25  261.18  256.31  258.40  258.54  207776.34
2019-12-26  260.76  256.59  259.79  258.54  189935.42
2019-12-27  270.07  258.26  260.48  267.99  504214.70
2019-12-30  271.05  262.99  267.43  269.38  379296.95
2019-12-31  282.31  271.74  271.74  277.30  562873.40
"""
# "Former restoration right"
"""
             High    Low   Open  Close     Volume
Date                                             
2010-01-04   5.41   5.24   5.24   5.33  216924.78
2010-01-05   5.37   5.21   5.35   5.33  107631.41
2010-01-06   5.38   5.21   5.32   5.22   89684.00
2010-01-07   5.25   5.03   5.19   5.07   85856.01
2010-01-08   5.13   5.02   5.05   5.12   58864.09
...           ...    ...    ...    ...        ...
2019-12-25  18.79  18.44  18.59  18.60  207776.34
2019-12-26  18.76  18.46  18.69  18.60  189935.42
2019-12-27  19.43  18.58  18.74  19.28  504214.70
2019-12-30  19.50  18.92  19.24  19.38  379296.95
2019-12-31  20.31  19.55  19.55  19.95  562873.40

[2431 rows x 5 columns]
"""

Initially, the stock price in January 2010 is quite different from ex right, pre right and post right. Next, the visual closing price is compared:

It can be seen from the figure that they are quite different, and the effect of backtesting with the strategy of de weighting data execution will be quite different from the real situation.
Therefore, when the data we get is the data of ex right, we need to convert it into the data of pre right recovery / post right recovery.

The key here is to get the real up and down data just mentioned, so that we can calculate the pre / post recovery factor based on the data. For example, we used to restore rights as follows:

"""
trade_date
2019-12-31    0.97
2019-12-30    0.97
2019-12-27    0.93
2019-12-26    0.93
2019-12-25    0.93
              ... 
2010-01-08    0.25
2010-01-07    0.26
2010-01-06    0.27
2010-01-05    0.27
2010-01-04    0.26
Name: fd_factor, Length: 2244, dtype: float64        
"""

If it does not include the rise and fall data, the rise and fall calculated based on the ex right share price is not correct. Following pct_chg1 is the rise and fall of ex right stock price, which can be seen with the original rise and fall pct_chg is very different, pct_chg2 is the difference.

"""
            pct_chg  pct_chg1  pct_chg2
Date                                   
2010-01-04     2.83       NaN       NaN
2010-01-05     0.07  7.06e-04      0.07
2010-01-06    -2.12 -2.12e-02      2.10
2010-01-07    -2.88 -2.88e-02      2.85
2010-01-08     1.04  1.04e-02      1.03
...             ...       ...       ...
2019-12-25     0.38  3.78e-03      0.37
2019-12-26     0.00  0.00e+00      0.00
2019-12-27     3.66  3.66e-02      3.62
2019-12-30     0.52  5.19e-03      0.51
2019-12-31     2.94  2.94e-02      2.91

[2244 rows x 3 columns]

"""

After processing, we visualize the closing price data of "new hope" again, as shown below. It can be seen that after the ex right share price is reinstated, it coincides with the former reinstatement curve.

For the post recovery right, the recovery right price will be different if the initial price is different. For example, the market data and closing price trend obtained from the resumption of rights since January 1, 2010 are as follows:

# "Post restoration right"
"""
             High    Low   Open  Close     Volume
Date                                             
2010-01-04  14.38  13.94  13.94  14.17  216924.78
2010-01-05  14.28  13.85  14.22  14.18  107631.41
2010-01-06  14.31  13.87  14.15  13.88   89684.00
2010-01-07  13.97  13.38  13.80  13.48   85856.01
2010-01-08  13.64  13.35  13.43  13.62   58864.09
...           ...    ...    ...    ...        ...
2019-12-25  49.98  49.05  49.45  49.48  207776.34
2019-12-26  49.90  49.10  49.72  49.48  189935.42
2019-12-27  51.68  49.42  49.85  51.29  504214.70
2019-12-30  51.87  50.33  51.18  51.55  379296.95
2019-12-31  54.03  52.00  52.00  53.07  562873.40

[2244 rows x 5 columns]
"""

We have introduced the definition of ex right and re right, as well as the differences between them. The most important thing is to consider that when the stock is split, consolidated or dividend, the stock price will be affected by ex right. In the future, we will continue to introduce how to deal with the right removal in backtesting.

For more discussion on the above content, welcome to pay attention to [lantern master takes you to quantify the transaction with Python]!!

Tags: Python

Posted on Fri, 19 Jun 2020 03:32:56 -0400 by jbrill