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

Resample indicator in init function



  • This is an example code for my problem. Any particular way if I can resample the data in init function.

    
    class firstStrategy(bt.Strategy):
    
        def __init__(self):
    
            # TODO: Resample data to daily, weekly or monthly
            self.pivot = bt.indicators.PivotPoint(self.data)
    
    
        def next(self):
            print("{} C: {} PP: {}".format(self.data.datetime.datetime(), self.data.close[0], self.pivot.lines.p[0]))
    
    # Variable for our starting cash
    startcash = 10000000000
    
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
    
    # Add our strategy
    cerebro.addstrategy(firstStrategy)
    
    data = store.getdata(dataname='AAPL', historical=True,
                         timeframe=bt.TimeFrame.Minutes, compression=5, fromdate=datetime.date(2020, 5, 26),
                         todate=datetime.date(2020, 8, 30))
    
    # Add the data to Cerebro
    cerebro.adddata(data)
    # cerebro.resampledata(data, name='data2', timeframe=bt.TimeFrame.Minutes, compression=5)
    
    # Set our desired cash start
    cerebro.broker.setcash(startcash)
    
    
    # Run over everything
    strategies = cerebro.run()
    firstStrat = strategies[0]
    
    # Get final portfolio Value
    portvalue = cerebro.broker.getvalue()
    
    # Print out the final result
    print('Final Portfolio Value: ${}'.format(portvalue))
    
    # Finally plot the end results
    cerebro.plot(style='candlestick')
    


  • Data gets resampled when adding to cerebro, then you can access this in init or other places.


Log in to reply
 

});