0
votes
longcon = crossover(ema5, ema21)
longCondition1 = longcon[4]
longCondition2 = ema50 > ema200 ? 1: 0
longCondition3 = bbr > 0.75 ? 1: 0
longCondition4 = sig > 15 ? 1: 0
longCondition5 = ao > 2 ? 1: 0
if (longCondition1 and longCondition2 and longCondition3 and longCondition4 and longCondition5)
    strategy.entry("long", strategy.long)
    strategy.exit("exit", "long", stop=strategy.position_avg_price*0.98, limit=strategy.position_avg_price*1.01)
plot(strategy.position_avg_price)
plot(strategy.position_avg_price*0.98)
plot(strategy.position_avg_price*1.01)

I have been trying to backtest this strategy but for some reason, the Stop Loss and Take Proft (Limit) do not work properly and I have no idea why. I plotted those values and the script just does not execute when those levels are reached. enter image description here

You can see the first exit is incorrect as the price increased by 1% but the script did not sell. The second time around the script works fine. What causes this?

1

1 Answers

0
votes

I just figured it out:

if (longCondition1 and longCondition2 and longCondition3 and longCondition4 and longCondition5)
    strategy.entry("long", strategy.long)
strategy.exit("exit", "long", stop=strategy.position_avg_price*0.98, limit=strategy.position_avg_price*1.01)