0
votes

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. GOLD symbol

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  
1

1 Answers

0
votes

Version 1

Your first example doesn't work because the else block is never executed. If the first condition is false, the second one will never be true. This would work:

//@version=4
strategy("Cancels",overlay=true, process_orders_on_close=true)

if (time > timestamp(2020, 02, 28,0,0,0))
    strategy.entry("buy", strategy.long, comment = "Long")

Stop1 = 280
Stop2 = 265

if (low < Stop2)
    strategy.cancel_all()
    strategy.close_all(comment = "STOP2")
else if (low < Stop1)
    strategy.cancel_all()
    strategy.close_all(comment = "STOP1")

enter image description here

Version 2

When both the low < Stop2 and low < Stop1 were true, I think your strategy.cancel() calls were canceling each other out.

One is eliminated here. Perhaps this is more what you are looking for:

//@version=4
strategy("My Script",overlay=true, process_orders_on_close=true)

Stop1 = 18.0
Stop2 = 16.0

if (low < Stop2)
    strategy.close_all(comment = "STOP2")
else if (low < Stop1)
    strategy.cancel_all()
    strategy.close_all(comment = "STOP1")
else if (time > timestamp(2019, 02, 28,0,0,0))
    strategy.entry("buy", strategy.long, comment = "Long")

enter image description here