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/

    How do you buy and sell the roll with RollOver Feed

    General Code/Help
    2
    4
    143
    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
      adamb032 last edited by

      hey - ive got the basic RollOver feed example (https://www.backtrader.com/docu/data-rollover/rolling-futures-over/) working as expected and I can verify the rolls are occurring when i want.

      however I'd like to simulate actually trading the roll, assuming its a long only model, so in my next() function

      def next(self):
              txt = list()
              txt.append('{}'.format(self.data0._name))
              # Internal knowledge ... current expiration in use is in _d
              txt.append('{}'.format(self.data0._d._name))
              txt.append('{}'.format(self.data.datetime.date()))
              txt.append('{}'.format(self.data.datetime.date().strftime('%a')))
              txt.append('{}'.format(self.data.open[0]))
              txt.append('{}'.format(self.data.high[0]))
              txt.append('{}'.format(self.data.low[0]))
              txt.append('{}'.format(self.data.close[0]))
              self.logger.info(', '.join(txt))
      
              if not self.position:
                  self.logger.info(f"{self.curr_future} :: [{self.data.datetime.date(0)}] SUBMITTED Buy MOC T+1 ref_px:{self.data.close[0]:.2f}")
                  self.buy()
              else:
                  '''we will need to roll the position so we check if tomorrow is roll date'''
                  if self._check_is_roll_date(self.data.datetime.date(), 
                                               self.curr_future):
              
                      self.logger.info(f"[{self.data.datetime.date(0)}] I SHOULD ROLL HERE!")
                      self.sell()
                      self.buy()
      

      so initially i buy on next bar data.close[0] to establish the initial long position

      the hiccup i get is on the roll (selling near, buying far). when i issue the self.sell() order its going to look at data.close[0]. however because of the way RollOver works, this will be the close of the next maturity in sequence. thats fine for the buy order, however the sell i need to sell from the previous maturity, but I don't see anywhere this is referenced in the RollOver class.

      Since RollOver is not backadjusting the prices but just mechanically creating a dataframe of on a given date, what is my active maturity close, it makes simulating the roll trade hard to understand.

      B 1 Reply Last reply Reply Quote 0
      • B
        BobDenar @adamb032 last edited by

        @adamb032 I have the same issue, here is the workaround I coded:

        if not self.position:
            ...
        else:
            if self._check_is_roll_date(self.data.datetime.date(), self._curr_future):
                self.sell(data=self.data._rolls[0])
                self.buy(data=self.data._rolls[1])
        

        The issue here is to get the right contract by its _rolls index. A solution would be to name each datafeed composing the RollOver, then find the wanted contract by its name.

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

          @bobdenar but in your solution, now i believe you are lagging a day inbetween the roll no? (closing one leg, then opening the next on the following day?)

          B 1 Reply Last reply Reply Quote 0
          • B
            BobDenar @adamb032 last edited by

            @adamb032 self.data._rolls[0] refers to the first contract from the RollOver feed, and self.data._rolls[1] to the second.
            Therefore it is closing position on the first contract and opening on the second at the same bar.

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