Pine Script多单strategy.exit未执行,空单正常问题求助
多单追踪止盈策略触发异常问题
手动设置追踪止盈规则:持仓多单时每次创出新高、持仓空单时每次创出新低,都会重新计算追踪止盈价。目前空单运行完全正常,但多单存在触发异常。
多单相关代码
if strategy.position_size > 0 trade_entry_price := strategy.opentrades.entry_price(0) longStopPrice := trade_entry_price * (1 - longSL) longTakePrice := trade_entry_price * (1 + longTP) strategy.exit("Exit", from_entry = "LONG", stop = longStopPrice, comment = "Long Stop Loss " + str.tostring(longStopPrice) + " / " + str.tostring(trade_entry_price)) if trail_longTP == 0.0 if high > longTakePrice trail_longTP := high * (1 - longTO) strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on first calc") else if (high * (1 - longTO)) > trail_longTP b_long_to_tp_set := true trail_longTP := high * (1 - longTO) strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc")
未执行的代码片段
strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc")
调试情况
已添加调试变量b_long_to_tp_set := true确认已进入更新追踪价的条件,图表显示trail_longTP(蓝线)已更新,但在标记的K线中价格已穿越该价位,订单却未触发。
临时解决方案(不符合需求)
添加以下代码后虽能触发,但成交时机晚于预期,不符合需求:
else if high[1] > high or close < trail_longTP strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit close price < trail long price")
完整修改后代码
if strategy.position_size > 0 trade_entry_price := strategy.opentrades.entry_price(0) longStopPrice := trade_entry_price * (1 - longSL) longTakePrice := trade_entry_price * (1 + longTP) strategy.exit("Exit", from_entry = "LONG", stop = longStopPrice, comment = "Long Stop Loss " + str.tostring(longStopPrice) + " / " + str.tostring(trade_entry_price)) if trail_longTP == 0.0 if high > longTakePrice trail_longTP := high * (1 - longTO) strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on first calc") else if (high * (1 - longTO)) > trail_longTP b_long_to_tp_set := true trail_longTP := high * (1 - longTO) strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc") else if high[1] > high or close < trail_longTP strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit close price < trail long price")
恳请提供有效的解决方案。
内容的提问来源于stack exchange,提问作者user23400508
相关产品推荐
相关产品推荐

