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. This column is a supplement to the brochure content!!
preface
You may be familiar back home with The titans of Wall Street, Peter Lynch and Warren E. Buffett, In fact, William J. O ‘Neil’s investment success is comparable.
William O ‘Neal focused his investment philosophy on the CANSLIM stock selection system, which he created for decades. It was one of the most stable and best-performing systems in both bull and bear markets.
In a nutshell, the CANSLIM system is a classic value approach that focuses on picking leaders with good fundamentals, strong technicals and enough institutional investor support.
In the CANSLIM stock selection system, there is a Relative Price Strength Rating (RPS), which refers to the Relative Strength of stock prices. It selects strong stocks in the market according to the rank of a stock’s rise in the rank of all stock’s rise in a period of time.
In this section, we will introduce how to calculate THE ORnell RPS index and how to make stock selection strategies based on the ORnell RPS index.
The calculation of up and down indicators
How do you calculate the RPS indicator? The RPS value is between 0 and 100. For example, there are 1000 stocks in a-shares. If A stock’s 250 day rise ranks the 100th among all stocks, the RPS value of the stock is :(1-100/1000)*100=90.
The RPS value represents the 250-day gain of the stock that outpaces the gain of the other 90% of the stock. Through this index can reflect the stock trend in the same period in the market relative strength performance. So over the past 250 trading days, the RPS of the top 1% of all gainers has been 99 to 100, and the RPS of the top 2% 98 to 99… And so on.
The RPS cycle can be adjusted to suit your needs. The default is 250 days (a year), but commonly 60 days (3 months), 120 days (6 months), and so on.
Next we use code step by step to realize the calculation of RPS indicators. Here, we use tushare’s data to check the closing price sequence of The stock Weixing New Material. For data acquisition, please refer to the section of “Common Stock Trading Data Interface for Differentiation Analysis”. As follows:
Trade_date 2018-01-02 19.78 2018-01-03 20.36 2018-01-04 20.60 2018-01-05 20.77 2018-01-08 20.62 2018-01-09 20.96" The 2018-01-10 20.53... 2019-12-31 13.17 Name: close, Length: 487, dTYPE: FLOAT64 """
Copy the code
Datafame. Pct_change () datafame. Pct_change () datafame. Pct_change () datafame. In order to display more intuitive, take the 5-day increase as an example, as shown below. Of course, for the missing value, we should fill it with 0 as introduced before.
""Trade_date 2018-01-02 NaN 2018-01-03 NaN 2018-01-04 NaN 2018-01-05 NaN 2018-01-08 NaN 2018-01-09 0.059656 2018-01-10 0.008350... 2019-12-31 0.025701 Name: close, Length: 487, dTYPE: float64"""
Copy the code
Because RPS index refers to the rank of a stock’s rise in the ranking of all stocks in a period of time, we will calculate the 120-day rise and fall of all stocks, as shown below:
""" Ping An Bank vanke A Guonong Technology... Trade_date, trade_date, trade_date, trade_date... 2019-07-03 0.524483 0.220502 NaN... 0.016753 0.57659 0.147368 2019-07-04 0.50754 0.227669 NaN... NaN 0.554585 0.151579 2019-07-05 0.427692 0.181308 NaN... 0.040506 0.495763 0.096192 2019-07-08 0.395277 0.163673 NaN... -0.002092 0.38944 0.043478 2019-07-09 0.406832 0.166000 NaN... -0.044529 0.384146 0.029703 2019-07-10 0.364185 0.151599 NaN... -0.037734 0.338614 0.043651 2019-07-11 0.338594 0.163282 NaN... -0.034982 0.324803 0.048000 2019-07-12 0.384314 0.17937 NaN... -0.029768 0.333992 0.029762 2019-07-15 0.384768 0.199041 NaN... -0.037566 0.355865 0.042510 2019-07-16 0.342773 0.194754 NaN... -0.007732 0.351085 0.021825 2019-07-17 0.306298 0.185287 0.26762... -0.040764 0.339921 0.034137 2019-07-18 0.333659 0.205046 0.244923... -0.033376 0.338028 0.011834 2019-07-19 0.341323 0.192158 0.225904... 0.011796 0.370297 0.011583 0.339458 0.212476 0.138211 2019-07-22... -0.045514 0.352475-0.046967 2019-07-23 0.338521 0.181102 0.148126... -0.045337 0.406000-0.027668 2019-07-24 0.341063 0.184117 0.162694... 0.029948 0.394790 0.002004.. . . . . . . 2019-12-31 0.210449 0.103945 0.085536... [120 rows x 120 columns] ""
Copy the code
Among them, different stocks are listed at different times, so there will be missing values in the moving ups and downs of the 120-day cycle, which can be filled with 0.
Students can refer to the section of “Stock Trading Strategy: Establishing Stock Selection Strategy by Linear Regression Algorithm” to obtain all stock information in the market, and then obtain individual stock data one by one.
Of course, the author has stored the data as CSV files, so that it can be imported from THE CSV file next time, and students who need it can get it through the exchange group. The files are as follows:
2019-12-31 this day, ping an Bank 120 days up 0.210449, we use visualization method to look at the day all the stock layout, the display effect is as follows:
It can be found that among the 3000 stocks, the 120-day rate of return is in the range of [-0.11188698-0.03826044], with 657 stocks, and there are many stocks near this range. The values of the distribution are as follows:
"" [3.8.7.22.67 151.348.657.547.427.225 145.102.97 62.25 30.15 15.9 8.8 8.5 2.0 2.0. [-0.62727273-0.55364619-0.48001966-0.40639312] [-0.62727273-0.55364619-0.48001966-0.40639312] [-0.62727273-0.55364619-0.48001966-0.40639312] [-0.62727273-0.55364619-0.48001966-0.40639312] [-0.62727273-0.55364619-0.48001966-0.40639312 0.33276658-0.25914005-0.18551351-0.11188698-0.03826044 0.03536609 0.10899263 0.18261916 0.2562457 0.32987224 0.40349877 0.47712531 0.55075184 0.62437838 0.69800491 0.77163145 0.84525799 0.91888452 0.99251106 1.06613759 1.13976413 1.21339066 1.2870172 1.36064373 1.43427027 1.50789681 1.58152334 1.65514988 1.72877641 1.80240295 1.87602948 1.94965602 2.02328256 2.09690909 2.17053563 2.24416216 2.3177887 2.39141523 2.46504177 2.5386683 2.61229484 2.68592138 2.75954791 2.83317445 2.98042752 3.05405405] ""
Copy the code
We select the top 10 stocks for visual gains and losses, as shown below:
Calculation of RPS index
The next step is to sort the rise and fall values of all stocks. After sorting, we can get the rise ranking of each day, as shown below:
China tianda 0.548611 China Great Wall 0.515271 Deep chase 0.502844 Deep Mulberry A 0.490971 brand new good 0.450980 Desai battery 0.408602 Digital China 0.325490 Ping an Bank 0.257258.. Changhong Huayi -0.139588 Special message -0.140711 Xingye Mining -0.148893 Fangda Group -0.152330 ""
Copy the code
Then rank the rise and fall of each stock one by one starting from 1, and calculate the RPS index. The following is the ranking of the rise and fall of a certain day, the corresponding RPS value and stock symbol, stored in DataFrame format, we only list 10 of them, as shown below:
"" PCT name RPS n 1 0.457271 new good 90.02 0.190374 Ping an Bank 80.03 0.101951 Guonong Sci-tech 70.04 0.088542 China Baoan 60.05 0.071986 Vanke A 50.06-0.051693 shenzhou high speed railway 30.0 8-0.058116 Shenzhou high speed railway 20.0 9-0.077844 century Star source 10.0 10-0.078804 *ST beautiful 0.0 ""
Copy the code
By the end of the day, the top 10 and the bottom 10 stock gainers, as shown below:
RPS index stock selection
With the daily stock RPS ranking, we can put together the daily number one stock, as follows:
""" PCT name RPS 20190703 2.925358 99.9667 20190704 2.883792 99.9667 20190705 2.779621 99.9667 20190708 3.089744 Xingqi eye medicine 99.9667 20190709 3.130105 Xingqi eye medicine 99.9667.. . . [120 rows x 3 columns] ""
Copy the code
All right, calculating the RPS indicator is just the first step, and then we’ll do a visual stock selection analysis against that data.
Take a look at the daily RPS from July 3, 2019 to December 31, 2019. Here we only list the top 10 stocks with daily RPS, as shown below:
From which you can see a lot of familiar “monster” figure, such as Jinyun laser, Xing Qi eye medicine, Baoding technology, walker…
In the case of The Rambler and Jinyun Laser, the RPS of the Rambler started at point A and remained high, maintaining the characteristics of A strong stock. Jinyun Laser started to fall at point B and then stayed low, and the stock never recovered. So the practical significance of RPS is to find strong stocks after the first wave of gains, and then dig deeper. When strong stocks turn weak can also be observed through the change of RPS.
conclusion
It can be seen that the RPS index can initially screen out strong stocks in the market, which is of great significance for the stock selection process. On the other hand, this index still needs to be combined with other indicators of CANSLIM system to further filter the stocks that are initially screened.
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!!
Subscribe for more quantitative trading contentSmall volumesRead!!!!!
You are also welcome to follow my wechat official account to learn more about Python quantitative trading