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

SessionFiller and Resampler together



  • Hi,

    I am trying to combine SessionFiller and Resampler but it does not seem to work no matter which order these are added. Independently both work correctly.

    The data set (just showing close) is

    ,close
    2018-08-08 09:01:01,4
    2018-08-08 09:02:01,5
    2018-08-08 09:03:01,6
    2018-08-08 09:06:01,7
    2018-08-08 09:07:01,8
    2018-08-08 09:08:01,9
    2018-08-08 09:09:01,10
    2018-08-08 09:10:01,11
    2018-08-08 09:11:01,12
    2018-08-08 09:12:01,13
    

    and I would expect to get something like

    2018-08-08 09:02:00, 4.0
    2018-08-08 09:04:00, 6.0
    2018-08-08 09:06:00, 6.0
    2018-08-08 09:08:00, 8.0
    2018-08-08 09:10:00, 10.0
    2018-08-08 09:12:00, 12.0
    2018-08-08 09:14:00, 13.0
    

    Thanks

    import backtrader as bt
    import sys
    import os
    import datetime
    
    class StrategyBidAsk(bt.Strategy):
    
        def start(self):
            self.counter = 0
    
        def next(self):
            self.counter += 1
            output_str = ""
            for i in range(len(self.datas)):
                output_str += "{}: {}, {}|".format(self.datas[i]._name, self.datas[i].datetime.datetime(0), self.datas[i].lines.close[0])
            output_str = output_str[:-1]
            print(output_str)
    
    def main(argv):
    
        folder = "data"
        file = 'Toy_Ask_new.csv'
        cash = 100000.0
    
        cerebro = bt.Cerebro()
        cerebro.broker.setcash(cash)
        cerebro.broker.set_shortcash(False)
        cerebro.broker.setcommission(commission=0)
        cerebro.broker.set_coc(False)
    
        data = bt.feeds.GenericCSVData(
                dataname=os.path.join(folder, file),
                dtformat='%Y-%m-%d %H:%M:%S', # default
                datetime=0,         # default
                time=-1,            # default
                close=1,
                high=2,
                low=3,
                open=4,
                volume=5,           # default
                openinterest=-1,    # default for not present
                sessionstart=datetime.time(9, 0),
                sessionend=datetime.time(16, 0),
                timeframe=bt.TimeFrame.Minutes,
                compression=1)      # default
    
        data.addfilter(bt.filters.SessionFilter)
        data.addfilter(bt.filters.SessionFiller)
        data.addfilter(bt.ResamplerMinutes, compression=2)
        cerebro.adddata(data, name=os.path.splitext(file)[0])
    
        idx = cerebro.addstrategy(StrategyBidAsk)
        cerebro.addsizer_byidx(idx, bt.sizers.SizerFix, stake=100)
        cerebro.run(runonce=False, preload=True)
    
    
    if __name__ == "__main__":
        main(sys.argv)
    

  • administrators

    You have to fill the original data feed.