In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand Python's feedback framework backtrader". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Backtrader belongs to the local version of Python quantitative testing framework with relatively complete functions. Since the industry is well received, we, as quantitative traders, are supposed to have all the useful tools in one, so let's experience this framework.
The use of backtrader is described in detail in the official documentation. It is roughly divided into two steps:
Create a policy, create a policy class that inherits from backtrader.Strategy, and then customize the methods in it. There is a class attribute params in the policy class, which is used to define some calculation methods in which many indicators are built into the adjustable parameter value backtrader.indicators in the policy, such as moving average, MACD, RSI and so on. When you use it, you only need to instantiate the technical indicators used in the policy to write the trading strategy, that is, the logic of entering and exiting the market.
Create a policy decision engine (originally Cerebro, here I use the word decision) to inject the defined strategy into the decision engine, and inject the market data into the decision engine to visually feedback the test results.
These are the core parts of the framework, and of course, there are many other extensible features.
Backtrader's data loading is very flexible. Here we use data in DataFrame format, as shown below:
"
High Low Open Close Volume OpenInterest
Trade_date
2017-01-03 8.12 8.07 8.07 8.12 179801.01 0
2017-01-04 8.16 8.09 8.13 8.15 166242.35 0
2017-01-05 8.23 8.13 8.15 8.17 222902.53 0
2017-01-06 8.19 8.12 8.18 8.13 128549.96 0
2017-01-09 8.15 8.08 8.13 8.13 136700.04 0
"
The class that builds the policy is to inherit the backtrader.Strategy and then rewrite the methods in it according to your own needs. Such as _ _ init__, log, notify_order, notify_trade, next, and so on.
With regard to the metrics in the policy, backtrader has many built-in types, which can be called directly. For example, the moving average:
Self.sma = bt.indicators.SimpleMovingAverage (
Self.datas [0], period=self.params.maperiod)
Because of the built-in talib module, you can also call it this way:
# built-in talib module
Self.sma = bt.talib.SMA (self.data
Timeperiod=self.params.maperiod)
In the next method, we implement a simple double moving average strategy as the logic of the transaction. For example, the buying condition is that MA5 wears MA10; and the selling condition is that MA10 wears MA5.
With regard to policy backtesting, in addition to adding data and policies to the Cerebro, some parameters are also set. For example, the setting of broker, such as initial capital, transaction commission. You can also use addsizer to set the number of shares bought in each transaction.
After the return test, the total funds accumulated during the execution of the trading strategy are returned. What we are testing here is the effectiveness of New Hope's strategy implementation during the period from January 1, 2017 to January 1, 2020, and the final funding has changed from 10000 to 15941.95.
Because backtrader has built-in Matplotlib, we can also visualize the effect of the backtest, as shown below:
This is the end of the content of "how to understand Python's back-testing framework backtrader". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.