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

多时间周期EMA的Pine Script买卖预警与图表显示问询

多EMA上穿/下穿的买卖预警与图表信号实现(Pine Script)

你的核心逻辑没问题,补全EMA计算、信号可视化和预警配置的部分后,就能实现需求。以下是完整可运行代码,附带关键说明:

//@version=5
indicator("多EMA买卖信号与预警", overlay=true)

// 自定义EMA周期参数(可在指标设置中调整)
ema_period_1 = input.int(20, title="EMA周期1")
ema_period_2 = input.int(50, title="EMA周期2")
ema_period_3 = input.int(100, title="EMA周期3")

// 计算三条不同周期的EMA
ema_1 = ta.ema(close, ema_period_1)
ema_2 = ta.ema(close, ema_period_2)
ema_3 = ta.ema(close, ema_period_3)

// 在图表上绘制三条EMA均线
plot(ema_1, color=color.blue, title="EMA1")
plot(ema_2, color=color.red, title="EMA2")
plot(ema_3, color=color.green, title="EMA3")

// 你的原条件逻辑(收盘价上穿/下穿任一EMA触发信号)
long_cond = ta.crossover(close, ema_1) or ta.crossover(close, ema_2) or ta.crossover(close, ema_3)
short_cond = ta.crossunder(close, ema_1) or ta.crossunder(close, ema_2) or ta.crossunder(close, ema_3)

// 在K线上下绘制买卖信号标签
plotshape(long_cond, title="买入信号", location=location.belowbar, color=color.green, 
          style=shape.labelup, text="买入", textcolor=color.white, size=size.small)
plotshape(short_cond, title="卖出信号", location=location.abovebar, color=color.red, 
          style=shape.labeldown, text="卖出", textcolor=color.white, size=size.small)

// 绑定预警条件(可在TradingView的预警功能中启用)
alertcondition(long_cond, title="买入预警", message="收盘价上穿任一EMA,触发买入信号")
alertcondition(short_cond, title="卖出预警", message="收盘价下穿任一EMA,触发卖出信号")

关键说明:

  • EMA计算:通过ta.ema()生成三条不同周期的均线,参数可在指标设置面板中灵活调整。
  • 信号逻辑:你写的crossover/crossunder组合逻辑准确——仅在收盘价从下往上突破某条EMA的瞬间触发买入信号,从上往下跌破某条EMA的瞬间触发卖出信号,避免持续重复提示。
  • 图表可视化:用plotshape在对应K线的下方/上方添加标签式信号,清晰直观。如果偏好图标样式,可把shape.labelup改成shape.triangleup等样式。
  • 预警设置:alertcondition会自动在TradingView的预警列表中生成对应的预警项,可设置邮件、弹窗或推送通知。

可选调整:

如果需求是收盘价持续高于任一EMA就触发买入提示(而非仅突破瞬间),可把买入条件改为:

long_cond = close > ema_1 or close > ema_2 or close > ema_3

但这种设置会让预警每根K线都触发,适合监控而非交易信号,按需选择即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 16:02:24