程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

I use Python to do a quantitative index enhancement strategy, cool! Code attached!

編輯:Python

This is Xing buidi 84 I want to quantify the sharing of the small lecture hall

author | Xing can't 、 Mies zinc selenium

A Stock is a magical place , Its biggest feature is that it does not rise for many years and will last forever 3000 spot .

Because of this , teasing A There are endless stories about stocks .

Even the Chinese men's football team is a rotten industry Diss, irony A Shares are worse than the national football team .

What I want to say is , Even though A Stocks have many problems , But it will never be as bad as the men's football team . At least A Stocks are definitely higher than the men's football team in the world .

Slide left and right to view the ranking comparison

But no matter A Which is better, thigh or men's football , Can't change The Chinese stock market has been 3000 The fact that points fluctuate up and down .

So let's explore the following A Stocks will not rise for ten thousand years The principle behind it .

And on this basis, we derive a Simple and efficient A Stock strategy , The final effect is as shown in the figure :

300 Double mystery strategy

from 07 Since, according to the strategy, the trading can be turned into 200 Many times , can Substantially outperform Shanghai and Shenzhen, which represent the market 300 Index .

How did this strategy come about , What's the principle of it ? It all starts with The Shanghai composite index Speaking of .

01

Introduction to Shanghai Stock Exchange Index

Speaking of A Shares surround 3000 When the point fluctuates up and down , By default we mean The Shanghai composite index . It contains all those listed on the Shanghai Stock Exchange 1700 More than one stock .

The Shanghai index is 2007 At the beginning of the year 2700 P.m. , During this period, it was up to 6000 spot , Then all the way down . In recent years, I have been 3000 It fluctuates around the point .

from 07 Year to 21 It's a whole year 15 Years of time only rose 36%, It really pulls the crotch .

Monthly data of Shanghai Stock Exchange Index

Although the media will default to use the Shanghai stock index to represent the Chinese stock market , But traders with some experience will not look at it .

Because everyone knows it is a The distortion The index of , Can't describe the overall market situation very well .

Then why is the Shanghai Composite Index distorted ? We take it from its Preparation method You can get the answer by starting , This is also closely related to our strategy .

02

The compilation method of Shanghai stock index

The compilation method of Shanghai stock index is Weighted average of the total market value .

That is, the daily rise and fall of a single stock will contribute to the whole rise and fall according to its market value weight .

Just looking at the definition may not be well understood , Let me give you another example .

If you are interested in index compilation , Welcome to scan the QR code below or add I wechat xbx719 Communicate with me .


1

case analysis

As shown in the following table , The total market value of Moutai that day was 2.6 One trillion , It is the highest among all Shanghai stock exchanges . Press... In turn Market value ranking , The final market value of Sanmao is only 12 Billion .

Put these stocks Total market value You can get the total market value of the Shanghai stock index (48 One trillion ).

Data time :2021 year 12 month 31 Japan

We use this to calculate the proportion of each stock in the total market value of Shanghai Stock Exchange , Like Maotai The market share is 5.37%(25752/479553=5.37%), And the lowest ranking Sanmao is only 0.05%(12/479553=0.05%)

Share of stock market value

Maotai rose or fell on the same day -1.2%, So its impact on the market

Namely 5.37% * (-1.2%). Other stocks can do the same .

We can finally add up these values to get The Shanghai stock index rose or fell by 0.57%.

From this we can know , The larger the market value of the stock , The greater the impact on the index .

Obviously, Moutai has the greatest impact on the index , The influence of Sanmao on the index can be basically ignored .

such The market value is very different This phenomenon is very serious in the constituent stocks of Shanghai Stock Exchange Index .

2

Weight drag index

We still use 2021 year 12 month 31 Take the daily data as an example .

A total of 1750 Share only

Top market capitalization 100 The proportion of the stock weight exceeds 52%, The rest of the 1650 The total number of stocks only accounts for 48%. This is very similar to our wealth gap data .

So the top 100 The stock of Shanghai Stock Exchange has a great influence on the trend of Shanghai Stock Exchange Index .

Let's look at the top stocks in the table below , E.g. XX line 、 In a certain oil 、 So and so on .

Before market value 100 Stocks

You can easily start from their month K It can be seen from the line diagram These stocks are basically in a millennium period The situation of , Just like Chinese football .

      Slide left and right to see more

But there is also a reason why these stocks have not risen .

Most of them are state-owned enterprises , Just serve the party and the people , There is no need for shareholders ( capitalist ) be responsible for .

To make a long story short , The essential reason why the Shanghai stock index has not risen for many years is : For the Shanghai Stock Index Heavyweights with the greatest impact have not risen or even fallen for many years , As a result, the index has been in 3000 Around the point .

Now that we understand this phenomenon , You should think about how to use it .

For example, I am very curious , If Remove or reduce the influence of these heavyweights , Also buy the constituent stocks of Shanghai Stock Exchange Index , Whether its revenue will increase ?

03

Shanghai Stock Exchange equal weight index

1

The concept of equal weight

Let's do an experiment here , Still calculate the daily rise and fall of these stocks . But the rules Each stock has an equal impact on the overall index .

Index equal weight Compilation Rules

For example, both Maotai and Sanmao have an impact on the index 1/1750, It has nothing to do with market value .

Data time :2021 year 12 month 31 Japan

It is equivalent to that we re compile a Equal weight index , It no longer favors large cap stocks .

At the same time, we continue to keep the constituent stocks of the new index consistent with the Shanghai index every day , But they affect each other Different weights .

What will be the final return of this equal weight index , Will it surpass the original Shanghai Composite Index ?

2

Data code

Want to know the result , First, we need the data of every stock every day .

I have helped you sort out the data , Including all the stocks ( Including delisted shares ). The data is shown in the figure :

Data presentation

It covers the first day since the listing of this stock , Daily Open higher and close lower prices and total market value .

With the data , adopt Python Code calculation can get the corresponding results .

If you need data and code , You can scan the QR code below or add me WeChat xbx719, They can be sent to you for free .

3

Equal weight index results

The final result is shown in the figure below :

The Yellow curve in the figure represents the Shanghai Stock Exchange Index , from 2007 It has risen since 36%, Very hip pulling .

And the blue curve representing the equal weight index rose 8.59 times . although 15 The year has only turned 10 Less than times , But also Far better than the Shanghai composite index itself .

The equity index net value performs better

Now let's take a look at the sentence of a football commentator “ Vietnam's stock market turned A Burst 6:1”.

If you put the card on Equal weight index and Vietnam stock market from 2010 If you start to make a comparison in, you will find , Even though the Vietnam index has started to soar in recent years , But it The overall increase is still not better than the equal weight index . I'm afraid this will disappoint Mr. Liu .

Shanghai Stock Exchange equal weight index vs Vietnam index

Through the explanation just now , We already know , The index does not really reflect the rise and fall of the market because of the compilation .

We can go through To achieve higher growth through equal rights purchase of all constituent stocks .

4

Process practice

But this knowledge can not only let us understand the rules of index compilation , It doesn't really help the investment .

Because there is no way to put into practice . You don't have so much money to buy so many stocks .

You don't have the ability to order so many stocks at once , If you place an order one by one, you don't know how long it will last .

Unless you like us can realize the programmed automatic order .

Interested parties can watch this video

Moreover, there is no corresponding index fund in the market Equal rights track all shares Of , So you Basically, it is difficult to realize this benefit .

But now that we know A Stocks do not rise because Large cap stocks detract from the index . Then we can think the other way , If Buy only small cap stocks , Can we achieve higher returns ?

04

Strategy building

1

Small market value strategy

We can try to build a simple stock selection strategy .

Security Selection

On the last trading day of each month Shanghai and Shenzhen 300 The constituent stocks of the index are sorted by market value .

selection The smallest market capitalization 10 Stock only , At the opening of the first trading day of the next month, all positions are bought .

After that, no matter how it goes up or down Hold immovable , Wait until the closing of the last trading day of the month to sell out the shares .

Then continue to select the stocks with the smallest market value at this time and buy , such cycle .

2

Illustrate with examples

For example, I am in 2006 Year of 12 month 29 Shanghai and Shenzhen of the same day were selected on the th 300 Among the constituent stocks The smallest market capitalization 10 Stock only .

Then I'll be 2007 year 1 On the first trading day of the month Equal position purchase They are not held until 1 month 31 We'll sell out in the next day .

Then choose the latest one 10 Buy only stocks , Repeat the above operation every month .

In this way, everyone should be able to fully understand , This is a very Simple strategy , It's easy to implement .

Because you only need to trade once a month , On the market software Sort by market value You can know what to buy .

So what are the benefits of this strategy ?

3

Strategy results

I do not recommend manual calculation of the benefits of this strategy .

We used Python Programming and corresponding stock data to get this result .

It is worth mentioning that , We have a column of stock data here to confirm this Whether the stock currently belongs to Shanghai and Shenzhen 300 stocks Of .

Component stock screening

This data is very rare , If you are interested in data and code , You can scan the QR code below or add I wechat xbx719, They can be sent to you for free .

The running result of the code is shown in the figure :

The blue curve shows the strategy , The final benefits are 26 About times , Far outperformed Shanghai and Shenzhen represented by the Yellow curve 300 Index .

There is a fly in the ointment , The maximum fallback of the strategy is as high as 70%, It's inevitable . Because the strategy requires always Full position holding Stocks , When all the stocks begin to fall , We are not immune .

