backtrader进行期货回测要注意的问题:保证金等设置,拼接滚动合约
2021/4/24 10:26:51
本文主要是介绍backtrader进行期货回测要注意的问题:保证金等设置,拼接滚动合约,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
点此获取backtrader技术教程
==================
很多同学问如何用backtrader进行期货回测。以下给个例子,与普通股票回测不同的是佣金的设置方式。
import datetime # For datetime objects import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind import pandas as pd import numpy as np from datetime import datetime class Par_SAR(bt.Strategy): params = (('period', 2), ('af', 0.02), ('afmax', 0.2)) def __init__(self): pass if __name__ == '__main__': # rb 15 min data brf_min_bar = bt.feeds.GenericCSVData( dataname='E:/Quant/Backtrader/backtrader-master/China_Market/data/rb_15m.csv', timeframe=bt.TimeFrame.Minutes, #fromdate=datetime.datetime(2019, 5, 1), #todate=datetime.datetime(2020, 6, 1), nullvalue=0.0, dtformat=('%Y-%m-%d %H:%M:%S'), datetime=1, time=-1, high=4, low=5, open=3, close=6, volume=-1, # openinterest=-1 ) # Add the Data Feed to Cerebro cerebro.adddata(brf_min_bar) #cerebro.resampledata(brf_min_bar, timeframe=bt.TimeFrame.Days) # Add strategy cerebro.addstrategy(Par_SAR) # 设置初始资本为100,000 cerebro.broker.setcash(1000000.0) # 设置期货佣金模式 cerebro.broker.setcommission(commission=0.0001, margin=2800.0, mult=10.0) cerebro.run()
以上佣金设置,实际上使用了backtrader内置的佣金类CommInfoBase,该类可设置的参数如下。当然,多数参数采用默认值即可,无需设置,如以上案例,只设置了3个参数,其他都是默认值。以下参数中的leverage,可能与期货也有关系。另外,该类也可以考虑借款利息。
Params:
commission
(def:0.0
): base commission value in percentage or monetary unitsmult
(def1.0
): multiplier applied to the asset for value/profitmargin
(def:None
): amount of monetary units needed to open/hold an operation. It only applies if the final_stocklike
attribute in the class is set toFalse
automargin
(def:False
): Used by the methodget_margin
to automatically calculate the margin/guarantees needed with the following policy- Use param
margin
if paramautomargin
evaluates toFalse
- Use param
mult
and usemult * price
ifautomargin < 0
- Use param
automargin
and useautomargin * price
ifautomargin > 0
- Use param
commtype
(def:None
): Supported values areCommInfoBase.COMM_PERC
(commission to be understood as %) andCommInfoBase.COMM_FIXED
(commission to be understood as monetary units)
The default value ofNone
is a supported value to retain compatibility with the legacyCommissionInfo
object. Ifcommtype
is set to None, then the following applies:margin
isNone
: Internal_commtype
is set toCOMM_PERC
and_stocklike
is set toTrue
(Operating %-wise with Stocks)margin
is notNone
:_commtype
set toCOMM_FIXED
and_stocklike
set toFalse
(Operating with fixed rount-trip commission with Futures)
If this param is set to something else than None
, then it will be passed to the internal _commtype
attribute and the same will be done with the param stocklike
and the internal attribute _stocklike
stocklike
(def:False
): Indicates if the instrument is Stock-like or Futures-like (see thecommtype
discussion above)percabs
(def:False
): whencommtype
is set to COMM_PERC, whether the parametercommission
has to be understood as XX% or 0.XX
If this param isTrue
: 0.XX If this param isFalse
: XX%interest
(def:0.0
)
If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling
The formula:days * price * abs(size) * (interest / 365)
It must be specified in absolute terms: 0.05 -> 5%
Note
the behavior can be changed by overriding the method:_get_credit_interest
interest_long
(def:False
)
Some products like ETFs get charged on interest for short and long positions. If ths isTrue
andinterest
is non-zero the interest will be charged on both directionsleverage
(def:1.0
)
Amount of leverage for the asset with regards to the needed cash
另外,backtrader还允许将不同到期日的期货合约滚动拼接起来形成连续期货(The RollOver Data Feed),例子如下
import backtrader as bt cerebro = bt.Cerebro() data0 = bt.feeds.MyFeed(dataname='Expiry0') data1 = bt.feeds.MyFeed(dataname='Expiry1') ... dataN = bt.feeds.MyFeed(dataname='ExpiryN') drollover = bt.feeds.RollOver(data0, data1, ..., dataN, dataname='MyRoll', **kwargs) cerebro.adddata(drollover) cerebro.run()
RollOver类有两个参数控制如何拼接合约,checkdate
和 checkcondition
。详细描述参考 Data Feeds - Rollover - Backtrader
这篇关于backtrader进行期货回测要注意的问题:保证金等设置,拼接滚动合约的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享
- 2024-07-03微信支付提示订单号重复-icode9专业技术文章分享
- 2024-07-02微服务启动nacos注册上去了,但是一直没有收到请求-icode9专业技术文章分享
- 2024-07-02如何检查文件的编码格式-icode9专业技术文章分享
- 2024-07-02sublime 更改编码格式-icode9专业技术文章分享
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享