Creating a Long/Short/Exit Strategy?
-
I'm relatively new to bt and have been having trouble trying to find out how to code in a long/short strategy.
My strategy in pseudo code:
if macd > signal & rsi > rsi_ma:
long
if macd < signal:
exit long
if macd < signal & rsi < rsi_ma:
short
if macd > 0:
exit shortI can't figure out how to exit a long position or short position given a indicator crossover.
I see the self. position.size > 0 but need something more like self.position = long.
Any help?
-
@roony16 said in Creating a Long/Short/Exit Strategy?:
I see the self. position.size > 0 but need something more like self.position = long.
> 0
is long (you have taken)== 0
is no position< 0
is short (you have given)
Or, what are you expecting?
Note:
Please format your code (see the top of the forum). Paying attention to detail is what will make an algotrader ... a good one. -
@backtrader Thank you - I figured the position.size was either 0 or > 0 indicating that you either had an open position or no position.
-
The existence of a position (regardless of direction) is the opposite of
pos == 0
and that's not> 0
. The opposite isnot pos
or the (theoretically) less recommendedpos != 0
-
@backtrader why is this theoretically not recommended?