How to get backtrader to recognize current position in next() ?
Jeffrey C F Wong last edited by
Hi everyone, I have 2 closing conditions but it doens't seem to recognize any of the conditions. At the same time, there is no coding error but no trades. Anyone know why this is happening?
if not self.position: if self.crossover: self.buy() if self.crossunder: self.sell() if self.position.size!=0: if self.position is self.buy and self.crossover_exit: self.close() if self.position is self.sell and self.crossunder_exit: self.close()
i've never seen this before and this may be the reason
if self.position is self.buy
in your case i would simplify the script like this (not tested)
if not self.position: if self.crossover: self.buy() if self.crossunder: self.sell() if self.position.size != 0: if self.crossunder_exit or self.crossover_exit: self.close()
if you want to obtain position direction, than for long
self.position.size > 0and for short
self.position.size < 0.