You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改均线交叉Pine Script参数后策略测试器显示‘NO DATA’求助

Pine Script 策略测试器显示「NO DATA」问题解决

问题根源分析

  • 历史数据量不足:使用50/200周期均线时,策略需要至少200根K线才能计算出有效的慢均线值。如果回测起始日期过近,没有足够历史数据支撑均线计算,就会触发「NO DATA」提示。
  • 平仓逻辑错误:代码中平空单的条件使用strategy.position_size > 0,但空单的仓位大小为负数,该条件永远无法触发,导致空单无法平仓,长期持仓后会引发测试数据异常。

修复步骤

1. 调整回测数据范围

在策略测试器中,将「From」日期设置为更早的时间,确保回测区间包含至少200根以上对应周期的K线数据(比如日线图需覆盖至少200天历史)。

2. 修正平仓逻辑

将平空单的条件从strategy.position_size > 0改为strategy.position_size < 0,才能正确识别空单仓位并触发平仓:

//close shorts
if strategy.position_size < 0 and bullCross
    strategy.close(id="short")

3. 优化开仓条件(可选)

移除开仓条件中的strategy.position_size == 0限制,避免因仓位判断逻辑冗余导致的信号遗漏:

buySignal = bullCross and not na(maFast) and not na(maSlow) and barstate.isconfirmed
sellSignal = bearCross and not na(maFast) and not na(maSlow) and barstate.isconfirmed

完整修复代码

strategy("Cross Over Strategies", overlay=true)

//get user input
fastMaLength = input.int(title="fast ma length", defval=5, minval=1, maxval=200, tooltip="Donchian Parameter= 5/20, golden cross = 50/200")
slowMaLength = input.int(title="slow ma length", defval=20, minval=1 , maxval=200, tooltip="Donchian Parameter= 5/20, golden cross = 50/200")

//calcs
//get ma values
maFast = ta.sma(close, fastMaLength)
maSlow = ta.sma(close, slowMaLength)

//calc crossover
bullCross = ta.crossover(maFast, maSlow)
bearCross = ta.crossunder(maFast, maSlow)

//detect buy and sell filter
buySignal = bullCross and not na(maFast) and not na(maSlow) and barstate.isconfirmed
sellSignal = bearCross and not na(maFast) and not na(maSlow) and barstate.isconfirmed

//enter buy orders
if buySignal
    strategy.entry(id="long", direction=strategy.long)

//enter sell orders
if sellSignal
    strategy.entry(id="short", direction=strategy.short)
    
// strategy.close function used for exiting on bool conditions
//close longs
if strategy.position_size > 0 and bearCross
    strategy.close(id="long")
//close shorts
if strategy.position_size < 0 and bullCross
    strategy.close(id="short")
    
//draw data to chart
plot(maFast, color=color.green, linewidth=2)
plot(maSlow, color=color.red, linewidth=2)
plotshape(bullCross, color=color.green, style=shape.triangleup, location=location.belowbar, size=size.normal)
plotshape(bearCross, color=color.red, style=shape.triangledown, location=location.abovebar, size=size.normal)

内容的提问来源于stack exchange,提问作者rvern

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 03:36:19