Backtrader 量化平台研究

Backtrader 中国期货回测

Backtrader 简介

先说几句题外话,Backtrader 到底谁在用?一下内容转自backtrader官方网站中References部分:https://www.backtrader.com/home/references/who-is-using-it/
Who is using it
我简单说几点:

  1. 2家 Eurostoxx50 银行
  2. 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 策略绩效报告

在这里插入图片描述
图片太大我就不一一展示出来
在这里插入图片描述
看下策略近期表现,也就是第八幅图:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值