Backtrader strategy pits: strategy preparation should pay special attention to pits on suspension days

"Sweeper Backtrader Awesome Tutorial Series One"

I have published a few articles about the issue of trading suspension when formulating strategies. After this article has been summarized and supplemented, I will publish it again. The content of this article is also published on the WeChat official account: optMaster. The articles on the official account are classified for easy retrieval and navigation on demand.

1 Introduction
A student found a strange phenomenon during the backtest. As shown in the figure below, on March 11, 2010, the 600,000 Shanghai Pudong Development Bank's purchase order was executed three times, and many of them were invalidated due to insufficient cash.

This is because 600000 was suspended from February 26 to March 10, 2010, so orders placed from February 25 to March 10 can only be executed on March 11. So the three were implemented on March 11, and the other cash was not enough.

This situation can be controlled in the program, such as not issuing orders on the suspension day. There are such cases in the tutorial as follows, where data0 is the index, and the date of the suspension stock on the day will not be equal to the date of the index (for reasons, please refer to the strategy iteration table in the tutorial) , So do not enter the list of possible orders, so as to ensure that orders will not be issued on the suspension day.

 

 

The above is just one of the strange consequences of the suspension. In the strategy backtest, the special circumstances of the trading suspension day must be considered in both the creation and execution of orders. Ask yourself your strategy:

(1) Is it allowed to place orders on the trading suspension day?

(2) The day after the order is placed is the trading suspension day, the order will not be executed. Does this affect the strategy?

(3) After placing an order, if the stocks are suspended for a long time tomorrow, your order may not be executed. Does it affect the strategy?

(4) If the stocks are suspended or even delisted for a long time starting on a certain day, how should you deal with your existing positions?

(5) In the case of multi-share operations, some stocks are not traded at the beginning (unlisted or early suspension), and the minimum period of iteration of these stocks will affect other stocks.

 

2 Basic principle: delete the suspension day record before entering the strategy

Since the backtrader iterates by bar, theoretically there should be no bar on the trading suspension day, otherwise it will easily lead to the transaction on the trading suspension day. Therefore, the record of the suspension day must be deleted in advance before being injected into the engine, so that the bar on the suspension day will not appear in the strategy.

Some data sources also have a record of the stock bar on the day of trading suspension, but only add a sign to indicate the suspension. This is a place that many people easily overlook. Failure to delete the suspension record will lead to a series of inexplicable problems.

Therefore, under the assumption that the suspension date record has been deleted, let us clarify some issues.

 

3 some scenes

3.1 Single stock strategy scenario

If your strategy only requires data for one stock, that is, only one market data, then the problem is relatively simple. Since there is no bar on the trading suspension day, the strategy will of course not iterate to the bar on the trading suspension day, so it is impossible to place an order on the suspension day or execute the order on the suspension day, because these two actions must be iterated to a certain bar. do. That is, the system will automatically ensure that you can only place an order on the trading day, and the order can only be traded on the next trading day (if it is a market order).

 

3.2 Multi-share strategy scenario

This scenario is more complicated and involves the understanding of a composite strategy iteration table composed of multiple shares. This strategy iteration table has a bar that spans multiple stocks. If the corresponding stock is suspended on the same day, a virtual bar will occupy the position. . That is to say, the strategy may iterate to this virtual bar. Therefore, it is also possible to place an order on the trading suspension day. However, this order will not be traded on the subsequent virtual bar. It can only be traded on the actual trading bar. This is Backtrader guarantees.

If you don’t want to place an order on the trading suspension day, you have to write some code to control it. To deal with this situation, you must understand the structure and use of the strategy iteration table across multiple data. It is recommended to carefully read our technical tutorial "Sweeping the floor The content of the strategy iteration table in "Monk Backtrader" .

Sometimes, after we place an order for a certain stock, the stock will be suspended for 10 days, for example, on the 11th day, there may be a buy or sell order many days ago mixed in the normal order, and some people’s orders are reserved. The order, but this mixed order will make the order of the order seem to be out of order. Several users encountered this problem. They all fell into the pit at the beginning. They could not find the reason for the disordered order. In fact, they have forgotten this stem.

3.3 Treatment of long-term suspension

Some stocks will be suspended for a long time after you place an order, or even delisted from the market the next day. You must carefully consider how to deal with these situations during the backtest.

For long-term trading suspensions, such as trading suspensions for several months, you can use limit orders with expiration dates to solve them, making the order invalid. However, the market order currently does not support the validity period. You may need to change the backtrader code so that the market order also supports the validity period.

Generally, there will be announcements for trading suspensions or even delistings that may take several years. Then iterating to the announcement day, you should immediately clear the existing positions and prohibit issuing orders afterwards. There should be a column in the original data that indicates that trading will be suspended for a long time. Usually, set the flag to 1 in the bar on the announcement day, and then when the flag is found to be 1 during iteration, additional processing is required.

Guess you like

Origin blog.csdn.net/qtbgo/article/details/111468614
Recommended