try to use a boolean in the below manner excluding the if statements triggering the entry.
strategy.entry("long", when=longCondition and testPeriod())
After removing the if , then try removing the // comments prior to the exit to see if it works.
I dunno if I'm correct (I could be wrong) but i'm assuming that the profit / loss is a float that wouldn't be search if untill your if statement is fullfilled while in between the movements are not calculated while your if is false.
It fixed up a mess of 2 weeks for me ... try it ...
This is my script:
length = input(9, minval=0)
displace = input(0, minval=0)
highsma = sma(high, length)
lowsma = sma(low, length)
longCondition = close > highsma and strategy.position_size <= 0
strategy.entry("long", true, comment="compra")
strategy.exit("exit", "long", profit=31, loss=10, comment="saida compra")
shortCondition = close < lowsma and strategy.position_size >= 0
strategy.entry("short", false, comment="venda")
strategy.exit("exit", "short", profit=30, loss=10, comment="saida venda")