Quantitative backtesting of position management based on backtrader

73402d13823e2dce65861e38bab299f1.gif

Overview of position management methods

Commonly used warehouse management methods

The method of position management is a key component in a trading strategy used to determine the amount of money that should be invested in a particular trade. Here are some common position management methods:

fixed amount method . In this method, a fixed amount of money is invested per trade. This approach is simple and easy to implement, but may not be flexible enough to adjust to market conditions and the trader's risk tolerance.

fixed ratio method . A fixed percentage of the total funds invested in the account per transaction. This approach allows trading position sizes to be adjusted accordingly as the account value grows or decreases.

Kelly formula. The Kelly formula is very famous in position management, and determines the best position for each transaction according to the ratio of transaction winning rate and odds. By using the Kelly formula, traders can manage risk while maximizing expected return.

Rectangular position management method. Divide the positions into equal parts, and each position is the same amount, such as thirds, fifths or tenths. This method is suitable for shocking the market, can reduce costs and share risks.

Funnel-shaped position management method. Divide the position into several parts from small to large, such as 10%, 15%, 20%, 25% and 30%. This method is suitable for a long-term decline in the market. In the early stage of the decline, first use a small position to build a position, and then gradually increase the intensity of the increase as the market further dips.

Pyramid position management method. Contrary to the funnel-shaped position management method, this method is to invest a large amount of funds when opening a position, and gradually reduce the proportion of subsequent positions. It is suitable for a certain rising market, intervene when an upward trend has been formed, and take advantage of the trend to obtain profits.

Volatility Adjustment Method. Adjust positions according to market volatility. In a market with higher volatility, a trader may reduce his position to reduce risk; in a market with lower volatility, a trader may increase his position to increase his return.

ATR (Average True Range) position management . Use the ATR indicator to adjust trading positions. When the ATR is high, the trader may reduce the position to reduce the risk; when the ATR is low, the trader may increase the position.

How to choose the right position management method?

Each method of position management has its advantages and disadvantages. Traders should choose the most suitable position management method according to their trading goals, risk tolerance and market conditions. Different trading strategies may exhibit different characteristics in different market environments. When choosing a position management method, you should first analyze your trading strategy and understand how it will perform in different market situations .

Assess risk tolerance . Your risk tolerance determines how much risk you are willing to take on a trade. According to your risk tolerance, choose an appropriate position management method to balance benefits and risks.

Consider the market environment . Different market environments may require different approaches to position management. In a volatile market, the rectangular position management method may be more appropriate; in an upward trend, the pyramid position management method may be more applicable; in a falling market, the funnel-shaped position management method may perform better.

Consider funding size. When choosing a position management method, you also need to consider the size of your account funds. For smaller funds, the fixed amount method or fixed ratio method may be more appropriate; for larger funds, the volatility adjustment method or ATR position management may be more suitable.

Do a backtest . Before actually applying the position management method, conduct historical data backtesting to evaluate the performance of the strategy in different market environments. Backtesting can help you find the best position management method for you and identify possible problems.

The following is a quantitative backtest and comparative analysis of several commonly used position management methods based on backtrader.


fd0bb3e4807236f389b48d7fb89b66bb.gif

backtraderbacktesting

Next, use qstock to obtain transaction data. Based on the backtrader backtesting framework, use the simplest double-average trading strategy to conduct quantitative backtesting with Xiaocheng Technology, Shenzhou Taiyue, Gree Electric Appliances, and Kweichow Moutai as the targets, and simply compare different position management. income situation. Knowledge Planet members can obtain the complete code on Python Financial Quantitative Knowledge Planet.

The initial capital of backtrader is set at 10 million, mainly to facilitate the backtesting of Kweichow Moutai and Gree Electric Appliances. The post-recovery price is relatively high, and the backtesting results cannot be reflected normally if the funds are small. Let’s first look at Xiaocheng Technology. The fundamentals of individual stocks are relatively poor, and they are speculative stocks with very large fluctuations. The trading strategy based on the double moving average can obtain positive returns during the backtest period, among which the funnel position management method has the greatest benefit compared to other methods.

3615d6338511d192f0438bf3a67b0fe4.jpeg

5d7d353d7b2164e966c1c3216fc21532.jpeg

The stock price trend of Shenzhou Taiyue is somewhat similar to that of Xiaocheng Technology. They are both speculative stocks on the GEM. Recently, artificial intelligence and game concepts that have emerged with the concept of chatgpt have been popular, driving the stock to skyrocket in the short term. The backtest results show that the stock's position management performance using the Kelly formula method is the best.

152f817a76b979f503b7079122fc2fc9.jpeg

a679a4ab664dc041931c12902db6eea9.jpeg

Both Gree Electric Appliances and Kweichow Moutai are value investment stocks. From the perspective of stock price trends, both have experienced a long-term upward trend. The backtest results show that Gree Electric and Kweichow Moutai use rectangular position management to achieve higher returns than other methods.

77c80803737a52b11e271317462578d5.jpeg

6516ab1cc2e3bd69eea902e0ab125d4f.jpeg

47b68e80735d91f72ed711610528d91e.jpeg

3606b6c3932186998c7ee9d010f054b3.jpeg

From the above backtest results, it can be seen that no single position management method can perform well in all situations. Among them, the rectangular position management method performed well in several sample backtests, and the Kelly formula performed well only for individual stocks such as Shenzhou Taiyue. In the A-share market, the position management of individual retail investors in terms of quantitative investment needs to clarify their own investment goals and risk tolerance, choose an appropriate position management method, and disperse funds (of course, position management is not very meaningful if the funds are too small) . At the same time, follow the market trend to make timely adjustments, strictly implement the stop loss strategy, and regularly evaluate the strategy performance.

46858317702884679afea2d34fe829ec.png

About Python Financial Quantification

bbeaa189ba1df48e0f4d4d19b69a5c30.png

Focus on sharing the application of Python in the field of financial quantification. Joining Knowledge Planet, you can get free qstock source code, more than 30 g of quantitative investment video materials, quantitative finance-related PDF materials, official account articles Python complete source code, direct communication with bloggers, answering questions, etc. Add personal WeChat sky2blue2 to get a 15% discount.

fc2e7c8714d8cff299e5518c83f0bfa0.jpeg


backtrader position management backtest code (the complete code can be obtained on Knowledge Planet)

#导入包
import qstock as qs
import pandas as pd
import backtrader as bt
from datetime import datetime, timedelta

Guess you like

Origin blog.csdn.net/ndhtou222/article/details/130497974