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

Datafeed ReSample Query



  • Hi,

    I'm trying to resample 1 min data to hourly. The Exchange time isbetween 9:15am and 3:30pm. I want the first hourly bar from 9:15am to 10:15am, second hourly bar from 10:15am to 11:15am and so on. The last hourly bar will contain data from 3:15pm to 3:30pm. How can go about to get this implemented? Below is my code which gives hourly bar from 9:15 to 10, 10 to 11 etc and finally from 3 to 3:30.

    class TestStrategy(bt.Strategy):
    
    
        def next(self):
            self.log (self.data.close[0])
    
        def log(self, txt):
            print (self.data.datetime.datetime(0),": ", txt)
                    
    
    if __name__ == '__main__':
    
        path = r'C:\Users\Priyadarshan.sathya\Documents\tata_mod.csv'
        
        cerebro = bt.Cerebro()
        cerebro.addstrategy(TestStrategy)
        
        data = btfeeds.GenericCSVData(
            dataname=path,
            timeframe = bt.TimeFrame.Minutes,
            compression = 1,
            fromdate=datetime.datetime(2019, 7, 1),
            todate=datetime.datetime(2019, 8, 6),
            sessionstart=datetime.time(9, 15),
            sessionend=datetime.time(15, 30),
            nullvalue=0.0,
        
            dtformat=('%d-%m-%Y'),
            tmformat = ('%H:%M:%S'),
        
            datetime=1,
            high=3,
            low=4,
            open=2,
            close=5,
            volume=6,
            openinterest=-1,
            time = 7
        )
        cerebro.adddata(data)
        cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes,
                                 compression=60)
    
        cerebro.broker.setcash(100000.0)
        cerebro.broker.setcommission(commission=0.001)
    
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
        cerebro.run()
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

  • administrators

    @Priyadarshan-S said in Datafeed ReSample Query:

    I want the first hourly bar from 9:15am to 10:15am

    That's actually not possible directly. You can always resample with pandas.


Log in to reply
 

});