Welcome to subscribe to the Python Data Analysis Practical: Building a Quantitative Trading System for Stocks booklet. After you finish this booklet, be sure to use your knowledge to help us analyze stocks!

preface

Recently, the a-share market is hot, for the shareholders, to choose A strong stock is the dream thing, because only the strong stock can make their earnings outperform the market, otherwise can only look at the index rising, and the hands of the stock is calm, frustrated!

Believe everybody heard quantitative trade this thing! Nevertheless at present domestic and foreign introduce quantitative book, course and so on, it is the person of financial background writes mostly, so their Angle and our common stock people can be a little different. Partial research and analysis of the model algorithm may be we do not understand, do not use, some quantitative system for the establishment of institutional trading, is not suitable for small funds of ordinary shareholders.

So, what is the suitable way for ordinary shareholders to open quantitative trading? This article will use a ground gas to select the stock scene to share with you, how to use the ordinary shareholders quantitative trading!

What is a jump gap

K line shape has a very powerful shape – jump vacancy. Jump vacancy mouth to point to the blank interval that did not trade appeared between adjacent two K line, when today low price and yesterday high price did not overlap between part, call up gap, when today high price and yesterday low price did not overlap between part, call down gap.

The stock price gap is not only a reflection of the day’s intense investor sentiment, in many cases, such a gap to judge the future of the market is of some significance.

So we used the tool of quantitative trading to filter out stocks that have recently jumped vacancies, because these are the ones that are going to be stronger in the afternoon.

How to implement

The first step is to obtain the individual stock data of the target sector in the A market. We take securities, real estate, cement, banking, healthcare, insurance, semiconductor and components as the analysis target.

There are many ways to get stock data, such as Baostock, Tushare, Converging, Access letter… Will do. Here we take Tushare as an example, first get the list of stocks, the code is as follows:

df_basic = pro.stock_basic(exchange=' ', list_status='L')
# Exclude all new shares listed after 2017
df_basic = df_basic[df_basic['list_date'].apply(int).values < 20170101]

# Remove ST shares
df_basic = df_basic[df_basic['name'].apply(lambda x: x.find('*ST') < 0)]

df_basic = df_basic[(df_basic["industry"] == u"Securities") | (df_basic["industry"] == u"National Estates")
                    | (df_basic["industry"] == u"Bank") | (df_basic["industry"] == u"Cement")
                    | (df_basic["industry"] == u"Insurance") | (df_basic["industry"] == u"Health care")
                    | (df_basic["industry"] == u"Semiconductor") | (df_basic["industry"] == u"Components")]
get_codes = dict(zip(df_basic.ts_code.values, df_basic.industry.values]))                   
Copy the code

Daily (ts_code=code_val, start_date=start_val, end_date=end_val); end_date=end_val.

At this point, we’re done with the data.

In the second step, we should design a strategy to judge the jump gap. Here, our algorithm is as follows:

  • If today is an uptrend, today’s low is greater than yesterday’s high, and the threshold is an upjump vacancy;

  • If today is a downtrend, yesterday’s low is greater than today’s high, and the set threshold is a downward jump vacancy.

Let’s write a policy for this purpose. The key code is as follows:

for kl_index in np.arange(0, self.stock_dat.shape[0]):
    today = self.stock_dat.iloc[kl_index]  If the version prompt is deprecated, use LOC or ILOC instead
    today = today.copy()
    if (today['changeRatio'] > 0) and ((today.Low - today.preClose) > jump_threshold):
        # up null (today lowest - yesterday close)/ threshold
        today['jump_power'] = (today.Low - today.preClose) / jump_threshold
        self.jump_pd = self.jump_pd.append(today)
    elif (today['changeRatio'] < 0) and ((today.preClose - today.High) > jump_threshold):
        # down empty (yesterday close - today highest)/ threshold
        today['jump_power'] = (today.High - today.preClose) / jump_threshold
        self.jump_pd = self.jump_pd.append(today)
Copy the code

The light is more than the threshold of the short jump is not enough, we can combine the volume to superposition judgment, after all, the bottom of the large short jump up, that the stock is more strong.

So with that said, let’s put the data into perspective, run the program and see if we can pick the strong stocks.

Let’s take a look at some of these logs!

ChangeRatio: rise or fall; PreClose denotes yesterday’s closing price; Jump_power Indicates the jump_power.

The condition of stacking is: the volume is greater than the recent average *0.5; That’s more than 2%.

For example, when detecting 000783, we found that the qualified jump gap appeared on June 15 and July 6 respectively. In particular, there were several trading adjustments after the June 15 jump gap, building momentum for the next pull up. It would be instructive if we could look at this gap on June 15!

conclusion

Through this simple and practical stock quantitative scenario, I hope to give the majority of friends for quantitative trading have an intuitive feeling.

Then, we should upgrade their own way to fry, the stock before their own that set of methods, abstract into a strategic model, with quantitative methods to the whole market back test evaluation, and then let the program to help us monitor the trend of the market. This is the quantitative trade that ordinary shareholder place suits opens way!

— — — — — — — — — — — — — — — — — — — — — — — —

If you want to have a more comprehensive and systematic introduction to the knowledge points involved from 0-1 way, here I recommend my book to youQuant Trading in Python Stocks from Beginning to Practice! Tmall, JINGdong, dangdang fully open for sale!

At the same time, you are welcome to follow my wechat official account [Yuanxiao Master Takes you to quantitative Trading in Python] to learn more about quantitative trading in Python