Tradingview Pine script `strategy.exit` and `strategy.close` don't respect `from_entry` value

怎甘沉沦 提交于 2021-02-11 15:17:58

问题


I have several different entries in my strategy, and I want to assign separate stop losses to them:

// @version=4
strategy("Test strategy")
strategy.entry("E0", strategy.long, limit=10000, when=close[1] > 10000)
strategy.entry("E1", strategy.long, limit=10000, when=close[1] > 10000)
strategy.exit("SL-E0", "E0", stop=9000)
strategy.exit("SL-E1", "E1", stop=9500)

As far as I understand the documentation (https://www.tradingview.com/pine-script-reference/#fun_strategy{dot}exit) the 2nd parameter of strategy.exit should cause the exit to apply only to the matching entry, however looking at the trade list (when applied to BTCUSD on a 2h timeframe - for reference) I see this:

1   Entry Long  E0      2019-07-02 14:00    10000.0 
    Exit Long   SL-E1   2019-07-17 02:00    9500.0
2   Entry Long  E1      2019-07-02 14:00    10000.0
    Exit Long   SL-E0   2019-09-25 04:00    9000.0

So the wrong stop loss is being applied. Is this a bug? I have tried numerous different configurations of the exit call, including loss instead of stop, as well as pulling the condition external:

if low < 9000
    strategy.exit("SL-E0", "E0")

All have the same effect whereby "SL-E1" causes "E0" to exit.


回答1:


Try this:

// @version=4
strategy("Test strategy", close_entries_rule="ANY")
strategy.entry("E0", strategy.long, limit=10000, when=close[1] > 10000)
strategy.entry("E1", strategy.long, limit=10000, when=close[1] > 10000)
strategy.exit("SL-E0", "E0", stop=9000)
strategy.exit("SL-E1", "E1", stop=9500)


来源:https://stackoverflow.com/questions/64743470/tradingview-pine-script-strategy-exit-and-strategy-close-dont-respect-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!