How do you buy and sell the roll with RollOver Feed
-
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.
-
@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 theRollOver
, then find the wanted contract by its name. -
@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?)
-
@adamb032
self.data._rolls[0]
refers to the first contract from theRollOver
feed, andself.data._rolls[1]
to the second.
Therefore it is closing position on the first contract and opening on the second at the same bar.