AttributeError:'int'对象没有属性'to_pydatetime'

2024-05-19 01:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法理解错误-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()))

数据和堆栈跟踪:

data feed image

error image


Tags: thetonameinimportselfdatadatetime

热门问题