【问题标题】:Pine Script: How to cancel order if not filled on bar after entry condition is met?Pine Script: 满足入场条件后柱未成交如何取消订单?
【发布时间】:2022-07-09 11:51:16
【问题描述】:

尝试使用barssince函数取消挂单。

如果未在相同的蜡烛订单上执行/在满足入场条件后的蜡烛上,希望它取消。我不明白为什么它不起作用它看起来很简单。

rp1 = close[1] >= open [1] and close < open and high >= high[1] 

rp1p = highest(high,1)

plotshape(rp1, style=shape.circle, location=location.abovebar, color=color.red)

if rp1 and strategy.opentrades == 0 
    
    strategy.entry("RP", strategy.long, stop=rp1p)
    
    strategy.cancel("RP", when = barssince(rp1 and strategy.opentrades == 0) == 1)

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    我也遇到过同样的问题。 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 的信号的解决方法。完成后我将分享我的解决方案。

    最好的问候, 朱利亚诺·桑托斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多