简体   繁体   中英

'numpy.int64' object has no attribute 'to_pydatetime' in backtrader feed

I am unable to fix the error - 'numpy.int64' object has no attribute 'to_pydatetime', I will be really grateful, if anyone could please help me out in this? I have already tried uninstalling pyfolio and itstalling it from git. Please see the complete code below

import os
import glob
import requests
import pandas as pd
from nsepy import *
from datetime import datetime
import backtrader as bt
import backtrader.feeds as btfeeds
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

class TestStrategy(bt.Strategy):

    def log(self, txt, dt=None):
        ''' Logging function for this strategy'''
        dt = dt or self.datas[0].datetime.date(0)
        print('%s, %s' % (dt.isoformat(), txt))

    def __init__(self):
        # Keep a reference to the "close" line in the data[0] dataseries
        self.dataclose = self.datas[0].close

    def next(self):
        # Simply log the closing price of the series from the reference
        self.log('Close, %.2f' % self.dataclose[0])

if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.addstrategy(TestStrategy)
    
    #Data feed block
    data_path = "/Users/kumarun/Documents/data/files"
    joined_files = os.path.join(data_path, "Oct-MONTHLY-Expirydata_2020.csv")
    joined_list = glob.glob(joined_files)
    df = pd.concat(map(pd.read_csv, joined_list), ignore_index=True)
    df.columns=['Ticker','date', 'open', 'high', 'low', 'close', 'volume','Open Interest']
    filtered = df[(df['Ticker'] == 'BANKNIFTY')]
#Cerebro block
    filtered.date = pd.to_datetime(filtered.date, format='%d-%m-%Y %H:%M:%S')
    feed = bt.feeds.PandasData(dataname=filtered)
    cerebro.adddata(feed)
    cerebro.broker.setcash(100000.0)

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

    cerebro.run()

    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

数据输入格式

I feel like you might be running into issues with the column names. Try ascribing your column names using:

 data = bt.feeds.PandasData(
        dataname=filtered,
        datetime="date",
        open='open',
        high='high',
        low='low',
        close='close',
        volume='volume',
        openinterest="Open Interest"
    )

Also, could drop the Ticker name column, and add the name using:

ticker = 'BANKNIFTY'
cerebro.adddata(feed, name=ticker)

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