zoukankan      html  css  js  c++  java
  • Backtrader中文笔记之Pandas DataFeed Example

    Pandas DataFeed Example

    Note

    pandas and its dependencies have to be installed

    pandas与它的依赖需要被安装

    Supporting Pandas Dataframes seems to be of concern to lots of people, who rely on the already available parsing code for different data sources (including CSV) and other functionalities offered by Pandas.

    支持Pandas数据模型似乎是很多人关心的问题,他们依赖于已经可用的针对不同数据源(包括CSV)的解析代码和Pandas提供的其他功能。

    The important declarations for the Datafeed.

    Datafeed的重要声明。

    Note

    These are ONLY declarations. Don't copy this code blindly. See the actual usage in the example below

    这些只是声明。不要盲目地复制这个代码。请参见下面的示例中的实际用法

    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),
        )
    

    The above excerpt from the PandasData class shows the keys:

    上面的摘自PandasData类的代码显示了键:

    • The dataname parameter to the class during instantiation holds the Pandas Dataframe

    • 在实例化期间,类的dataname参数保存了panda Dataframe

      This parameter is inherited from the base class feed.DataBase

    • 这个参数是从基类feed.DataBase继承的
    • The new parameters have the names of the regular fields in the DataSeries and follow these conventions

    • 新参数具有数据集中的常规字段的名称,并遵循这些约定

      • datetime (default: None)

      • None : datetime is the “index” in the Pandas Dataframe

      • None : datetime is the “index” 在Pandas Dataframe里面
      • -1 : autodetect position or case-wise equal name

      • -1:自动检测位置或大小写相等的名称
      • = 0 : numeric index to the colum in the pandas dataframe

      • =0:数字索引来至pandas dataframe的列
      • string : column name (as index) in the pandas dataframe

      • string :pandas数据帧中的列名(作为索引)
      • open, high, low, high, close, volume, openinterest (default: -1 for all of them)

      • None : column not present

      • None :列不存在
      • -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

    A small sample should be able to load the standar 2006 sample, having been parsed by Pandas, rather than directly by backtrader

    一个小样本应该能够加载standar2006样本,已经由Pandas解析,而不是直接由backtrader进行分析

    Running the sample to use the exiting “headers” in the CSV data:

    运行示例以使用CSV数据中现有的“标题”:

    $ ./panda-test.py
    --------------------------------------------------
                   Open     High      Low    Close  Volume  OpenInterest
    Date
    2006-01-02  3578.73  3605.95  3578.73  3604.33       0             0
    2006-01-03  3604.08  3638.42  3601.84  3614.34       0             0
    2006-01-04  3615.23  3652.46  3615.23  3652.46       0             0
    

     The same but telling the script to skip the headers:

    相同,但告诉脚本跳过标题:

    $ ./panda-test.py --noheaders
    --------------------------------------------------
                      1        2        3        4  5  6
    0
    2006-01-02  3578.73  3605.95  3578.73  3604.33  0  0
    2006-01-03  3604.08  3638.42  3601.84  3614.34  0  0
    2006-01-04  3615.23  3652.46  3615.23  3652.46  0  0
    

    The 2nd run is using tells pandas.read_csv:

    第二次运行告诉pandas.read_csv

    • To skip the first input row (skiprows keyword argument set to 1)

    • 要跳过第一个输入行(skiprows关键字参数设置为1)
    • Not to look for a headers row (header keyword argument set to None)

    • 不查找标题行(标题关键字参数设置为None)

    The backtrader support for Pandas tries to automatically detect if column names have been used or else numeric indices and acts accordingly, trying to offer a best match.

     Pandas的backtrader支持尝试自动检测列名是否被使用,或者是否使用了数字索引,并相应地采取行动,试图提供最佳匹配。

    The following chart is the tribute to success. The Pandas Dataframe has been correctly loaded (in both cases)

    下表是对成功的案例。Pandas数据框架已正确加载(在两种情况下)

    The sample code for the test.

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import argparse
    
    import backtrader as bt
    import backtrader.feeds as btfeeds
    
    import pandas
    
    
    def runstrat():
        args = parse_args()
    
        # Create a cerebro entity
        cerebro = bt.Cerebro(stdstats=False)
    
        # Add a strategy
        cerebro.addstrategy(bt.Strategy)
    
        # Get a pandas dataframe
        datapath = ('../../datas/2006-day-001.txt')
    
        # Simulate the header row isn't there if noheaders requested
        skiprows = 1 if args.noheaders else 0
        header = None if args.noheaders else 0
    
        dataframe = pandas.read_csv(datapath,
                                    skiprows=skiprows,
                                    header=header,
                                    parse_dates=True,
                                    index_col=0)
    
        if not args.noprint:
            print('--------------------------------------------------')
            print(dataframe)
            print('--------------------------------------------------')
    
        # Pass it to the backtrader datafeed and add it to the cerebro
        data = bt.feeds.PandasData(dataname=dataframe)
    
        cerebro.adddata(data)
    
        # Run over everything
        cerebro.run()
    
        # Plot the result
        cerebro.plot(style='bar')
    
    
    def parse_args():
        parser = argparse.ArgumentParser(
            description='Pandas test script')
    
        parser.add_argument('--noheaders', action='store_true', default=False,
                            required=False,
                            help='Do not use header rows')
    
        parser.add_argument('--noprint', action='store_true', default=False,
                            help='Print the dataframe')
    
        return parser.parse_args()
    
    
    if __name__ == '__main__':
        runstrat()
    
  • 相关阅读:
    MFC添加右键菜单
    人生导师——如何学习C++的Windows方向
    删除CListCtrl中具有某一相同数据的所有行
    向某地址写入值并执行
    问题解决——使用CriticalSection后 0xXXXXXXXX处最可能的异常: 0xC0000005: 写入位置 0x00000014 时发生访问冲突
    问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
    问题解决——在结构体中使用set保存结构体数据
    问题解决——基于MSCOMM32.OCX控件的类在客户机不能创建控件
    问题解决——ShowWindow不显示窗口
    问题解决——cout 输出 CString
  • 原文地址:https://www.cnblogs.com/sidianok/p/13558476.html
Copyright © 2011-2022 走看看