In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the common traps of Python quantitative back testing". In the daily operation, I believe many people have doubts about the common traps of Python quantitative back testing. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the common traps of Python quantitative back testing?" Next, please follow the editor to study!
Many quantitative trading friends are easy to make these types of mistakes when specifying strategies: forward-looking bias, no sliding point, no handling fee, over-optimization and so on. These mistakes can enable you to make an unprofitable strategy profitable during the back test phase, but it can have disastrous consequences in a real transaction.
First of all, the forward-looking deviation is introduced. Forward-looking bias means that the future information is taken to determine the previous transaction in the strategy review phase, which is not available in the real trading operation. Here are some typical cases to introduce:
Buying strategy 1: "if the closing price of the day breaks through the highest price of the day before, then use the opening price of the day to buy shares." obviously, it is almost impossible to close a deal at the opening price.
Forward-looking deviation is a mistake that we should try our best to avoid. How to avoid forward-looking deviation? You can use "lagging" historical data to calculate policy signals. The lagging data means that only the closing data of the "previous" trading day are used when it comes to moving averages, highest prices, lowest prices, trading volume and other indicators. For example:
Shift (1) moves the value of the sequence backward one unit while the index is constant. The aim is to get the highest / lowest price as of yesterday. In this way, the "lag" historical data can be used to calculate the policy signal.
If a trading strategy requires you to trade at the price at which the signal is triggered, then there is a situation of price theft in that trading strategy.
Many people underestimate the harmfulness of cheating. In fact, fixed-point cheating is equivalent to superimposing a straight line with a positive slope on the original capital curve (in the case of a fixed number of players in each transaction). Suppose there are 250 trading days in a year, each trading day carries out a trading of 100 shares on a stock that maintains 10 yuan for a long time, and each transaction steals a tick (tick is set to 1 point, that is, 0.1 yuan), then you can make a profit of 5000.1100 yuan more than 100 yuan for the whole year, which even exceeds your principal (10 yuan 100 yuan 1000).
However, the reality is that not only can not be lower than the price of a tick, but more is higher than the price of one or more tick. The reason is simple: most trend traders build positions at certain key points, and competition causes prices to fluctuate sharply at these key positions, and such fluctuations are usually unfavorable to programmed trading. Therefore, when building a high-frequency trading system, it is more necessary to consider and solve the problem of slippage.
The fee is equivalent to superimposing a straight line with a negative slope on the original capital curve (in the case of a fixed number of hands for each transaction). When the strategy is tested, if the fee is not included, the capital curve may vary greatly, and even the profit-making strategy may incur a loss after taking into account the fee.
The current stock trading fee consists of three parts: commission, stamp duty and transfer fee (Shanghai stock only)
Stamp duty: 1 ‰ of the transaction amount.
Transfer fee: this refers to the fee that needs to be paid to change the account name after the stock transaction. This fee is charged at 0.02 ‰ of the transaction amount.
Transaction commission: the maximum fee is 3 ‰ and the minimum fee is 5 yuan.
Suppose you buy 10000 shares at a price of 10 yuan per share. Take this as an example, calculate the purchase cost:
The amount used to buy shares: 10 yuan per share x 10000 shares = 100000 yuan
Transfer fee: 0.02 ‰ × 100000002 yuan
Transaction commission: 1000000 × 3 ‰ = 300 yuan (calculated according to the highest standard, normally less than this value)
Total purchase cost: 100000 yuan + 300 yuan + 2 yuan = 100302 yuan (buy 10000 shares, 10 yuan per share, total capital required)
If calculated at the selling price of 10.08 per share:
Stock amount: 10.08 yuan per share x 10000 shares = 100800 yuan
Stamp duty: 100800 yuan x 1 ‰ = 100.8 yuan
Transfer fee: 0.002% × 100800 yuan ≈ 2 yuan
Transaction commission: 100800 yuan x 3 ‰ = 302.4 yuan
Sales income: 100800 yuan-100.8 yuan-2 yuan-302.4 yuan = 100394.8 yuan
The slip point refers to the difference between the price of the trigger order and the final transaction price, which is inevitable. There are usually two types of reasons for the slip point:
1. Caused by sharp fluctuations in the market, insufficient market capacity, etc.
two。 Caused by network delay, unstable trading platform, etc.
Slippage is a factor that must be fully taken into account in a qualified trading strategy. If the number of slip points is set to 0 in a trading strategy, that is, the capital curve of buying and selling securities with current price orders will be better than that of the same strategy with non-zero slip points. Usually we build and test the system under the most extreme conditions, and the system is usually built above the slip point of 2 tick.
Let's talk about over-optimization. Over-optimization refers to repeatedly optimizing the parameters or rules of a strategy to the best situation in order to accommodate the noise of historical data.
If you want to do a very good capital curve of a strategy, we can optimize it like this: optimize the parameters for a specific market, and then splice the capital curve corresponding to the segmented market to get a capital curve with better performance. You can also specify not to trade on certain trading days that have performed poorly in the past. The above processed return test must perform well, simply to pursue the capital curve, this kind of strategic model is only applicable to historical accidents that occurred in the past but will not be repeated in the future, and does not consider whether it is applicable to the future market.
There are several effective ways to avoid over-optimization:
Generally speaking, the more rules of the strategy and the more parameters of the model, the more likely it is to over-fit. Therefore, the fewer parameters are used, the fewer rules can be optimized, the natural over-optimization can be well avoided, and the strategy constructed with fewer parameters is often more robust, regardless of its timeliness or robustness can surpass many complex strategies.
Reasonable selection of samples. Some strategies cover too few stocks and the time period is too short to fully show the effect of the strategy. These strategies can only be applied to individual varieties, or the capital curve may not perform so well over a longer period of time. Although some strategies cover a longer period of time, or more varieties or markets, they cover only one type of market (such as a long bull market). These strategies may perform very differently when faced with very different market conditions.
Conduct an out-of-sample test. When building the strategy model, the historical data is divided into two segments according to time. Parameter optimization and qualitative selection use the previous data (training set), and the testing of the strategy model uses the latter data (test set). The size of the two pieces of data should be roughly the same. If the amount of data is not enough, at least 1/3 of the data will be tested. The minimum content of the training set is determined by the empirical rules of sample size.
At this point, the study of "what are the common pitfalls of Python quantitative back test" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.