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

求助:实现Pine Script双买卖预警条件的可选触发功能

Pine Script 实现可切换的买卖预警条件

你可以通过input.options()创建下拉选择器,然后根据选中的选项动态切换触发条件。下面是完整的实现代码,包含两种条件的切换逻辑以及对应的买卖预警设置:

//@version=5
indicator("可切换的RSI+趋势预警", overlay=true)

// 1. 创建条件选择器
conditionChoice = input.options(title="选择预警条件", options=["RSI穿越50 + +DI上穿-DI", "RSI穿越50 + ADX>25"], defval="RSI穿越50 + +DI上穿-DI")

// 2. 计算所需指标
rsiValue = ta.rsi(close, 14)
[diPlus, diMinus, adxValue] = ta.dmi(14, 14)

// 3. 定义两个条件的买卖信号
// 条件1:RSI穿越50 + +DI上穿-DI
longCond1 = ta.crossover(rsiValue, 50) and ta.crossover(diPlus, diMinus)
shortCond1 = ta.crossunder(rsiValue, 50) and ta.crossunder(diPlus, diMinus)

// 条件2:RSI穿越50 + ADX>25
longCond2 = ta.crossover(rsiValue, 50) and adxValue > 25
shortCond2 = ta.crossunder(rsiValue, 50) and adxValue > 25

// 4. 根据选择的条件切换最终信号
longSignal = conditionChoice == "RSI穿越50 + +DI上穿-DI" ? longCond1 : longCond2
shortSignal = conditionChoice == "RSI穿越50 + +DI上穿-DI" ? shortCond1 : shortCond2

// 5. 绘制信号标记
plotshape(longSignal, title="多单信号", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(shortSignal, title="空单信号", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

// 6. 设置预警条件
alertcondition(longSignal, title="多单预警", message="触发多单条件:" + conditionChoice)
alertcondition(shortSignal, title="空单预警", message="触发空单条件:" + conditionChoice)

关键逻辑说明

  • 选择器实现:用input.options()生成直观的下拉菜单,支持用户快速切换目标条件,defval可设置默认选中的条件。
  • 信号切换:通过三元运算符? :根据选择结果,将对应条件的信号赋值给最终的longSignal和shortSignal,确保只有选中的条件会触发后续的标记和预警。
  • 预警绑定:alertcondition直接关联最终的信号变量,切换选择器后,预警会自动跟随选中的条件生效。

常见问题排查

如果之前的实现未达预期,大概率是以下原因:

  1. 未将最终信号与选择器关联,比如直接用单个条件绑定预警,没有根据选择结果切换逻辑。
  2. 条件判断逻辑错误,比如把ADX的判断写成ta.crossover(adxValue,25)而非adxValue>25,或是DI的交叉方向搞反。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 23:27:11