PineScript填充问题:填充角度丑陋、颜色位置错误
修复Stochastic RSI的填充效果问题
你希望实现K/D线与上下轨间的针对性整洁填充,当前代码的问题在于错误填充了K、D线之间的全区域以及整个80-20区间背景,导致界面混乱。以下是修改后的代码:
indicator(title="Stochastic RSI", shorttitle="Stoch RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true) smoothK = input.int(3, "K", minval=1) smoothD = input.int(3, "D", minval=1) lengthRSI = input.int(14, "RSI Length", minval=1) lengthStoch = input.int(14, "Stochastic Length", minval=1) src = input(close, title="RSI Source") transpSwitch = input.int(40, title="Band Transparency", step=5) rsi1 = ta.rsi(src, lengthRSI) k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) d = ta.sma(k, smoothD) a = plot(k, "K", color=#2962FF) b = plot(d, "D", color=#FF6D00) h0 = hline(80, "Upper Band", color=#787B86) hline(50, "Middle Band", color=color.new(#787B86, 50)) h1 = hline(20, "Lower Band", color=#787B86) // 定义填充颜色 bullishColor = color.new(color.green, transpSwitch) bearishColor = color.new(color.red, transpSwitch) // 多头趋势:填充K/D线与上轨80之间的区域 fill(a, h0, color=k > d ? bullishColor : na) fill(b, h0, color=k > d ? bullishColor : na) // 空头趋势:填充K/D线与下轨20之间的区域 fill(a, h1, color=k < d ? bearishColor : na) fill(b, h1, color=k < d ? bearishColor : na)
核心改动说明:
- 移除原代码中覆盖整个80-20区间的背景填充,避免界面杂乱
- 新增条件化填充逻辑:仅在K线位于D线上方时,填充两条线与上轨的绿色区域;仅在K线位于D线下方时,填充两条线与下轨的红色区域
- 使用
na值控制填充显示,非目标区间自动隐藏填充,保证中间区域整洁
内容的提问来源于stack exchange,提问作者CrazyChicken
相关产品推荐
相关产品推荐

