Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    2
    30
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • monil shah 0
      monil shah 0 last edited by

      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')
      
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

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

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }