我也遇到过同样的问题。 Pine 似乎有一个限制,即 strategy.opentrades 仅在输入有效执行时才会更改。
最初,我尝试使用以下代码取消未填写的条目:
candlesToEntrytInput = input.int(defval=3, title="Limit in candles to trigger")
longConditionWasPlaced = <any rule you want>
targetPrice = <your target price>
entryID = "Entry 123:\n" + str.tostring(bar_index)
strategy.entry(entryID, strategy.long, stop=targetPrice, when=longConditionWasPlaced)
for tradeNumber = 0 to strategy.opentrades - 1
tradeEntryID = strategy.opentrades.entry_id(tradeNumber)
splitPosition = str.pos(tradeEntryID, ":")
signalBar = str.tonumber(str.substring(traderEntryID, splitPosition + 1))
entryIsNoLongerValid = bar_index - signalBar == candlesToEntrytInput and strategy.opentrades.size(tradeNumber) == 0
strategy.cancel(traderEntryID, when=entryIsNoLongerValid)
这种方法也不起作用。所以我尝试了另一种解决方案:
shouldCancelEntry = ta.barssince(longConditionWasPlaced) == candlesToEntrytInput and strategy.position_size == 0
strategy.cancel(entryID, when=shouldCancelEntry)
第二种方法确实有效。但是,问题是当两个连续的长信号出现时,逻辑就会失败。
如果 strategy.entry 语句有一个 thresholdInCandlesToEntry 参数就好了。这样就解决了这个问题。
我正在使用一个辅助数组来控制我对 strategy.opentrades 的信号的解决方法。完成后我将分享我的解决方案。
最好的问候,
朱利亚诺·桑托斯