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

resampling - first resampled bar is sampled incorrectly



  • I'm trying to resample from 1 minute to 2, 5 and 10 minutes. The only problem I'm having is when resampling, the first OHLC data I'm logging of the 2,5 and 10 minute is exactly the same as the first 1 minute OHLC data. after that it resamples properly, however it then is out of sync.

    an example will make it more clear when sampling 1 minute to 2 minute data:

    Output in the log:

    Starting Portfolio Value: 1000000.00
    2020-08-12 00:00, O: 1.17383, H: 1.17391, L: 1.17372, C: 1.17391
    2020-08-12 00:02, O: 1.17391, H: 1.17408, L: 1.17384, C: 1.17384
    2020-08-12 00:04, O: 1.17384, H: 1.17384, L: 1.17377, C: 1.17377
    2020-08-12 00:06, O: 1.17377, H: 1.17389, L: 1.17376, C: 1.17386
    

    dataframe info:

                            Open    High    Low    Close   Volume
    Datetime					
    2020-08-12 00:00:00	1.17383	1.17391	1.17372	1.17391	23
    2020-08-12 00:01:00	1.17391	1.17408	1.17390	1.17390	18
    2020-08-12 00:02:00	1.17390	1.17390	1.17384	1.17384	33
    2020-08-12 00:03:00	1.17384	1.17384	1.17378	1.17378	22
    2020-08-12 00:04:00	1.17378	1.17378	1.17377	1.17377	25
    2020-08-12 00:05:00	1.17377	1.17382	1.17376	1.17382	39
    2020-08-12 00:06:00	1.17382	1.17389	1.17382	1.17386	36
    
    

    The first candle should be O:1.7383 H:1.17408 L:1.17372 C: 1.17390 but for some reason it always takes over the very first minute candle which causes all the rest of the candles to be out of sync.

    my code below:

    class Resampling_test(bt.Strategy):
     
        def log(self, txt, dt=None):
            ''' Logging function for this strategy'''
            dt = dt or self.datas[0].datetime.datetime(0)
            print('%s, %s' % (dt.strftime("%Y-%m-%d %H:%M"), txt))
        
        def __init__(self):
            
            pass
           
        def next(self):
            
            self.log('O: %.5f, H: %.5f, L: %.5f, C: %.5f' %(self.datas[0].open[0], self.datas[0].high[0], self.datas[0].low[0], self.datas[0].close[0]))
    
    if __name__ == '__main__':
    
        cerebro = bt.Cerebro()
    
        # Add a strategy
        cerebro.addstrategy(Resampling_test)
        
        # Create a Data Feed
        data = bt.feeds.PandasData(dataname=df2020)
        two_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
        
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
        
        cerebro.run()
        
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    many thanks



  • I've been able to fix the bars by using

    boundoff=-1
    

    for the 2 minute timeframe and

    boundoff=1
    

    for the 5 and 10 minute timeframe

    That's great but now the time is messed up

    Starting Portfolio Value: 10000.00
    2020-08-12 00:01, O: 1.17383, H: 1.17408, L: 1.17372, C: 1.17390
    2020-08-12 00:03, O: 1.17390, H: 1.17390, L: 1.17378, C: 1.17378
    2020-08-12 00:05, O: 1.17378, H: 1.17382, L: 1.17376, C: 1.17382
    2020-08-12 00:07, O: 1.17382, H: 1.17389, L: 1.17382, C: 1.17386
    

    it should be even starting from 00:00

    I've read the following about boundoff from the documentation Here:

    If for example the resampling is from 1 minute to 15 minutes, the default behavior is to take the 1-minute bars from 00:01:00 until 00:15:00 to produce a 15-minutes replayed/resampled bar.

    I'm not understanding this very well as in my mind it makes sense to take 1 minute bars from 0:00 to 14:00. so not sure why the default behavior is like this. regardless, it can be fixed using boundoff but then it messes up the datetime.



  • you could also play with rightedge=True and boundoff=x

    see this thread: https://community.backtrader.com/topic/970/backtest-vs-live-bars-off-by-1-discrepancy



  • @dasch thanks for your response. yes as described in my previous post, I have used boundoff which fixes the bar issue. when I use rightedge=True or any of the other parameters. it doesn't fix my timebar issue however



  • @Jens-Halsberghe yes, i saw your solution. That’s when I remembered the other thread.



  • @dasch I just read the full thread. this seems to be an outstanding issue for a while. I hope it'll get fixed at some time. I really like the library and I want to use it for livetrading but I hope this problem doesn't jeopardize it. what do you now use for livetrading?



  • I am still using backtrader. For simple stuff I write stuff directly using the oanda api.


Log in to reply
 

});