Study Error: negative quantity for entry or order

Viewed 584

Can't seem to figure out why I'm getting a negative "quantity for entry or order" when backtesting my strategy when I go Short. Long orders work fine but when I switch to shorting I get the error. My order execution for shorting is a mirror opposite of my long execution. Here is a shortened version of my order execution.

'''if LongOrShort
       if (longConditions and strategy.position_size <= 0)
           strategy.entry("Long", true)
    
       if (closeLongConditions and strategy.position_size > 0)
           strategy.close("Long")


   else
       if (shortConditions and strategy.position_size <= 0)
           strategy.entry("Short", false)
    
       if (closeShortConditions and strategy.position_size > 0)
           strategy.close("Short")'''

 
1 Answers

I believe that the strategy.position_size returns short positions as a negative as on a broker. Try changing the lte and gt:

       if (shortConditions and strategy.position_size >= 0)
           strategy.entry("Short", false)
    
       if (closeShortConditions and strategy.position_size < 0)
           strategy.close("Short")'''
Related