I was doing the following: def notify_trade(self, trade): if trade.justopened: self.trade_length = 0 if trade.isclosed: self.trade_length = None def next(self): pos = self.getposition() if pos: self.trade_length += 1