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

Pine Script v5提示找不到ema函数报错 DEMA+SuperTrend策略问题求助

问题解答

一、报错解决方案

你触发的ema函数未找到报错是Pine Script v5版本的语法规则导致的:

  • v5版本将所有技术分析类函数统一收纳到ta命名空间下,v4及更早版本直接写ema()的写法在v5中不生效,需要改为ta.ema()
  • 另外你代码最后一行平仓语句存在参数错误,strategy.close函数仅需传入要平仓的订单ID即可,不需要额外加strategy.close作为第二个参数。

修正涉及的三处代码:

  1. 第13行:e1 = ta.ema(src, demaLength)
  2. 第14行:e2 = ta.ema(e1, demaLength)
  3. 第21行:strategy.close("long")

二、策略逻辑正确性验证

你的逻辑方向完全正确,符合趋势跟踪类策略的常规设计思路:

  • 200周期DEMA作为大趋势过滤器,仅在价格高于DEMA、整体趋势向上的环境下开多,能有效过滤下跌趋势中的无效做多信号
  • SuperTrend指标的direction字段小于0时代表价格上穿SuperTrend线,是标准的做多触发信号;direction大于0时代表价格下穿SuperTrend线,作为平仓信号也符合指标的常规用法。

修正后完整可运行代码

//@version=5
strategy("DEMA and SuperTrend", overlay=true)

// SuperTrend
atrPeriod = input(12, "ATR Length")
factor = input.float(3.0, "Factor", step = 0.01)

[_, direction] = ta.supertrend(factor, atrPeriod)

// DEMA
demaLength = input(200)
src = input(close, title="Source")
e1 = ta.ema(src, demaLength)
e2 = ta.ema(e1, demaLength)
dema = 2 * e1 - e2

if ta.change(direction) < 0 and close > dema
    strategy.entry("long", strategy.long)
    
if ta.change(direction) > 0
    strategy.close("long")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 02:24:04