Found the following in pine-script and not sure if it's a bug or if there is a mention about it in the documentation that I haven't noticed.
Say for ticker GOLD
This works:
//@version=4
strategy("My Script",overlay=true, process_orders_on_close=true)
if (time > timestamp(2020, 02, 28,0,0,0))
strategy.entry("buy", strategy.long, comment = "Long")
Stop1 = 18.03
Stop2 = 16.5
if (low < Stop1)
strategy.cancel_all()
strategy.close_all(comment = "STOP1")
else if (low < Stop2)
strategy.cancel_all()
strategy.close_all(comment = "STOP2")
However, this does not work (position doesn't close)
//@version=4
strategy("My Script",overlay=true, process_orders_on_close=true)
plot(close)
if (time > timestamp(2020, 02, 28,0,0,0))
strategy.entry("buy", strategy.long, comment = "Long")
Stop1 = 18.03
Stop2 = 16.5
if (low < Stop1)
strategy.cancel_all()
strategy.close_all(comment = "STOP1")
if (low < Stop2)
strategy.cancel_all()
strategy.close_all(comment = "STOP2")
I would expect, in the second version, that "STOP2" would cancel "STOP1" and proceed with closing the position, However neither of the orders seems to be placed.
Please let me know if I'm doing something wrong so that I know it going forward. My code was behaving unexpectedly and it took me ages to narrow it down :)
Thanks!
EDIT: Here is a screenshot of the problem.
After looking into it a bit more, I THINK it has to do with the decimals. This works:
Stop1 = 16.5
Stop2 = 18.3
.. but this doesn't work
Stop1 = 16.5
Stop2 = 18.03