Backtrader 量化平台研究
Backtrader 中国期货回测
Backtrader 简介
先说几句题外话,Backtrader 到底谁在用?一下内容转自backtrader官方网站中References部分:https://www.backtrader.com/home/references/who-is-using-it/
Who is using it
我简单说几点:
- 2家 Eurostoxx50 银行
- 6家量化私募
不过这些都是很早以前的数据,还有一些做能源交易的对冲基金。
reddit里面有个algo trading 板块,里面有人提了backtrader到底有多火的问题?一下引用了部分:
No. There is not a list. This is actually outdated: the number of banks remains at 2 (there may be more, but I don’t know it), but there are more than 6 firms using it for internal purposes, including for example companies working in energy markets, due to the compensation feature which allows buying and selling different assets which compensate each other (this is probably not available in zipline) which allows to model using spot and futures prices (this is a particularity of energy markets to avoid having the actual goods delivered to you)
The thing here is that one would have to define usage. I can for example quote what someone from one of those banks told me: “we use backtrader to quickly prototype our ideas and backtest them. If they prove to be what we expected and after further refinement, they will be rewritten in Java and put into our production system”
This is actually the same scheme which one of the Quant firms (which I personally visited) used: prototyping in backtrader and production in Java.
As you may imagine I don’t track the lifes of those using backtrader, so it may also be that some of the banks and companies decided to no longer use backtrader.
I do also guess that some banks and quant firms use zipline following the same scheme.
Backtrader 螺纹钢回测
数据准备:这里用本地csv数据,数据频率是15分钟,全部历史(2009-03-27到2020-06-18)。
跑的是sar策略,下面贴上部分代码
# -*- coding:utf-8 -*-
# 引入python3.X的一些特性
from __future__ import (absolute_import, division, print_function,
unicode_literals)
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):
self.dataclose = self.datas[0].close
self.pa_sar = bt.indicators.PSAR()
'''
隐藏了策略逻辑部分,有需要的朋友私信我
'''
def stop(self):
print('period: %s, af: %s, afmax: %s, final_value: %.2f' %
(self.p.period, self.p.af, self.p.afmax, self.broker.getvalue()))
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
)
# 2.2Add the Data Feed to Cerebro
cerebro.adddata(brf_min_bar)
#cerebro.resampledata(brf_min_bar, timeframe=bt.TimeFrame.Days)
# 3 Add strategy
cerebro.addstrategy(Par_SAR)
# 设置初始资本为100,000
cerebro.broker.setcash(1000000.0)
#每次固定交易数量
cerebro.addsizer(bt.sizers.FixedSize, stake=5)
# 3 set futures commission
cerebro.broker.setcommission(commission=0.0001, margin=2800.0, mult=10.0)
print('初始资金: %.2f' % cerebro.broker.getvalue())
cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name = 'SharpeRatio')
cerebro.addanalyzer(bt.analyzers.DrawDown, _name='DW')
results = cerebro.run()
strat = results[0]
print('最终资金: %.2f' % cerebro.broker.getvalue())
print('夏普比率:', strat.analyzers.SharpeRatio.get_analysis())
print('回撤指标:', strat.analyzers.DW.get_analysis())
# 4. Run
cerebro.run()
cerebro.plot(iplot=False,style='candlestick',numfigs=9)
Backtrader 策略绩效报告
图片太大我就不一一展示出来
看下策略近期表现,也就是第八幅图: