You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script中未在下根K线成交的buystop订单如何取消撮合

你需要先记录入场条件触发的K线位置,在后续K线判断订单是否超时未成交,主动取消对应订单即可,修改后的代码如下:

//@version=5
strategy("13日突破策略(订单有效期1根K)", overlay=true)

// 变量声明:记录待成交订单的触发bar索引,以及是否有待成交订单
var pendingLongBar = na
var hasPendingLong = false

hh = ta.highest(high, 13)
longCondition = close >= hh[1]

// 触发入场条件时发单,同时记录触发位置
if (longCondition)
    StopPrice = high + syminfo.mintick
    LimitPrice = StopPrice + (4 * syminfo.mintick)
    strategy.entry("13 days breakout", long=true, stop=StopPrice, limit=LimitPrice)
    pendingLongBar = bar_index
    hasPendingLong = true

// 检查是否到了触发后的下一根K,且订单仍未成交,执行取消
if hasPendingLong and bar_index == pendingLongBar + 1
    // 检查当前该订单是否已成交
    if strategy.order("13 days breakout").status == strategy.orderstatus.filled
        hasPendingLong = false
    else
        // 未成交则取消订单
        strategy.cancel("13 days breakout")
        hasPendingLong = false

关键说明:

  • 你之前调用strategy.Cancel未生效,绝大多数情况是取消的订单ID和strategy.entry中定义的ID不一致,或者取消触发时机不对
  • 上述代码仅允许订单在触发后的下一根K线内撮合,超时未成交就会直接取消,不会在后续K线继续等待

内容的提问来源于stack exchange,提问作者Abhay Sahu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 13:36:03