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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM