属性错误:'NoneType'对象没有属性'_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,因此它正在向adddata()发送None。也许你需要返回数据帧?如果没有说明你的意图。

# 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())

相关内容

  • 没有找到相关文章

最新更新