self.close() backtrader : not able to close the trade as intended

185 Views Asked by At

Whenever an order gets bought or sold or closed in the "next(self)" method, the corresponding order.isbuy() or issell() methods get executed in the notify_order(self, order) method. I am OK with isbuy() and issell() methods being called whenever there is a self.buy() or self.sell() execution at "next(self)", but for self.close() I would like to identify in notify_order(self, order) that the call is for close and not for buy or sell and take appropriate actions. If someone who is well verse with backtrader can advise that would be great

def notify_order(self, order):
        if order.status in [order.Submitted, order.Accepted]:
            # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            return
        if order.isbuy():
           pass
        elif order.issell():
           pass

def next(self):
      if self.order == None:
         return
                  if pos == 0:
                    #logic 
                    self.order = self.buy()

                elif #logic:
                    self.order = self.sell()
            else:
                if pos > 0:
                    if  #logic:
                        self.order = self.close()
                else:
                    if  #logic:
                        self.order = self.close()```




I tried tweaking the logic various ways, but I couldn't achieve the end result
0

There are 0 best solutions below