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

Pine Script循环逻辑问题:订单成交平仓/3K未成交撤单需求无法实现

问题:订单成交判断与超时撤单逻辑实现困惑

我搞不定循环逻辑,需求是:要么等到str.contains(orderIds, '2') == true(订单成交),要么3根K线后执行strategy.cancel撤单。折腾一天了,循环就是跑不对。具体需求:通过orderIds判断持仓状态,订单成交时用strategy.exit平仓;如果3根K线后还没成交,就撤单。

以下是我的代码实现:

var line[] imbalancedUpMidLines = array.new_line()
var line[] imbalancedUpMidLines2 = array.new_line()


orderIds = ""

for idx = 0 to strategy.opentrades - 1
    strSuffix = idx == strategy.opentrades - 1 ? "" : ", "
    orderIds += strategy.opentrades.entry_id(idx) + strSuffix

//Show result on the chart
label.new(bar_index, high, orderIds)



if entry_signal_all
    if str.contains(orderIds, '1') == false
        array.push(imbalancedUpMidLines, line.new(x1=bar_index, y1=high, x2=bar_index+20, y2=high, extend=extend.right, color = color.red))
        strategy.order(id = '1', direction= strategy.long, qty = 0.1, limit = atrBandBot)
        line1=line.new(x1=bar_index, y1=high, x2=bar_index+20, y2=high, extend=extend.right, color = color.red)
        i = 1    
        while str.contains(orderIds, '1') == true  
            if i >= 3
                strategy.cancel('1')
                break
            else
                i += 1
        strategy.exit(id = "C1", from_entry= '1', limit=line.get_y1(line1)) 


    else       
        if str.contains(orderIds, '2') == false
            array.push(imbalancedUpMidLines2, line.new(x1=bar_index, y1=high, x2=bar_index+20, y2=high, extend=extend.right, color = color.red))
            strategy.order(id = '2', direction= strategy.long, qty = 0.1, limit = atrBandBot)
            line2=line.new(x1=bar_index, y1=high, x2=bar_index+20, y2=high, extend=extend.right, color = color.red)
            i = 1    
            while str.contains(orderIds, '2') == true  
                if i >= 3
                    strategy.cancel('2')
                    break
                else
                    i += 1
                strategy.exit(id = "C2", from_entry= '2', limit=line.get_y1(line2)) 

我还尝试过以下循环写法,但还是没头绪:

i = 1    
while i >= 3  
    if str.contains(orderIds, '2')
        strategy.exit(id = "C2", from_entry= '2', limit=line.get_y1(line2))
        break
    else if i <= 3
        strategy.cancel('2')
        break
    else
        i += 1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 13:10:50