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

TradingView Pine Script策略回测报错求助:EMA交叉策略编译失败

问题分析与修正方案

错误点1:缺少Pine Script版本声明

Pine Script强制要求在脚本最顶部声明版本,错误提示已明确指出需添加版本声明,推荐使用兼容性更好的高版本(比如//@version=5)。

错误点2:EMA周期不能使用非整数

ema()函数的周期参数必须是整数,你写的48.5不符合函数要求,需修改为整数,比如48或49。

错误点3:strategy.exit用法错误

原代码中strategy.exit的参数格式不正确,若要平仓,直接用strategy.close()指定仓位名称即可,无需将strategy.close作为参数传入。


修正后的完整代码

//@version=5
strategy("EMA Cross Strategy", overlay=true)

// 设置EMA周期
lookbackShort = 13
lookbackLong = 48  // 修正为整数周期

// 计算长短周期EMA
emaShort = ta.ema(close, lookbackShort)
emaLong = ta.ema(close, lookbackLong)

// 短EMA上穿长EMA,开多单
if ta.crossover(emaShort, emaLong)
    strategy.entry("Long", strategy.long)

// 短EMA下穿长EMA,平多单
if ta.crossunder(emaShort, emaLong)
    strategy.close("Long")

// 在图表上绘制EMA线
plot(emaShort, color=color.red, title="13日EMA")
plot(emaLong, color=color.blue, title="48日EMA")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 12:15:31