But in actual transactions , You can choose the time , You can rely on your smart little head to avoid this biggest retreat .

But the cruel reality is , For most people , The more time you choose , On the contrary, the greater the pullback , The lower the payoff .

4

Strategy development

I suggest you try this strategy , Because it is essentially a Shanghai Shenzhen 300 The index of .

The key is , This strategy we only need to buy each time 10 Shares , Many people can afford it .

You can even buy... Every time 3 Stock only , See how it works .

You can scan the QR code below or add me WeChat xbx719, Ask me to modify the parameters after I ask for the code and data , I won't go into details here .

In addition to changing the number of shares , Can we extend this strategy further ?

05

Strategy optimization

1

whole A Introduction to small market value stock strategy

We were in Shanghai and Shenzhen 300 The constituent stock of Choose stocks in the stock market , If in Of all the shares Choose the stock with the smallest market capitalization , What will happen ?

Of course , To avoid buying stocks that are easily delisted , We need to add a constraint .

I hope Net profit of the selected stock in the last four quarters , That is, net profit TTM Greater than 0.

That is to say, the company we have chosen has at least recently 1 Years are money making . And this stock cannot be ST Shares or stocks about to be delisted .

under these circumstances , I pick and buy and hold every month The smallest stock by market value A month , It will not be sold until the last trading day of this month .

2

case analysis

For example 2006 year 12 end of the month , I chose 10 Stock only , Then in 07 year 1 Buy and hold the whole on the first trading day of the month 1 month .

I will be in 1 month 31 At the close of the day, the company sold its shares , And choose a new one 10 Stock only , stay 2 Buy and hold on the first trading day of the month , And so on .

In this way , How much will my final income be ? We still have to use data and Python To achieve .

If you need this code and data , You can scan the QR code below or add me WeChat xbx719, Can be directly sent to you .

3

Strategy results

After running the code, the final result is shown in Figure :

The blue curve represents the net value of the strategy

This seemingly simple strategy starts with 2007 Year begins , After deducting all the service charges, it finally turned over 284 times .

Annualized income Also from the original 25% To improve the 46%, The maximum pullback has also been reduced .

However, the maximum pullback is relatively large , You can try to further optimize the strategy .

And when we compare the capital curve of the two strategies , You will find that their performance is different in different time periods .

2015 Years ago all A The strategy of selecting small market value stocks has increased even more , but 15 So far this year , Shanghai and Shenzhen 300 The small market value stocks selected among the constituent stocks are better .

This is due to differences in strategic styles , Different strategies apply to different markets . So we sometimes run multiple strategies at the same time .

Of course, this is another topic , If you are interested, you can praise more , You can also scan the QR code below or add me WeChat xbx719 communication . We can talk about it later How to run with multiple strategies 、 Multi Strategy Selection and Multi Strategy rotation .

06

The end of the

thus , We go through A There is an inherent flaw in the stock index , Brings out an interesting trading strategy .

In fact, there are many similar interesting phenomena and extended trading strategies .

If you want to see it, you can praise it more , Or add me WeChat xbx719 communication , Like breaking 100 Words , After that, I will issue another issue to have a chat A Other interesting phenomena of stocks .

To make a long story short , We need to learn to Science investment , You can't just pat your head subjectively , Then make decisions based on some inaccurate experience .

In the final analysis , We speak with data .

07

Postscript

At the end of the article , Share a little with you Experience of quantifying investment .

Many people ask me how Xiaobai started Learn to quantify investment , There's nothing to The book list recommends .

My advice is Never read a book directly .

You're looking for Ben Programming Book reading , Then knock it out “Hello World” It's over ; You're looking for Ben mathematics Book reading , Then I fell asleep when I saw the formula on page 7 .

A better way to learn is to do practical projects , stay practice Learning quantitative strategies .

Research Report is a good quantitative practice project .

A research paper is a strategy , The authors are highly educated securities analysts with an annual salary of millions , All you have to do is Read the Strategy Research Report , And implement it in code .

During this period, I will learn nothing , Where will not order where , Holding Problem solving mentality To learn , Yield twice the result with half the effort .

Familiar with 300 tang poems , You can sing even if you can't write a poem .

So where can we Obtain the research report Well ?

You can scan the QR code below or add mine WeChat xbx719, I have different categories here Tens of thousands of research reports , Will Real time updates .

I can distinguish the difficulty , selected Then send it to you .

Add me WeChat xbx719 after , It's fine too Exchange quantitative investment related issues , I'm busy , The reply was slow , But I will reply to everything I see .

Have a good chat , We had a good chat , A lot of quantitative data 、 All the information is OK it's for you Of .

You can also turn over me Circle of friends The content of , quite a lot Quantify dry goods . Some content that will not be publicly developed , Will say in the circle of friends .

Contact the author


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved