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/Replay for custom (extended) Feed

    General Discussion
    4
    6
    196
    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.
    • A
      alexgorbachev last edited by

      I extended Pandas feed to add a volume weighted average. I'm feeding 5 seconds bars and want to resample (replay, actually), 1 minute bars along with 5 seconds. However, cerebro.resampledata() only produces OHLC columns... Where do I add custom code to for an extra column/line aggregation? I saw a hint to add another filter on top of Replayer or Resampler and I could add a dummy filter just to see if it work (and it does - it's call method does execute and gets resampled 1m bar) but I can't figure out how to add another line/column to that feed.

      run-out vladisld 2 Replies Last reply Reply Quote 0
      • run-out
        run-out @alexgorbachev last edited by

        @alexgorbachev Can you share your code please?

        RunBacktest.com

        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld @alexgorbachev last edited by

          @alexgorbachev I think it was already discussed here:
          https://community.backtrader.com/topic/2812/resample-for-extra-information-in-lines

          A 1 Reply Last reply Reply Quote 2
          • A
            alexgorbachev @vladisld last edited by

            @vladisld Thanks for the reference. I may give it a try to create a custom Replayer then. Resampling workaround is easy - I can just resample myself and feed as a normal feed but I can't do that to replay incomplete bars...

            A 1 Reply Last reply Reply Quote 0
            • A
              alexgorbachev @alexgorbachev last edited by

              @vladisld: Thanks so much for your help! I spend few hours trying to find examples until you pointed me to that post.

              So here is what seems to work but I have couple questions if you could confirm... See the questions in the code comments.

              class PandasDataVW(PandasData):
                  """Pandas feed with additional columns `vw` for volume weighted average."""
              
                  lines = ('vw',)
                  params = (('vw', -1),)
              
              class _MyBar(bt.dataseries._Bar):
                  def bstart(self, maxdate=False):
                      super().bstart(maxdate)
                      self.vw = float('NaN')
                      self.vw_vol = 0
              
                  def bupdate(self, data, reopen=False):
                      return_value = super().bupdate(data, reopen)
                      self.vw_vol += data.vw[0] * data.volume[0]
                      self.vw = self.vw_vol / self.volume if self.volume else data.vw[0]
                      return return_value
              
              class MyReplayer(bt.Replayer):
              
                  def __init__(self, data):
                      super().__init__(data)
                      self.bar = _MyBar(maxdate=True)
              
              cerebro.addstrategy(TestStrat)
              data0 = PandasDataVW(dataname=df, timeframe=bt.TimeFrame.Seconds, compression=5)
              cerebro.adddata(data0, name='s5')
              # For some reason cloning doesn't work here - DataClone doesn't have my `vw` line
              # so I can recreate the feed. I assume I shouldn't add filter to the same feed directly
              # because I'm already using it as is. Do I do something wrong with cloning?
              # data1 = data0.clone()
              data1 = PandasDataVW(dataname=df, timeframe=bt.TimeFrame.Seconds, compression=5)
              data1.addfilter(MyReplayer,
                              timeframe=bt.TimeFrame.Minutes,
                              compression=1)
              cerebro.adddata(data1, name='m1')
              # `_doreply=True` I saw in cerebro.resampledata() along with cloning so I replicated it.
              # Is this required?
              cerebro._doreplay = True
              cerebro.run(preload=False)
              

              Hope this comes useful to others.

              I 1 Reply Last reply Reply Quote 1
              • I
                invoker @alexgorbachev last edited by

                @alexgorbachev at last ,it prove it is works,but there some thing to say,if your extend num>=2,
                the extend in bstart and bupdate must be define and inplement by strict sort,it seem the backtrader recoginze them by order and not name

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors