TradingView PineScript策略与SAR指标信号延迟1根K线问题求助
问题:TradingView SAR策略信号延迟1根K线的问题
我编写的TradingView策略和SAR指标存在信号不同步的问题:直接看指标能找到正确入场点,但策略执行时总是延迟1根K线开仓。
策略逻辑:当SAR从K线上方移动到下方时做多,从下方移动到上方时做空。
原策略代码:
//@version=5 strategy("myStrategy", overlay=true,calc_on_every_tick = true, process_orders_on_close =true) start = input(0.02) increment = input(0.02) maximum = input(0.2, "Max Value") SAR = ta.sar(start, increment, maximum) plot(SAR, "ParabolicSAR", style=plot.style_cross, color=#f2f4f8,linewidth = 4) if SAR[1]>high[1] and SAR<low strategy.entry("Buy", strategy.long,comment="Buy",qty=1) if SAR[1]<low[1] and SAR>high strategy.entry("Sell", strategy.short,comment="Sell",qty=1)
肉眼观察时,SAR在当前K线开盘就出现信号,我会立刻开仓,但策略要等到当前K线收盘才执行开仓。想让策略和指标信号同步,在同一根K线开盘时就开仓,请问差异原因是什么?怎么解决?
原因分析
- 订单执行时机参数设置错误:你开启了
process_orders_on_close = true,这个参数会强制策略在当前K线完全收盘后才执行订单,直接导致信号触发延迟。 - 入场条件依赖未确定的K线数据:你的做多条件用了
SAR<low,做空条件用了SAR>high——low和high是当前K线的最低价和最高价,只有到K线收盘时才能确定最终值。开盘时这些数据还在变动,策略自然无法在开盘时触发信号。
解决方案
1. 修改订单执行时机参数
把process_orders_on_close = true改成process_orders_on_close = false,这样策略会在信号出现的第一时间(实时tick上)执行订单,不用等收盘。
2. 调整入场条件逻辑
放弃依赖当前K线的low/high,改用已确定的历史数据或者实时价格来判断SAR的穿越信号。比如用开盘价(K线启动时就确定的数值)作为判断基准,结合ta.crossover/ta.crossunder函数精准识别穿越动作。
修改后的完整代码
//@version=5 strategy("myStrategy", overlay=true, calc_on_every_tick = true, process_orders_on_close = false) start = input(0.02) increment = input(0.02) maximum = input(0.2, "Max Value") SAR = ta.sar(start, increment, maximum) plot(SAR, "ParabolicSAR", style=plot.style_cross, color=#f2f4f8, linewidth = 4) // 判断SAR从上方穿越至当前K线开盘价下方,同时确认前一根K线SAR在价格上方 if ta.crossunder(SAR, open) and SAR[1] > high[1] strategy.entry("Buy", strategy.long, comment="Buy", qty=1) // 判断SAR从下方穿越至当前K线开盘价上方,同时确认前一根K线SAR在价格下方 if ta.crossover(SAR, open) and SAR[1] < low[1] strategy.entry("Sell", strategy.short, comment="Sell", qty=1)
补充说明
- 保留
calc_on_every_tick = true是正确的,它能让策略实时计算每一个tick的数据,保证信号不遗漏。 - 如果想更贴近你肉眼观察的信号时机,也可以把
open替换成close[1](前一根K线收盘价),根据自己的观察习惯调整即可。
内容的提问来源于stack exchange,提问作者mohsen taherinia
相关产品推荐
相关产品推荐

