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

如何在Pine Script中预设RSI数据源为RSI-based MA?

解决方案

要将rsiSourceInput的默认数据源从close改为“RSI-based MA”,需按以下逻辑调整代码:

  1. 预先生成RSI-based MA序列:基于代码中已有的第一个RSI计算结果生成均线,作为默认数据源的备选
  2. 调整变量定义顺序:确保RSI-based MA在input.source()之前定义,避免未引用错误
  3. 修改input.source的默认值:将defval从close替换为预先生成的RSI-based MA

修改后的完整代码

//input
len = input.int(title='RSI len', defval=14, inline='1')
src = input.source(title='Source', defval=close, inline='1')
upli = input.int(title='Up line limit', defval=70, inline='2')
lowli = input.int(title='Down line limit', defval=30, inline='2')
theme = input.string(title='Mode', defval='Dark', options=['Dark', 'Light'])

//rsi
rsilevel = ta.rsi(src, len)

// 预先生成RSI-based MA(这里用SMA,可按需调整均线类型)
rsi_based_ma = ta.sma(rsilevel, len)

//calculations RSI lines
ob = 100
upper = ob - lowli
lower = ob - upli
center = ob - 50
zero = 0

//mode settings
color colorred = na
color colorgreen = na
color coloryellow = na
color colorwhite = na
color coloraqua = na

modeDark = if theme == 'Dark'
    colorred := color.red
    colorgreen := color.green
    coloryellow := color.yellow
    colorwhite := color.white
    coloraqua := color.aqua
    coloraqua
modeLight = if theme == 'Light'
    colorred := color.red
    colorgreen := color.green
    coloryellow := color.blue
    colorwhite := color.green
    coloraqua := color.yellow
    coloraqua

//display RSI lines (center, overbought, oversold)
upline = plot(upper, color=colorred, title='Up line', linewidth=2)
dnline = plot(lower, color=colorgreen, title='Down line', linewidth=2)
ctr = plot(center, color=colorwhite, title='Center line', transp=50)

//display rsi
rsiover = plot(rsilevel, color=color.rgb(148, 41, 148), title='RSI', transp=50)
// 把RSI-based MA绘制出来,确保它出现在input.source的下拉选项中
plot(rsi_based_ma, color=color.orange, title='RSI-based MA', transp=50)

//calculations RSI overbought and oversold lines for filling
lineupper = rsilevel > upper ? upper : rsilevel
linelower = rsilevel < lower ? lower : rsilevel
lu = plot(lineupper, color=colorred, title='Overbought', transp=100)
ll = plot(linelower, color=colorgreen, title='Oversold', transp=100)

//filling lines
fill(ll, rsiover, color=colorgreen, title='Fill background oversold zone', transp=20)
fill(lu, rsiover, color=colorred, title='Fill background overbought zone', transp=20)

fill(ctr, upline, color=colorred, title='Fill background from center line to Up line', transp=100)
fill(ctr, dnline, color=colorgreen, title='Fill background from center line to Down line', transp=100)


ma1(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands1" => ta.sma(source, length)
        "EMA1" => ta.ema(source, length)
        "SMMA (RMA)1" => ta.rma(source, length)
        "WMA1" => ta.wma(source, length)
        "VWMA1" => ta.vwma(source, length)

rsiLengthInput = input.int(100, minval=1, title="RSI Length", group="RSI Settings")
// 将默认数据源改为预先生成的rsi_based_ma
rsiSourceInput = input.source(rsi_based_ma, "Source", group="RSI Settings")
maTypeInput1 = input.string("SMA1", title="MA Type", options=["SMA1", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput1 = input.int(14, title="MA Length", group="MA Settings")
bbMultInput1 = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up1 = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down1 = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi1 = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - (100 / (1 + up1 / down1))
rsiMA1 = ma1(rsi1, maLengthInput1, maTypeInput1)
isBB1 = maTypeInput1 == "Bollinger Bands"

plot(rsi1, "RSI 2", color.rgb(199, 226, 47))

rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand1 = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand1, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand1 = plot(isBB1 ? rsiMA1 + ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand1 = plot(isBB1 ? rsiMA1 - ta.stdev(rsi1, maLengthInput1) * bbMultInput1 : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand1, bbLowerBand1, color= isBB1 ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")



if ta.crossover(upli,70)
    label.new(bar_index, 75, text = 'Sell', color= color.rgb(201, 60, 55), textcolor = color.rgb(252, 249, 249), style=label.style_label_down)
if ta.crossover(30, len)
    label.new(bar_index, 20, text = 'Buy', color= color.rgb(40, 180, 45), textcolor = #ffffff, style=label.style_label_up)

关键修改说明

  • 在第一个RSI计算后新增rsi_based_ma = ta.sma(rsilevel, len),生成基于RSI的均线序列
  • 将rsiSourceInput = input.source(close, ...)改为rsiSourceInput = input.source(rsi_based_ma, ...),设置默认数据源为RSI-based MA
  • 新增plot(rsi_based_ma, ...),确保该序列出现在input.source的下拉选项中,方便用户手动切换数据源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 11:47:06