1

我无法理解错误 - AttributeError: 'int' object has no attribute 'to_pydatetime',我将不胜感激,如果有人能帮我解决这个问题?

from datetime import date, datetime
import pandas as pd
import backtrader as bt

file_name = 'fromstart/2021 APR NIFTY.txt'
dataframe = pd.read_csv(file_name)
data = bt.feeds.PandasData(
    dataname = dataframe,
    fromdate = datetime(2021, 1, 1),
    todate = datetime(2021, 4, 30),
    )

class AllInOne(bt.Strategy):
    def __init__(self):
        self.dataopen = self.datas[0].open_p      # Keep a reference to the "open" line in the data[0] dataseries
        self.datahigh = self.datas[0].high_p      # Keep a reference to the "high" line in the data[0] dataseries
        self.datalow = self.datas[0].low_p        # Keep a reference to the "low" line in the data[0] dataseries
        self.dataclose = self.datas[0].close_p    # Keep a reference to the "close" line in the data[0] dataseries

    def next(self):
        pass

if __name__ == '__main__' :
    cerebro = bt.Cerebro()  # We initialize the `cerebro` backtester.
    cerebro.adddata(data) # We add the dataset in the Data cell.
    cerebro.addstrategy(AllInOne)
    print('Starting Portfolio Value: {0:8.2f}'.format(cerebro.broker.getvalue()))
    results = cerebro.run()
    print('Final Portfolio Value: {0:8.2f}'.format(cerebro.broker.getvalue()))

数据和堆栈跟踪:

数据馈送图像

错误图像

4

2 回答 2

0

我有同样的错误,接下来的步骤对我有用。

  1. 这是我正在使用的 CSV 文件数据。

来自 csv 文件的数据

  1. 我认为你应该连接列“日期”和“时间”。
  2. 最后,读取csv文件时,指定索引并解析时间。
pathfile = ".../2021-10-11--2021-10-15.csv"
df_data = pd.read_csv(pathfile, delimiter=",", index_col="Datetime", parse_dates= True)
于 2021-12-24T02:56:19.083 回答
0

这对我有用,用于输入我的自定义数据集。

datapath = '../db/stockData/daily/ONGC.csv'
dataframe = pd.read_csv(datapath,
                            parse_dates=True,
                        index_col="timestamp",
                       )

dataframe.index = pd.to_datetime(dataframe.index,format="%Y-%m-%d",utc=True)
data = bt.feeds.PandasData(dataname=dataframe)

cerebro.adddata(data)
于 2022-01-25T19:59:40.713 回答