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

Daily Resample Data



  • Hi fellow traders,

    Can I ask whether any of you have tried resampling to daily data before?

    In the below code, it seems that the output, the daily closing price, is shown once every other days.

    If I want to use resampling to show the daily closing price, what should I do?

    import backtrader as bt
    import datetime as dt
    import pytz
    
    class Test_Strategy (bt.Strategy):
        def next (self):
            self.log (self.data.close[0])
            
        def log (self, text):
            print (self.data.datetime.datetime(0),": ", text)
            
    if __name__ == '__main__':
        ibstore = bt.stores.IBStore (host = '127.0.0.1', clientId = 100, port = 7497)
        data = ibstore.getdata (dataname = 'GBP.USD-CASH-IDEALPRO',     
                                rtbar = False,
                                timeframe = bt.TimeFrame.Minutes,
                                compression = 60,
                                historical = True,
                                fromdate = dt.datetime (2019,4,25),
                                todate = dt.datetime(2019,10,26),
                                tz=pytz.timezone('US/Eastern'),
                                )
        cerebro = bt.Cerebro()
        cerebro.resampledata (data, timeframe = bt.TimeFrame.Days, compression = 1)
        cerebro.addstrategy (Test_Strategy)
        cerebro.run()
    

    See the below screenshot for selected portion of the output based on the above code.
    3ebc8056-9751-468e-8433-ce934b5d0bfc-image.png



  • Re: Daily Resample Data

    The problem is solved after I remove timeframe and compression parameter in getdata().

    Can I ask why would that resolve the problem?

    Is the inclusion of parameters, timeframe and compression, in ibstore.getdata() redundant if I were to specify a value for rtbar?

    See below correct code.

    import backtrader as bt
    import datetime as dt
    import pytz
    
    class Test_Strategy (bt.Strategy):
        def next (self):
            self.log (self.data.close[0])
            
        def log (self, text):
            print (self.data.datetime.datetime(0),": ", text)
            
    if __name__ == '__main__':
        ibstore = bt.stores.IBStore (host = '127.0.0.1', clientId = 100, port = 7497)
        data = ibstore.getdata (dataname = 'GBP.USD-CASH-IDEALPRO',     
                                rtbar = False,
                                historical = True,
                                fromdate = dt.datetime (2019,4,25),
                                todate = dt.datetime(2019,10,26),
                                tz=pytz.timezone('US/Eastern'),
                                )
        cerebro = bt.Cerebro()
        cerebro.resampledata (data, timeframe = bt.TimeFrame.Days, compression = 1)
        cerebro.addstrategy (Test_Strategy)
        cerebro.run()

Log in to reply
 

});