我无法理解错误-- AttributeError:'int‘对象没有属性' 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()))数据和堆栈跟踪:


发布于 2021-12-24 02:56:19
我也犯了同样的错误,接下来的步骤也对我起了作用。

我认为在读取csv文件时,应该将列"date“和"time".
pathfile = ".../2021-10-11--2021-10-15.csv"
df_data = pd.read_csv(pathfile, delimiter=",", index_col="Datetime", parse_dates= True)发布于 2022-01-25 19:59:40
这对我起了作用,为我的自定义数据集提供了信息。
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)https://stackoverflow.com/questions/68028434
复制相似问题