如何创建结合RSI交叉与MACD交叉的预警脚本?
多指标联动预警脚本实现(RSI交叉后MACD确认)
核心逻辑
- 先捕获RSI的金叉/死叉信号,记录信号发生的K线位置
- 设置时间窗口(可自定义),在窗口内等待同方向的MACD交叉信号
- 当两个条件先后满足时,触发预警
Pine Script 实现代码
//@version=5 indicator("RSI-MACD 联动预警", overlay=false) // 自定义参数 rsiLength = input.int(14, title="RSI周期") rsiOverbought = input.int(70, title="RSI超买阈值") rsiOversold = input.int(30, title="RSI超卖阈值") macdFast = input.int(12, title="MACD快线周期") macdSlow = input.int(26, title="MACD慢线周期") macdSignal = input.int(9, title="MACD信号线周期") signalWindow = input.int(5, title="RSI信号后有效窗口(K线数)") // 计算指标值 rsiValue = ta.rsi(close, rsiLength) [macdLine, signalLine, _] = ta.macd(close, macdFast, macdSlow, macdSignal) // 识别RSI交叉信号 rsiGoldenCross = ta.crossover(rsiValue, rsiOversold) // RSI上穿超卖线(金叉) rsiDeathCross = ta.crossunder(rsiValue, rsiOverbought) // RSI下穿超买线(死叉) // 持久化记录RSI信号的K线位置 var int rsiGoldenBar = na var int rsiDeathBar = na if rsiGoldenCross rsiGoldenBar = bar_index if rsiDeathCross rsiDeathBar = bar_index // 验证MACD同方向交叉是否在RSI信号窗口内 macdGoldenCross = ta.crossover(macdLine, signalLine) macdDeathCross = ta.crossunder(macdLine, signalLine) confirmGolden = macdGoldenCross and (bar_index - rsiGoldenBar) <= signalWindow confirmDeath = macdDeathCross and (bar_index - rsiDeathBar) <= signalWindow // 可视化信号+设置预警 plotshape(confirmGolden, title="金叉确认", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="多指标金叉") plotshape(confirmDeath, title="死叉确认", location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, text="多指标死叉") alertcondition(confirmGolden, title="金叉预警", message="RSI金叉后MACD金叉确认") alertcondition(confirmDeath, title="死叉预警", message="RSI死叉后MACD死叉确认")
关键细节说明
- 信号窗口:
signalWindow控制RSI交叉后多久内的MACD交叉有效,可根据交易周期调整(日线建议5-10,小时线建议10-20) - 方向一致性:严格绑定RSI与MACD的交叉方向,避免反向信号干扰
- 变量持久化:用
var关键字保存RSI信号的K线索引,确保信号不会随新K线刷新而丢失
内容的提问来源于stack exchange,提问作者CantIhavecrypto
相关产品推荐
相关产品推荐

