AttributeError: 'NoneType' object has no attribute '_id'

AttributeError: 'NoneType' object has no attribute '_id'

我正在尝试制作一个交易机器人,并且正在使用 backtrader 来做同样的事情。我一直在尝试调试这个问题,但还没有找到解决方案。代码如下图

# from ast import Constant
from operator import imod
import os, sys
import config
from binance.client import Client
import backtrader
import pandas as pd
import datetime, time


client = Client(config.Binanceapikey, config.BinancesecretKey)


def GetHistoricalData(howLong):
    # Calculate the timestamps for the binance api function
    untilThisDate = datetime.datetime.now()
    sinceThisDate = untilThisDate - datetime.timedelta(days = howLong)
    # Execute the query from binance - timestamps must be converted to strings !
    candle = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1MINUTE, str(sinceThisDate), str(untilThisDate))

    # Create a dataframe to label all the columns returned by binance so we work with them later.
    df = pd.DataFrame(candle, columns=['dateTime', 'open', 'high', 'low', 'close', 'volume', 'closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol', 'takerBuyQuoteVol', 'ignore'])
    # as timestamp is returned in ms, let us convert this back to proper timestamps.
    df.dateTime = pd.to_datetime(df.dateTime, unit='ms').dt.strftime("%Y-%m-%d")
    df.set_index('dateTime', inplace=True)

    # Get rid of columns we do not need
    df = df.drop(['closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol','takerBuyQuoteVol', 'ignore'], axis=1)
    
    
cerebro = backtrader.Cerebro()

cerebro.broker.set_cash(100000)

cerebro.adddata(GetHistoricalData(1))


print('Starting porfolio value: %.2f' %cerebro.broker.getvalue())

cerebro.run()

print('Final porfolio value: %.2f' %cerebro.broker.getvalue())

报错信息如下:

File "/TradingBot/tradingBot.py", line 40, in <module>
    cerebro.adddata(GetHistoricalData(1))
  File "/usr/local/lib/python3.8/site-packages/backtrader/cerebro.py", line 757, in adddata
    data._id = next(self._dataid)
AttributeError: 'NoneType' object has no attribute '_id'

提前致谢!

您在 GetHistoricalData 中没有 return,因此它将 None 发送到 adddata()。也许您需要 return 数据框?如果没有说明您的意图。

# from ast import Constant
from operator import imod
import os, sys
import config
from binance.client import Client
import backtrader
import pandas as pd
import datetime, time


client = Client(config.Binanceapikey, config.BinancesecretKey)


def GetHistoricalData(howLong):
    # Calculate the timestamps for the binance api function
    untilThisDate = datetime.datetime.now()
    sinceThisDate = untilThisDate - datetime.timedelta(days = howLong)
    # Execute the query from binance - timestamps must be converted to strings !
    candle = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1MINUTE, str(sinceThisDate), str(untilThisDate))

    # Create a dataframe to label all the columns returned by binance so we work with them later.
    df = pd.DataFrame(candle, columns=['dateTime', 'open', 'high', 'low', 'close', 'volume', 'closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol', 'takerBuyQuoteVol', 'ignore'])
    # as timestamp is returned in ms, let us convert this back to proper timestamps.
    df.dateTime = pd.to_datetime(df.dateTime, unit='ms').dt.strftime("%Y-%m-%d")
    df.set_index('dateTime', inplace=True)

    # Get rid of columns we do not need
    df = df.drop(['closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBaseVol','takerBuyQuoteVol', 'ignore'], axis=1)
    #return the df
    return df
    
cerebro = backtrader.Cerebro()

cerebro.broker.set_cash(100000)

cerebro.adddata(GetHistoricalData(1))


print('Starting porfolio value: %.2f' %cerebro.broker.getvalue())

cerebro.run()

print('Final porfolio value: %.2f' %cerebro.broker.getvalue())