For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

AttributeError: module 'backtrader' has no attribute 'DataResampler'



  • Hey everyone!

    running into an issue here attempting to load backtrader on Python 3.8.2:

    AttributeError: module 'backtrader' has no attribute 'DataResampler'

    My file has a unique name that wouldn't collide with other names, any idea what is going on here?

    Thanks!

    import os, sys, argparse
    import pandas as pd
    import backtrader as bt
    import backtrader.feeds as btfeeds
    import backtrader.indicators as btindicators
    import datetime
    import matplotlib as plt
    import math
    
    %matplotlib inline
    
    cerebro = bt.Cerebro()
    cerebro.broker.setcash(1000)
    
         
    class GoldenCross(bt.Strategy):
        params = (('fast', 50), ('slow', 200), ('order_percentage', 0.95), ('ticker', 'ETHUSD'))
        
        def __init__(self):
            self.fast_moving_average = bt.indicators.SMA(self.data.close, period=self.params.fast, plotname='50 dma')
    
            self.slow_moving_average = bt.indicators.SMA(self.data.close, period=self.params.slow, plotname='200 dma')
            
            self.crossover = bt.indicators.CrossOver(self.fast_moving_average, self.slow_moving_average)
            
            def next(self):
                if self.position.size == 0:
                    if self.crossover > 0:
                        self.buy(size=1000)
                        
                elif self.position.size > 0:
                    if self.crossover < 0:
                        print('SELL {} shares of {} at {}'.format(self.size, self.params.ticker, self.data.close[0]))
                        self.close()
                            
    data = btfeeds.GenericCSVData(
        dataname=r'C:\Users\steve\Desktop\ethusd.csv',
    
        fromdate=datetime.datetime(2020, 1, 1),
        todate=datetime.datetime(2020, 5, 17),
    
        nullvalue=0.0,
        dtformat=('%Y-%m-%d %H:%M:%S'),
        datetime=0,
        high=3,
        low=4,
        open=2,
        close=5,
        volume=7,
        openinterest=-1
    )
    
    # Handy dictionary for the argument timeframe conversion
    tframes = dict(
        daily=bt.TimeFrame.Days,
        weekly=bt.TimeFrame.Weeks,
        monthly=bt.TimeFrame.Months)
    
    # Resample the data
    data_resampled = bt.DataResampler(
        dataname=data,
        timeframe=tframes[args.timeframe],
        compression=args.compression)
        
        
    cerebro.adddata(data_resampled)
    cerebro.addstrategy(GoldenCross)
    cerebro.run()
    
    cerebro.plot()
    


  • I figured it out, it looks like "DataResampler" was changed to "resampledata"

    read more here: https://www.backtrader.com/blog/posts/2016-03-07-release-1.2.1.88/release-1.2.1.88/

    The documentation here is possibly out of data -->https://www.backtrader.com/docu/data-resampling/data-resampling/

    The code I used for resampling 1-minute data into 6 hour data (6 hours = 360 mins):

    cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=360) # to weeks


Log in to reply
 

});