Pine Script策略止损单次日跳空高开无法触发的解决办法咨询
Pine Script 跳空高开触发进场单的解决方案
问题根源
Pine Script的strategy.entry带stop参数时,默认要求价格实体穿过stop价位才会触发进场。如果次日开盘直接跳空至stop价上方(比如stop设为320,开盘价321),当前K线的最低价高于stop价,没有价格从下往上穿过stop的过程,回测引擎就不会触发该进场单。
解决方法
1. 拆分挂单与开盘价检查逻辑
用strategy.order提前挂stop单,同时在次日开盘时主动检查跳空情况,手动触发进场:
// 计算当日最低价(示例逻辑,可根据实际需求调整) today_low = ta.lowest(low, ta.barssince(ta.change(timeframe.period)) + 1) // 常规K线结束时挂stop进场单 if session.islastbar_regular strategy.order(id='Long_Stop', strategy.long, stop=today_low) // 次日开盘时,若开盘价高于stop价且无仓位,直接触发进场 if not session.islastbar_regular and open > today_low and strategy.position_size == 0 strategy.entry(id='Long_Gap', strategy.long)
2. 用变量存储待触发价位,开盘直接判断
通过变量记录前一天设置的stop价,次日开盘时直接验证条件:
today_low = ta.lowest(low, ta.barssince(ta.change(timeframe.period)) + 1) var float pending_stop = na // 记录待触发的进场价位 if session.islastbar_regular pending_stop := today_low // 跳空高开且无仓位时触发进场 if not na(pending_stop) and open > pending_stop and strategy.position_size == 0 strategy.entry(id='Long_Gap', strategy.long) pending_stop := na // 触发后重置变量
3. 开启逐tick计算
在策略开头开启calc_on_every_tick参数,让回测引擎检查每一个tick的价格变化,跳空时的开盘tick会被识别为穿过stop价的过程:
strategy("你的策略名称", overlay=true, calc_on_every_tick=true) // 原有策略逻辑 today_low = ta.lowest(low, ta.barssince(ta.change(timeframe.period)) + 1) if session.islastbar_regular strategy.entry(id='Long', direction=strategy.long, stop=today_low)
注意:开启逐tick计算会增加回测的计算量,低时间框架策略可能出现性能下降,需根据实际情况选择。
内容的提问来源于stack exchange,提问作者jerin jacob
相关产品推荐
相关产品推荐

