简体   繁体   中英

Converting a GenericCSVData object to a backtrader datafeed

How can I convert a backtrader csv reader to a backtrader datafeed ? I tried:

Attempt 1: (replace datafeed with GenericCSV)

all_data=bt.feeds.GenericCSVData(
  #my csv params here
)

for s, df in all_data.items(): #THIS LINE READS IN CSV AND ERRORS
    #do stuff

'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'items'

Attempt 2: (convert GenericCSV to Datafeed)

all_data=bt.feeds.GenericCSVData(
  #my csv params here
)
all_datafeed = bt.feeds.PandasData(dataname=all_data) 

ERROR: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'columns'

Attempt 3: (Read in csv and convert to datafeed)

df=pd.read_csv('/home/abc/EUR_USD.csv',header=0,parse_dates=True)
all_datafeed = bt.feeds.PandasData(dataname=df)
for df in all_datafeed.items():
    print(df)

'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'items'

Excerpt from csv:

time,oask,hask,lask,cask,obid,hbid,lbid,cbid,volume
2002-05-06 20:00:00 UTC,0.9184,0.9184,0.9181,0.9184,0.9181,0.9181,0.9181,0.9181,1
2002-05-07 20:00:00 UTC,0.9155,0.9155,0.9152,0.9155,0.9152,0.9152,0.9152,0.9152,1
2002-05-08 20:00:00 UTC,0.9045,0.9045,0.9042,0.9045,0.9042,0.9042,0.9042,0.9042,1
# Create a Data Feed
data = bt.feeds.GenericCSVData(
    dataname='filepath.csv',
    fromdate=datetime.datetime(2018, 1, 1),
    todate=datetime.datetime(2018, 12, 31),
    nullvalue=0.0,
    dtformat=('%Y-%m-%d'),
    datetime=0,
    open = 1,
    high = 2,
    low = 3,
    close = 4,
    volume =5, 
    openinterest=-1,
    reverse=False)

# Add the Data Feed to Cerebro
cerebro.adddata(data)

If this is what you are trying to accomplish, although i am not sure.

https://www.backtrader.com/docu/datafeed.html

All you need to do is to create a data feed and pass it to the cerebro .

比特币数据集

I have the same problem and it was solved by paying attention to the following:

  1. datetime format is as %Y.%m.%d hh:mm:ss .

So you should replace - with . in your example, using the following and then save the dataframe:

import pandas as pd
df = pd.read_csv("your_csv_file.csv")
df.index = df.index.map(lambda datetime: datetime.replace("-", ".")
df.to_csv("your_csv_file.csv")

  1. The numbers we pass to GenericCSVData are the index of columns (start from 0 from left to right)
| col_0 | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 | col_7 | 
# in my example:
| datetime | symbol | open | high | low | close | volume btc | volume usd |

So it should be added like the following:

#...
data = bt.feeds.GenericCSVData(
    dataname="./dataset/btc.csv",
    dtformat=('%Y.%m.%d %H:%M:%S'),
    datetime=0,
    open=2,
    high=3,
    low=4,
    close=5,
)

cerebro.adddata(data)
#...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM