[Vnpy Quantitative Investment from Scratch] 18. Automatic monthly change and simulated main continuous contract

[Vnpy Quantitative Investment from Scratch] 18. Automatic monthly change and simulated main continuous contract

overview

There is an obvious difference between futures contracts and stocks, that is, futures contracts have a delivery time. Most varieties have a contract that is due for delivery every month, but not every contract has the same trading volume. Therefore, we generally call the contract with the largest trading volume of the same product the main contract. Due to the relatively large trading volume of this type of contract, the bid-ask spread will be relatively small when placing an order at the market price, so that the transaction price will be more cost-effective. If it is a month with very small trading volume, such as the newly created farthest month contract, hundreds of lots may be traded a day, and the price difference between buying and selling pending orders will be as high as tens or even hundreds of points.
When the delivery date of the main contract is getting closer, most traders will transfer their positions to the next main contract. For us in quantitative trading, this is also something that needs to be considered. If the automatic month-changing work cannot be completed, it will be necessary to manually adjust the strategy configuration and migrate positions every once in a while, which will be a very tedious and error-prone work. For developers, it is this kind of work that is worthy of the goal of the development process, freeing people from repetitive and trivial work.

Policy change month configuration

We have written two types of strategies before, one is the DualThrust strategy with a very short trading cycle, and the other is the Turtle strategy with a longer trading cycle. The moon-changing steps of the two strategies are also slightly different. Below we will explain how to switch the contract to the next month.

short term strategy

The short-term strategy here does not simply refer to the strategy with a short trading cycle. The short-term strategy I want to define has the main feature that in the strategy data file cta_strategy_data, except for the built-in variable of pos, other variables have no effect on the transaction after the month is changed. Strategy. Taking the author's DualThrust strategy as an example, the opening price required to calculate the recent ATR and the opening point of the day can be obtained by directly loading historical data. It is also only necessary to record the highest and lowest prices after opening a position on the day.

Guess you like

Origin blog.csdn.net/u011687355/article/details/131081561