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

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(
        def next(self):
            print("{} C: {} PP: {}".format(,[0], self.pivot.lines.p[0]))
    # Variable for our starting cash
    startcash = 10000000000
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
    # Add our strategy
    data = store.getdata(dataname='AAPL', historical=True,
                         timeframe=bt.TimeFrame.Minutes, compression=5,, 5, 26),
               , 8, 30))
    # Add the data to Cerebro
    # cerebro.resampledata(data, name='data2', timeframe=bt.TimeFrame.Minutes, compression=5)
    # Set our desired cash start
    # Run over everything
    strategies =
    firstStrat = strategies[0]
    # Get final portfolio Value
    portvalue =
    # Print out the final result
    print('Final Portfolio Value: ${}'.format(portvalue))
    # Finally plot the end results

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

Log in to reply