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

如何为MACD交叉标记添加固定值条件并设置交易警报?

Pine Script v5 修改MACD交叉标记与添加警报实现方案

一、修改交叉标记逻辑

原来的交叉判断仅检测MACD线与Signal线的交叉,现在要新增MACD值低于2的限制条件,只需在交叉条件后用and连接macd < 2即可,确保只有双条件同时满足时才绘制圆圈标记。

二、添加双条件触发的警报

使用Pine Script v5内置的alertcondition()函数,将“交叉发生+MACD<2”的组合条件作为触发逻辑,就能在两个条件同时满足时触发警报。

完整代码

//@version=5
indicator("MACD with Cross Mark & Alert", overlay=false)

// MACD核心计算
fastLength = input.int(12, title="Fast Length")
slowLength = input.int(26, title="Slow Length")
signalLength = input.int(9, title="Signal Length")
src = input(close, title="Source")

macd = ta.ema(src, fastLength) - ta.ema(src, slowLength)
signal = ta.ema(macd, signalLength)
histogram = macd - signal

// 绘制基础MACD指标
plot(macd, color=color.blue, title="MACD")
plot(signal, color=color.red, title="Signal")
plot(histogram, color=color.green, style=plot.style_columns, title="Histogram")

// --- mark the cross 段 修改后 ---
// 定义双条件交叉判断
bullishCross = ta.crossover(macd, signal) and macd < 2
bearishCross = ta.crossunder(macd, signal) and macd < 2

// 绘制满足条件的交叉圆圈
plotshape(bullishCross, title="Bullish Cross", location=location.belowbar, color=color.green, style=shape.circle, size=size.small)
plotshape(bearishCross, title="Bearish Cross", location=location.abovebar, color=color.red, style=shape.circle, size=size.small)
// --- 修改结束 ---

// 添加双条件警报
alertcondition(bullishCross, title="Bullish Cross Alert", message="MACD上穿Signal且MACD值低于2")
alertcondition(bearishCross, title="Bearish Cross Alert", message="MACD下穿Signal且MACD值低于2")

关键说明

  1. 交叉标记修改:在ta.crossover和ta.crossunder的基础上,新增macd < 2的判断,确保只有双条件同时满足时才绘制圆圈。
  2. 警报设置:alertcondition()的触发条件与标记逻辑完全一致,实现标记出现时同步触发警报的效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 18:30:00