首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError: int对象没有属性'to_pydatetime‘

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

Stack Overflow用户
提问于 2021-06-18 02:01:20
回答 2查看 1.4K关注 0票数 1

我无法理解错误-- AttributeError:'int‘对象没有属性' to _pydatetime',如果有人能在这方面帮助我,我会非常感激的。

代码语言:javascript
复制
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()))

数据和堆栈跟踪:

EN

回答 2

Stack Overflow用户

发布于 2021-12-24 02:56:19

我也犯了同样的错误,接下来的步骤也对我起了作用。

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

我认为在读取csv文件时,应该将列"date“和"time".

  • Finally,连接起来,指定索引并解析时间.

代码语言:javascript
复制
pathfile = ".../2021-10-11--2021-10-15.csv"
df_data = pd.read_csv(pathfile, delimiter=",", index_col="Datetime", parse_dates= True)
票数 0
EN

Stack Overflow用户

发布于 2022-01-25 19:59:40

这对我起了作用,为我的自定义数据集提供了信息。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68028434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档