1

我正在尝试制作一个交易机器人,并正在使用 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'

提前致谢!

4

1 回答 1

3

你没有returninGetHistoricalData所以它正在发送Noneadddata()。也许您需要返回数据框?如果不指定您的意图。

# 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())
于 2022-01-24T18:51:49.223 回答