Search code examples
pythonbinancebacktrader

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


I'm trying to make a trading bot and am using backtrader for the same. I have been trying to debug this issue but couldn't find a solution yet. The code is as shown below

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

The error message is as follows:

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'

Thanks in advance!


Solution

  • You do not have a return in GetHistoricalData so it is sending None to adddata(). Maybe you need to return the dataframe? if not specify your intent.

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