量化交易研究

量化交易回测框架Backtrader初识

2020-04-11  本文已影响0人  一块自由的砖

Backtrader知识储备

框架的概念还挺多:Cerebro,Data Feeds,Strategy,Indicators,Orders,Broker,Analyzers,Observers,Sizers。逐个来看看大概的功能和角色。对学习框架好处多多。

看了下官方给出的:Quickstart Guide。由于官网举得例子都是国外的数据。要是想掌握这个必须把源头先搞定,就是提供交易数据(Data Feeds ),还好官方支持按照规范自己实现自己的数据。所以还是从这里开始研究。

第一弹:Data Feeds(交易数据)

背景

因为数据的获取,我使用的是tushare。tushare返回的是pandas的dataframe格式。所以我就主攻pandas-datafeed这块。

原理

参考官方文档pandas-datafeed

class PandasData(feed.DataBase):
    '''
    The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas
    DataFrame
    '''

    params = (
        # Possible values for datetime (must always be present)
        #  None : datetime is the "index" in the Pandas Dataframe
        #  -1 : autodetect position or case-wise equal name
        #  >= 0 : numeric index to the colum in the pandas dataframe
        #  string : column name (as index) in the pandas dataframe
        ('datetime', None),

        # Possible values below:
        #  None : column not present
        #  -1 : autodetect position or case-wise equal name
        #  >= 0 : numeric index to the colum in the pandas dataframe
        #  string : column name (as index) in the pandas dataframe
        ('open', -1),
        ('high', -1),
        ('low', -1),
        ('close', -1),
        ('volume', -1),
        ('openinterest', -1),
    )

PandasData类继承feed.DataBase类,初始化数据需要7列数据 分别为:datetime、open、high、low、close、volume、openinterest,其中datetime是索引列。经过实践送过去的数据按照这个顺序排列,不包含header部分即可。

实践(Data Feeds 使用pandas数据)

使用的是601668 中国建筑的股票数据,csv文件我放到github上了。可以自行去下载,按照官方文档的源码改了下数据元,正常执行。

# -*- coding: utf-8 -*-
"""
搞定自定义的数据送给backtrader
"""
#############################################################
#import
#############################################################
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import os,sys
import pandas as pd
import backtrader as bt
#############################################################
#global const values
#############################################################
#############################################################
#static function
#############################################################
#############################################################
#class
#############################################################
#############################################################
#global values
#############################################################
#############################################################
#global function
#############################################################
def G_get_dataframe():
     # Get a pandas dataframe
    datapath = './data/stockinfo.csv'
    tmpdatapath = './data/stockinfo_tmp.csv'
    print('-----------------------read csv---------------------------')
    dataframe = pd.read_csv(datapath,
                                skiprows=0,
                                header=0,
                                parse_dates=True,
                                index_col=0)
    print(dataframe)
    print('--------------------------------------------------')
    print('-----------------------change time------------------------')
    dataframe.trade_date =  pd.to_datetime(dataframe.trade_date, format="%Y%m%d")
    print(dataframe)
    print('--------------------------------------------------')
    print('-----------------------add openinterest-------------------')
    dataframe['openinterest'] = '0'
    print(dataframe)
    print('--------------------------------------------------')
    print('-----------------------make feedsdf-----------------------')
    feedsdf = dataframe[['trade_date', 'open', 'high', 'low', 'close', 'vol', 'openinterest']]
    print(feedsdf)
    print('--------------------------------------------------')
    print('-----------------------change columns---------------------')
    feedsdf.columns =['datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest']
    print(feedsdf)
    print('--------------------------------------------------')
    print('-----------------------change index-----------------------')
    feedsdf.set_index(keys='datetime', inplace =True)
    print(feedsdf)
    print('--------------------------------------------------')
    feedsdf.iloc[::-1].to_csv(tmpdatapath)
    feedsdf = pd.read_csv(tmpdatapath, skiprows=0, header=0, parse_dates=True, index_col=0)
    if os.path.isfile(tmpdatapath):
        os.remove(tmpdatapath)
        print(tmpdatapath+" removed!")
    return feedsdf
########################################################################
#main
########################################################################
if __name__ == '__main__':
    # Create a cerebro entity
    cerebro = bt.Cerebro(stdstats=False)

    # Add a strategy
    cerebro.addstrategy(bt.Strategy)

    # Get a pandas dataframe
    feedsdf = G_get_dataframe()
    
    # Pass it to the backtrader datafeed and add it to the cerebro
    data = bt.feeds.PandasData(dataname=feedsdf)
    print(data)

    cerebro.adddata(data)

    # Run over everything
    cerebro.run()

    # Plot the result
    cerebro.plot(style='bar')

输出图形

Figure_0.png

源码:

qtbt

上一篇下一篇

猜你喜欢

热点阅读