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
相关产品推荐
相关产品推荐

