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

Trading View Pine Script策略回测“No Data”错误原因及代码排查求助

TradingView Pine Script 回测"No Data"错误排查及代码修复

问题场景

使用以下Pine Script策略进行TradingView回测时,出现"No Data"错误,无法正常显示回测结果:

//@version=5
strategy("My script",overlay=true)

ma60 = ta.sma(close,60)

price=close

_color = if ma60 > ma60[1]
color.green
else
    color.red

plot(ma60,color = _color,linewidth = 2)
plot(ma200,color = color.orange, linewidth=3)

//long
if price>ma60 and price> ta.highest(20)
    strategy.entry("longposition",strategy.long,comment="Long Entry")
if price<ma60
     strategy.close("longposition",comment = "long Exit")

//short
if price<ma60 and price< ta.lowest(20)
    strategy.entry("shortposition",strategy.short,comment="Long Entry")
if price<ma60
    strategy.close("shortposition",comment = "long Exit")

//sl
strategy.exit("Exit long sl",from_entry = "longposition",stop=low )
strategy.exit("Exit short sl",from_entry = "shortposition",stop=high )

策略逻辑:当价格突破MA60且收盘价为最近20根K线的最高价时做多,价格回落至MA60时平仓;当价格跌破MA60且收盘价为最近20根K线的最低价时做空,价格回到MA60时平仓,并设置止损。

错误原因分析

  1. 未定义变量ma200:代码中调用plot(ma200,...)但未提前定义该变量,导致脚本编译失败,直接触发"No Data"错误。
  2. 空单平仓条件逻辑错误:空单平仓条件写为price<ma60,与空单开仓条件一致,会导致开仓后立刻平仓,无有效持仓,回测无数据生成。
  3. 止损设置逻辑缺陷:直接使用当前K线的low/high作为止损价,会随每根K线更新而变动,无法固定开仓时的止损位置,可能引发异常。
  4. 空单开仓注释错误:注释写为"Long Entry",与实际做空逻辑不符,属于标注错误。

修复后的代码

//@version=5
strategy("My script", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 定义缺失的MA200变量
ma60 = ta.sma(close, 60)
ma200 = ta.sma(close, 200)

price = close

_color = ma60 > ma60[1] ? color.green : color.red

plot(ma60, color=_color, linewidth=2)
plot(ma200, color=color.orange, linewidth=3)

// 做多逻辑
if price > ma60 and price > ta.highest(20)
    strategy.entry("longposition", strategy.long, comment="Long Entry")
// 多单平仓:价格回落至MA60下方
if price < ma60
    strategy.close("longposition", comment="Long Exit")

// 做空逻辑
if price < ma60 and price < ta.lowest(20)
    strategy.entry("shortposition", strategy.short, comment="Short Entry")
// 空单平仓:价格回到MA60上方(修正原逻辑错误)
if price > ma60
    strategy.close("shortposition", comment="Short Exit")

// 止损设置:固定开仓K线的高低点作为止损
var float long_stop = na
var float short_stop = na

if strategy.position_size > 0 and strategy.opentrades.entry_id(strategy.opentrades - 1) == "longposition"
    long_stop := ta.low[1] // 用开仓前一根K线的低点作为多单止损
if strategy.position_size < 0 and strategy.opentrades.entry_id(strategy.opentrades - 1) == "shortposition"
    short_stop := ta.high[1] // 用开仓前一根K线的高点作为空单止损

strategy.exit("Exit long sl", from_entry="longposition", stop=long_stop)
strategy.exit("Exit short sl", from_entry="shortposition", stop=short_stop)

修复说明

  1. 补充定义ma200变量,解决编译错误。
  2. 修正空单平仓条件为price>ma60,符合策略逻辑(价格回到MA60上方时平仓空单)。
  3. 调整止损设置逻辑,固定开仓时的止损价,避免随K线变动,同时使用var变量保存止损值,确保稳定性。
  4. 修正空单开仓的注释为"Short Entry",标注准确。
  5. 添加基础策略参数(初始资金、佣金),优化回测环境配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:01:00