Thanks for the script. I can now see the reason of the problem.
You're entering trades at an entry stop and profit target that are both sometimes below the actual price. Therefore the entry stop is hit immediately, but the profit target is at the wrong side of the price, so the trade is rejected with an error message.
In the messages you don't see the actual price, but only your entry and profit targets. This had me also confused for a moment. We'll change that message so that the real price will also be displayed.