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

Pine脚本求助:EMA指标首次升降时绘制图形遇问题

Pine脚本:RSI-EMA拐点信号绘制问题

需求说明

作为Pine脚本新手,希望实现以下功能:

  • 当RSI的EMA指标首次上升(即EMA出现低位拐点,满足ema_rsi[2] > ema_rsi[1] < ema_rsi)时,在K线下方绘制绿色向上三角形
  • 当RSI的EMA指标首次下降(即EMA出现高位拐点,满足ema_rsi[2] < ema_rsi[1] > ema_rsi)时,在K线上方绘制红色向下三角形

原代码片段

RSI及EMA计算代码

// RSI code
rsi = ta.rsi(close, len)
plot(rsi, color=color.new(color.white, 0), linewidth=3)
plot(rsi)

lenema = input.int(200, minval=1, title='RSI EMA Length')
out = ta.ema(rsi, lenema)
col = out >= out[1] ? color.lime : color.red
plot(out, title='RSI EMA', color=col, linewidth=2)

绘图信号代码

buySignal = input.bool(true,title='Buy Signal')
sellSignal = input.bool(true,title='Sell Signal')

plotshape(buySignal ? lenema > lenema[1] and lenema[1] < lenema[2]: na,title='Buy Shape', style=shape.triangleup,color=color.green, location=location.belowbar,size=size.large)

plotshape(sellSignal ? lenema < lenema[1] and lenema[1] > lenema[2]: na,title='Sell Shape',style=shape.triangledown,color=color.red, location=location.abovebar,size=size.large)

问题修正与完整代码

原代码存在变量混淆、逻辑重复触发、参数未定义三个核心问题,修正后的完整代码如下:

//@version=5
indicator("RSI-EMA拐点信号", overlay=true)

// 输入参数
len_rsi = input.int(14, minval=1, title='RSI周期')
len_ema = input.int(200, minval=1, title='RSI EMA周期')
buySignal = input.bool(true, title='启用买入信号')
sellSignal = input.bool(true, title='启用卖出信号')

// RSI及EMA计算
rsi = ta.rsi(close, len_rsi)
ema_rsi = ta.ema(rsi, len_ema)

// 绘制RSI和EMA线
plot(rsi, color=color.white, linewidth=3, title='RSI')
plot(ema_rsi, color=ema_rsi >= ema_rsi[1] ? color.lime : color.red, linewidth=2, title='RSI EMA')

// 拐点信号判断(仅首次触发)
buy_condition = ema_rsi[2] > ema_rsi[1] and ema_rsi[1] < ema_rsi
buy_trigger = buy_condition and not buy_condition[1]

sell_condition = ema_rsi[2] < ema_rsi[1] and ema_rsi[1] > ema_rsi
sell_trigger = sell_condition and not sell_condition[1]

// 绘制信号形状
plotshape(buySignal ? buy_trigger : na, title='买入信号', style=shape.triangleup, color=color.green, location=location.belowbar, size=size.large)
plotshape(sellSignal ? sell_trigger : na, title='卖出信号', style=shape.triangledown, color=color.red, location=location.abovebar, size=size.large)

修正点说明

  1. 变量命名规范:将周期参数lenema改为len_ema,EMA指标值命名为ema_rsi,避免参数与指标值混淆
  2. 修复逻辑重复:通过buy_trigger = buy_condition and not buy_condition[1]确保仅在拐点出现的第一根K线绘制信号,避免连续触发
  3. 补全缺失参数:添加RSI周期参数len_rsi,解决原代码中len未定义的报错问题
  4. 优化显示逻辑:添加overlay=true让信号直接绘制在K线图上,直观对应价格走势

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 09:55:33