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

Pine Script:RSI突破上下带时填充颜色问题求助

解决RSI穿越上下带时的区域填充问题

问题分析

你需要实现RSI线上穿70超买线或下穿30超卖线时,在对应区间填充指定颜色的效果,原代码中的fill仅固定填充70-30之间的区域,未结合RSI的实际位置做条件判断,因此无法达到动态填充的目标。

修改后的代码

indicator(title="Relative Strength Index", shorttitle="RSI1", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

// RSI核心计算
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

// 定义关键绘图对象
rsiPlot = plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)

// 动态填充超买/超卖区域
fill(rsiPlot, rsiUpperBand, color=rsi > 70 ? color.new(color.red, 80) : na, title="超买区域填充")
fill(rsiPlot, rsiLowerBand, color=rsi < 30 ? color.new(color.green, 80) : na, title="超卖区域填充")

// 保留原布林带逻辑
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")

关键修改说明

  1. 状态判断:通过rsi > 70和rsi < 30精准捕获RSI的超买/超卖状态
  2. 条件填充:在fill函数的color参数中使用三元表达式,仅当RSI处于目标区间时显示填充色,否则用na隐藏填充
  3. 功能保留:完整保留原代码的RSI线、RSI均线、布林带逻辑,仅新增动态填充逻辑

内容的提问来源于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.22 07:52:54