Pine Script策略异常:反向订单互平仓问题排查求助
策略异常:反向订单交叉平仓且图表无显示
问题现象
- 出现空单平仓多单、多单平仓空单的异常交叉平仓情况
- 尝试通过
strategy.order并设置close_entries_rule = 'ANY'修复,未生效 - 实际成交逻辑:多单被空单平仓、空单已平仓,但图表完全不显示这些操作
异常情况截图



策略代码
if bar_1s_prev_close_First_Value < bar_5m_prev_low// and barstate.isconfirmed// and ht_ema < (close / P_ema) if str.contains(orderIds, 'long') == false and array.get(openOrder1_long, 0) == 0 mid_long_test = (bar_5m_prev_low + bar_1s_prev_close_First_Value) / 2 if mid_long_test > (bar_1s_prev_close_First_Value * (min_dif_trade_small + 1)) line.new(x1=bar_index, y1=mid_long_test, x2=bar_index+3, y2=mid_long_test, color = color.red, width = 2) SL_long = low / (1 + tp1_input_perc) array.set(L_SL_line, 0, SL_long) array.set(limitClose_long, 0, mid_long_test) array.set(limitEntry_long, 0, bar_1s_prev_close_First_Value / (1 + limitEntry_offset)) line.new(x1=bar_index, y1=array.get(limitEntry_long, 0), x2=bar_index+3, y2=array.get(limitEntry_long, 0), color = color.rgb(21, 255, 0), width = 2) strategy.order(id = 'long', direction=strategy.long, limit = array.get(limitEntry_long, 0), stop = array.get(L_SL_line, 0)) strategy.exit(id = 'C S long', from_entry='long', limit = array.get(limitClose_long, 0), stop = array.get(L_SL_line, 0)) //L_s_count := L_s_count+1 bear_imbalence_simple := low / 1.001 array.set(openOrder1_long, 0, 1) if str.contains(orderIds, 'long') == true array.set(openOrder1_long, 0, 0) if bar_1s_prev_close_First_Value > array.get(limitClose_long, 0) if str.contains(orderIds, 'long') == false strategy.cancel('long') strategy.cancel('C S long') array.set(openOrder1_long, 0, 0) //------------------------------------------------------------------------------------------ //short if bar_1s_prev_close_First_Value > bar_5m_prev_high // and barstate.isconfirmed// and ht_ema < (close / P_ema) if str.contains(orderIds, 'short') == false and array.get(openOrder1_short, 0) == 0 mid_short_test = (bar_5m_prev_high + bar_1s_prev_close_First_Value) / 2 if mid_short_test < (bar_1s_prev_close_First_Value / (min_dif_trade_small + 1)) line.new(x1=bar_index, y1=mid_short_test, x2=bar_index+3, y2=mid_short_test, color = color.red, width = 2) SL_short = high * (1 + tp1_input_perc) array.set(S_SL_line, 0, SL_short) array.set(limitClose_short, 0, mid_short_test) array.set(limitEntry_short, 0, bar_1s_prev_close_First_Value * (1 + limitEntry_offset)) line.new(x1=bar_index, y1=array.get(limitEntry_short, 0), x2=bar_index+3, y2=array.get(limitEntry_short, 0), color = color.rgb(21, 255, 0), width = 2) strategy.order(id = 'short', direction=strategy.short, limit = array.get(limitEntry_short, 0), stop = array.get(S_SL_line, 0)) strategy.exit(id = 'C S short', from_entry= 'short', limit = array.get(limitClose_short, 0), stop = array.get(S_SL_line, 0)) //L_s_count := L_s_count+1 bull_imbalence_simple := low / 1.001 array.set(openOrder1_short, 0, 1) if str.contains(orderIds, 'short') == true array.set(openOrder1_short, 0, 0) if bar_1s_prev_close_First_Value < array.get(limitClose_short, 0) if str.contains(orderIds, 'short') == false strategy.cancel('short') strategy.cancel('C S short') array.set(openOrder1_short, 0, 0)
问题成因与修复建议
核心问题分析
- 订单状态判断逻辑不可靠:用
str.contains(orderIds, 'long')判断订单存在性有漏洞,orderIds是动态集合,订单成交后会被移除,导致提前重置openOrder1_long状态,触发反向订单时未检查当前持仓。 - 未控制反向订单触发时机:代码未检查当前是否已有反向持仓,Pine Script默认会用新订单平仓旧持仓,形成交叉平仓,但退出规则未正确绑定持仓,导致图表不显示平仓动作。
close_entries_rule设置位置错误:该参数需在strategy()初始化时设置,单独在strategy.order中设置不生效。
具体修复步骤
- 初始化时配置平仓规则:在策略开头的
strategy()函数中添加参数,确保反向订单平仓逻辑符合预期:strategy("你的策略名称", overlay=true, close_entries_rule='ANY') - 用持仓状态控制订单触发:替换
str.contains(orderIds, 'long')的判断,改用当前持仓方向校验:- 多单触发前检查
strategy.position_size <= 0(无持仓或空单已平仓) - 空单触发前检查
strategy.position_size >= 0(无持仓或多单已平仓)
- 多单触发前检查
- 优化挂单状态检查:移除冗余的数组判断,改用
strategy.opentrades检查未成交挂单:has_pending_long = strategy.opentrades.size > 0 and strategy.opentrades.entry_id(0) == 'long' - 主动平仓反向持仓:触发反向订单前,主动平仓现有持仓,避免依赖默认交叉平仓:
// 触发空单前先平仓多单 if strategy.position_size > 0 strategy.close('long')
内容的提问来源于stack exchange,提问作者MENTORJJ
相关产品推荐
相关产品推荐

