Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Pandas DataFeed Example

Note

pandas and its dependencies have to be installed

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

The important declarations for the Datafeed.

Note

These areONLY declarations. Don't copy this code blindly. See theactual usage in the example below

classPandasData(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 thePandasData class shows the keys:

  • Thedataname parameter to the class during instantiation holds the Pandas Dataframe

    This parameter is inherited from the base classfeed.DataBase

  • The new parameters have the names of the regular fields in theDataSeries and follow these conventions

    • datetime (default: None)

    • 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

    • open,high,low,high,close,volume,openinterest (default: -1 for all of them)

    • 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

A small sample should be able to load the standar 2006 sample, having beenparsed byPandas, rather than directly bybacktrader

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

$ ./panda-test.py--------------------------------------------------               Open     High      Low    Close  Volume  OpenInterestDate2006-01-02  3578.73  3605.95  3578.73  3604.33       0             02006-01-03  3604.08  3638.42  3601.84  3614.34       0             02006-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  602006-01-02  3578.73  3605.95  3578.73  3604.33  0  02006-01-03  3604.08  3638.42  3601.84  3614.34  0  02006-01-04  3615.23  3652.46  3615.23  3652.46  0  0

The 2nd run is using tellspandas.read_csv:

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

  • Not to look for a headers row (header keyword argument set to None)

Thebacktrader support for Pandas tries to automatically detect if columnnames have been used or else numeric indices and acts accordingly, trying tooffer a best match.

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

image

The sample code for the test.

from__future__import(absolute_import,division,print_function,unicode_literals)importargparseimportbacktraderasbtimportbacktrader.feedsasbtfeedsimportpandasdefrunstrat():args=parse_args()# Create a cerebro entitycerebro=bt.Cerebro(stdstats=False)# Add a strategycerebro.addstrategy(bt.Strategy)# Get a pandas dataframedatapath=('../../datas/2006-day-001.txt')# Simulate the header row isn't there if noheaders requestedskiprows=1ifargs.noheaderselse0header=Noneifargs.noheaderselse0dataframe=pandas.read_csv(datapath,skiprows=skiprows,header=header,parse_dates=True,index_col=0)ifnotargs.noprint:print('--------------------------------------------------')print(dataframe)print('--------------------------------------------------')# Pass it to the backtrader datafeed and add it to the cerebrodata=bt.feeds.PandasData(dataname=dataframe)cerebro.adddata(data)# Run over everythingcerebro.run()# Plot the resultcerebro.plot(style='bar')defparse_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')returnparser.parse_args()if__name__=='__main__':runstrat()

[8]ページ先頭

©2009-2025 Movatter.jp