Difference in number of trades executed and actual order issued during backtesting
-
I have a simple pairs trading code in which I buy one symbol and sell another symbol when a condition is met and vice versa when another condition is met. I haven't put any restrictions in backtrader. After analyzing the results using notify_order and notify_trade, I am getting total trades ([('total', 369), ('open', 2), ('closed', 367)]) while for sym1 trades are 241 and sym2 trades are 126. I also added a global variable in next() to record how many times self.close() is called for each symbol and it's 409 for both. Why are there discrepancies in the results?
-
@zuj It's really hard to help you when we can't see your code.
-
@run-out I can't share my whole strategy but I will show the part in which I issue trades because that's what matters for the notify_trade() to the best of my knowledge.
if self.currentPosition is None: if condition1: self.log("Long Entry") self.sell(self.data) self.buy(self.data1) self.currentPosition="long" elif condition2: self.log("Short Entry") self.buy(self.data) self.sell(self.data1) self.currentPosition="short" else: if self.currentPosition=="long" and condition3: self.log("Long Exit") self.close(self.data) self.close(self.data1) self.trade_count_sym1+=1 self.trade_count_sym2+=1 self.currentPosition=None elif self.currentPosition=="short" and condition4: self.log("Short Exit") self.close(self.data) self.close(self.data1) self.currentPosition=None self.trade_count_sym1+=1 self.trade_count_sym2+=1
trade_count_sym1 and trade_count_sym2 both turn out to be 409 while the trade count from analyzers are mentioned in the original post