QMT quantitative trading grid trading source code

Presumably everyone will encounter the following three problems in the process of stock trading. The first is that the stock market fluctuates most of the time.

We all know that due to its own characteristics, the A-share market, in layman's terms, is that the bull market is relatively fast, and the bear market is relatively slow. time,

For our ordinary investors, it is actually easier to obtain income during the rising market, that is, during the bull market.

But in this volatile market, if we don't have an effective strategy, we can only stand by and watch, and it is difficult to make profits.

The second problem is that when we bought a long-term optimistic stock, but finally found that the stock market has not started for a long time, and then the stock price has not risen, at this time, most of our investment funds have been occupied.

The stock price has been rising and falling. Our income is basically zero.

Without an effective trading strategy to deal with,

We dare not switch to short-term trading rashly, because the risk of short-term trading is relatively high, and then it is more difficult to choose.

The third problem is that when we misread a stock, the stock continued to fall, and finally got stuck.

What should we do at this time? For most ordinary investors. Perhaps due to its own limitations,

For example, our trading rules are not clear enough, luck and greed. During the stock rise, the profit was not stopped in time. Then when the stocks fell, they dared not admit the failure of their investment, and couldn't bear to cut the meat to stop the loss. This will lead to the stock being arbitraged,

Then I gave up on myself and fortunately deleted this kind of stock from the self-selected stocks, out of sight and out of mind.

For these three types of problems, while thinking about how to deal with them, we can retreat to think about an essential problem,

That is, in the stock market, how does our profit come about? In fact, the answer is very simple, that is, sell high and buy low, that is to say,

Our buying price is relatively low, and our selling price is relatively high, which generates profits. This is the nature of making money in the stock market. The ideal situation is to buy at the lowest price and sell at the highest price. However, this can only be an ideal state, and no one can achieve it. A more realistic strategy is that we can buy at a relatively low price and then sell at a relatively high price. Is there a more realistic strategy for this essential problem?

It is the grid trading strategy we are going to talk about today. Its basic principle is that we make a relatively clear and strictly defined trading plan in advance, and buy in batches when stocks fall. When the stock is rising, sell it in batches, and use this grid trading method or clear trading rules to capture the price difference income brought about by the price fluctuation.

The main application scenario of grid trading is the volatile market.

The function of grid trading just solves the above three problems. First, it can gradually reduce the loss of the stocks we are trapped in, and finally even turn losses into profits. Second, for stocks that we are optimistic about for a long time and have not started the market for a long time, we can use grid trading to reduce the cost of holding positions, and finally wait for the market to start to obtain relatively large profits.

third,

For investors who want to do short-term trading. In the middle of the volatile market, using this relatively strictly defined grid trading strategy to do short-term trading can effectively help us capture investment opportunities brought about by price fluctuations, so as to make profits.

Let's take a look at a specific example. Suppose we hold a stock when the stock price is 10 yuan, or open a position and buy 50,000 shares at 10 yuan, and then, as the market fluctuates, we formulate a strict online trading plan. The plan is to buy 5,000 shares when the price falls by 0.1 yuan, and sell 5,000 shares when the price rises by 0.1 yuan. When it fell to 9.9 yuan, we bought 5,000 shares for the first time. At this time, the total position reached 55,000 shares. Then, the stock price fell further and reached 9.8 yuan, which is B2.

At this time, according to the plan, I bought 5,000 shares again, and the position reached 60,000 shares. After that, as the stock price rebounded, you can see that its price came to 9.9 yuan and 10 yuan successively. Finally, it ran to 10.3 yuan. At this time, according to the trading plan, continuous selling has to be carried out. He sold 5,000 shares each time, so his position finally came to 35,000 shares.

After that, the stock price fell again, falling to 10 yuan. After a period of time, the price rose and fell, and finally returned to 10 yuan. We made three consecutive buys in accordance with the original trading plan.

Looking back at the entire transaction process, in fact, our positions have not changed. Our initial position was 50,000 shares, and the final position was still 50,000 shares. In such a fluctuating market, we used grid trading, resulting in five buy transactions. There are also five sell trades, or five pairs. Aside from the handling fee and roughly estimate the five pairs of trading transactions, the final profit is 0.1 times 5000 times 5, which is equal to 2500 yuan. We can also say that the grid trading method made us a profit of 2,500 yuan, or reduced our position, such as the cost of holding 50,000 shares, by 2,500 yuan.

After understanding the basic principles of grid trading. Next, let's take a look at the specific strategy development level,

What other factors need to be considered in grid trading? The example just now actually uses our most common grid trading method.

It is called equal grid trading method. The so-called equal grid trading method is based on the benchmark price. Cast the net at equal distances up and down, and sell a fixed number of stocks for every fixed amount of increase. For every drop of the same amount, buy the same number of stocks. Such a grid transaction,

It is called a bisected grid. Equally divide the grid, remove the transaction fee rate, and get the profit of multiplying the grid value by the number of shares bought and sold for each pair of buying and selling transactions.

 

After the introduction of the previous examples, you can find that, in fact, before carrying out specific strategy development, you need to determine five parameters. The first one is to determine the target of the transaction, that is, the stock to be traded.

What kind of stocks are suitable for online trading, or the bottom position I hold now. When is the right time for grid trading, this is the first factor to consider.

Second, in the previous example, we also mentioned that we use 10 yuan as the benchmark price to conduct grid transactions, so how should such a benchmark price be chosen and how to set it up, or how should I open a position when I don’t have a bottom position? .

Third, in the example of our online transaction just now, you can find that I increase the price by 1 yuan or decrease it by 0.1 yuan, and then trade. This 0.1 yuan is called the cell value of the grid, or the step size, so how to set such a step size?

The fourth is the quantity of each transaction we make. The example just now is 5,000 shares; fifth, careful friends may find out. Our grid trading strategy still has certain potential risks, that is, when the stock market has extreme market conditions, such as continuous rise or continuous decline, what should we do? All funds are bought in. This is obviously not good for the transaction. When the stock market is rising, if you sell continuously, it is possible to sell the bottom position, so that we cannot continue to obtain the profit opportunities brought about by the rising stock price. Therefore, the grid trading strategy should deal with this situation. There are corresponding A parameter is called the setting of the upper and lower limit of the position.

To put it simply, when the stock price falls and we buy it several times in a row and reach the upper limit of my position, we will stop buying. When the stock price continues to rise, we have reached the lower limit of the position after selling several times in a row. Do not continue to sell.

Specifically. First, the choice of stocks is what kind of stocks to use for grid trading strategies. There are four main factors to consider. One, it is best to be in the recent market, or individual stocks, which are volatile.

We can learn from the market, or the performance of stocks in the past month, or the past two weeks. For a rough reference, it is best to be in a volatile market at this time, because the most suitable for online trading is a volatile market. Second, the volatility of the stock we choose is as high as possible, and then it is in a volatile market, which is the best condition for us to conduct grid trading.

Secondly, when we conduct grid trading, when we do not have a bottom position, we try to build a position first, and then conduct grid trading. At this time, we must pay extra attention to the selection of stocks. We'd better choose stocks with good performance and long-term prospects, so that the investment risk of stocks can be controlled to a certain extent.

There is an investment product in the market called index fund, such as 50ETF, such an index fund corresponds to a basket of stocks. Index funds actually help to diversify the risk of individual stocks. In addition, the fee rate of ETF transactions is relatively low, and ETFs are more suitable for grid transactions.

second parameter. is the base price option. There may be two types of people doing online transactions. The first category is that you have already held a bottom position in stocks, and then the stock prices you hold have fluctuated significantly in the recent period. Usually our selection is the latest price. When we have no positions, we try to buy and open a stock for grid trading. At this time, we can refer to the performance of this stock in the past period of time, such as the average of the highest price and the lowest price in the past 30 days as our A reference to the opening price of buying stocks. Try to avoid high prices, and open positions when prices are high. This is a relatively unfavorable factor in our grid trading, and it also needs everyone's attention.

The third aspect is the step size of grid transactions and the number of transactions per transaction. Usually there are five options. The first one, we can set up according to some usage conventions, grid trading, there is a 1/10, or 2/20 principle. What does that mean? It means that when we set the step size, we can set 1% or 2% of the benchmark price as the step size. Then take 10% or 20% of the position or bottom position as the amount of each transaction.

For example, the benchmark price is 10 yuan, and then there are 50,000 shares in the position. According to the principle of 1/10 or 2/20 for grid trading, you can set the step size to 0.1 yuan or 0.2 yuan. Then the quantity of each transaction is 5000 shares or 10000 shares.

Second, we can base on historical market conditions, for example, the past 30 days. The highest and lowest prices of stocks are used to determine the range of online transactions. Under normal circumstances, we use historical prices as a reference, and there is a high probability that stocks will fluctuate within this range for a period of time in the future.

The third is to determine the step size based on the average volatility of the past ten days, or the real volatility ATR, and the number of grids. For example, if the number of grids is 10, the step size is ART / 10.

In addition to considering the step size of the grid transaction and the quantity of each transaction, we also need to ensure that the income of each transaction is greater than the cost, so that the grid transaction will be meaningful. In addition, we can also calculate the upper and lower limits of the position according to the number of grids and the number of each transaction.

Next, we will share with you the overall idea of ​​a specific grid trading strategy. First of all, the first step is to choose a trading product. Just like the introduction before, choose an ETF. Here we choose the Shanghai and Shenzhen 300ETF, the code is 510300.

The second step is to open a position. As we said just now, for grid trading, if you use the newly-built stocks to conduct grid trading, you hope to have a good position to open a position, which is beneficial to grid trading. Therefore, a simple moving average long arrangement strategy is used here to open positions. When the market meets the moving average long arrangement, buy. Initially buy 60% of the funds, because grid transactions will be conducted later, so we reserve 40% to ensure that there will be enough funds for subsequent grid transactions. Then the opening price is used as the benchmark price for grid trading.

The third step is to choose the step size. We use 1% of the benchmark price as the step size. Friends who are familiar with index funds may understand that the fluctuation range of the CSI 300 ETF is relatively small. Then, we choose 1% as the step size. step size. Of course, the value of the step size may be adjusted in the follow-up strategy, or for different trading varieties, this parameter can also be changed.

The fourth step is the selection of the transaction quantity. We take 10% of the opening quantity as the quantity of each grid transaction. We set the number of grids to 10, that is, there are five grids around the benchmark price, and there are no grid transactions beyond the range of the grid lines, that is, five grid lines above and below. It is also a limiting condition for the upper and lower limits of positions.

 

Next, let's take a closer look at the final grid trading strategy. It is also our idea of ​​​​writing quantitative trading strategies next. First of all, according to the benchmark price, in the strategy just mentioned, it is the price for opening a position.

There is also a 1% step size to calculate prices for the upper and lower five grid lines.

Take the example just mentioned, the base price is 10, and then the step size is 1%, then the price of the upward grid line is 10.1, 10.2, 10.3, 10.4, 10.5. Then proceed to calculate in the same way.

In the second step, we calculate the number of open positions that the grid strategy should have for each price range. Let's illustrate with an example. You can see that when the price is 10-9.9 yuan, the initial position is 50,000 shares. When the stock falls, for example, when it falls to 9.95, it will not buy or sell because it has not fallen below the grid line. The position size is Unchanged, or 50,000 shares.

Then, but when the price falls to the range of 9.9 to 9.8, the target position should be 50,000 shares plus the 5,000 shares bought, and the target position should be 55,000 shares. Here is an additional explanation to everyone that in the same price range, the target position is different when the price rises and falls. What does that mean? You can take a look, when the price is between 10 and 10.1 yuan, when it rises, it has not broken through 10.1 yuan, and the target position is actually 50,000 shares. But suppose the price drops from B4 to B3, and before it breaks through B3, you can see from the picture that the target position is 45,000 shares.

That is, when the price falls and needs to be bought, the target position is lower than when the price rises and the target position should be sold. Here is 5000 shares. Everyone needs to pay attention here.

After we calculate the target positions corresponding to the rising and falling situations in each price range. Next, we get the current position, and then calculate the amount of adjustment that should be made relative to the target position.

Then the fourth step is to judge whether the amount of rebalancing is greater than 0. If it is greater than 0, it actually triggers the grid trading conditions, and then correspondingly carry out corresponding buying and selling operations, buying or selling rebalancing. In general, the idea of ​​the strategy is like this. With the update of the market data, obtain the range of the stock price, judge whether the price is rising or falling, obtain the bottom position, calculate the amount that needs to be adjusted, and then carry out the actual trading operation. This is a cycle process.

 

Guess you like

Origin blog.csdn.net/qmtptrade/article/details/124658222