如何实现Pine Script中两个plotshape均显示在K线底部?
问题:如何让Pine Script中两个plotshape都显示在K线底部且不重叠?
我编写了名为SSL Hybrid 2X的Pine Script指标,希望通过两个不同的plotshape绘制两条独立线条,但目前仅能让一个显示在K线顶部、另一个在底部;若将两者都设为顶部或底部,会重叠导致无法区分。请问如何让这两个plotshape都显示在K线底部?
原代码如下:
indicator('SSL Hybrid 2X', overlay=true) show_Baseline = input(title='Show Baseline', defval=true) maType = input.string(title='SSL1 / Baseline Type', defval='HMA', options=['HMA']) len = input(title='SSL1 / Baseline Length', defval=60) SSL3Type = input.string(title='EXIT Type', defval='HMA', options=['HMA']) len3 = input(title='EXIT Length', defval=15) src = input(title='Source', defval=close) ma(type, src, len) => float result = 0 if type == 'HMA' // Hull result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) result emaHigh = ma(maType, high, len) emaLow = ma(maType, low, len) BBMC = ma(maType, close, len) useTrueRange = input(true) multy = input.float(0.2, step=0.05, title='Base Channel Multiplier') Keltma = ma(maType, src, len) range_1 = useTrueRange ? ta.tr : high - low rangema = ta.ema(range_1, len) upperk = Keltma + rangema * multy lowerk = Keltma - rangema * multy open_pos = open * 1 close_pos = close * 1 difference = math.abs(close_pos - open_pos) Hlv = int(na) Hlv := close > emaHigh ? 1 : close < emaLow ? -1 : Hlv[1] sslDown = Hlv < 0 ? emaHigh : emaLow color_bar = close > upperk ? #00c3ff : close < lowerk ? #ff0062 : color.gray color_ssl1 = close > sslDown ? #00c3ff : close < sslDown ? #ff0062 : na plotshape(show_Baseline ? BBMC : na, style=shape.square, color=color_bar, title='MA Baseline') show_Baseline_2 = input(title='Show Baseline 2', defval=true) maType_2 = input.string(title='SSL1 2 / Baseline Type 2', defval='HMA', options=['HMA']) len_2 = input(title='SSL1 2 / Baseline Length 2', defval=9) SSL3Type_2 = input.string(title='EXIT Type 2', defval='HMA', options=['HMA']) len3_2 = input(title='EXIT Length 2', defval=15) src_2 = input(title='Source 2', defval=close) ma_2(type, src_2, len_2) => float result = 0 if type == 'HMA' // Hull result := ta.wma(2 * ta.wma(src_2, len_2 / 2) - ta.wma(src_2, len_2), math.round(math.sqrt(len_2))) result emaHigh_2 = ma(maType_2, high, len_2) emaLow_2 = ma(maType_2, low, len_2) BBMC_2 = ma(maType_2, close, len_2) useTrueRange_2 = input(true) multy_2 = input.float(0.2, step=0.05, title='Base Channel Multiplier 2') Keltma_2 = ma(maType_2, src_2, len_2) range_1_2 = useTrueRange_2 ? ta.tr : high- low rangema_2 = ta.ema(range_1_2, len_2) upperk_2 = Keltma_2 + rangema_2 * multy_2 lowerk_2 = Keltma_2 - rangema_2 * multy_2 open_pos_2 = open * 1 close_pos_2 = close * 1 difference_2 = math.abs(close_pos_2 - open_pos_2) Hlv_2 = int(na) Hlv_2 := close > emaHigh_2 ? 1 : close < emaLow_2 ? -1 : Hlv_2[1] sslDown_2 = Hlv_2 < 0 ? emaHigh_2 : emaLow_2 color_bar_2 = close> upperk_2 ? #00c3ff : close < lowerk_2 ? #ff0062 : color.gray color_ssl1_2 = close > sslDown_2 ? #00c3ff : close < sslDown_2 ? #ff0062 : na plotshape(show_Baseline_2 ? BBMC : na, style=shape.square, color=color_bar, title='MA Baseline')
解决方案
1. 修正第二个plotshape的数据源错误
你当前第二个plotshape调用的是BBMC(第一个基线的数据),而非BBMC_2(第二个基线的数据),这是导致两条线重叠的核心原因之一。
2. 强制固定显示位置
给两个plotshape都添加location=location.belowbar参数,指定它们都显示在K线下方。
3. 添加偏移或调整大小避免重叠
可以通过offset参数让其中一条线横向偏移,或者调整size参数区分线条大小,确保两条线可清晰区分。
修改后的完整代码
indicator('SSL Hybrid 2X', overlay=true) show_Baseline = input(title='Show Baseline', defval=true) maType = input.string(title='SSL1 / Baseline Type', defval='HMA', options=['HMA']) len = input(title='SSL1 / Baseline Length', defval=60) SSL3Type = input.string(title='EXIT Type', defval='HMA', options=['HMA']) len3 = input(title='EXIT Length', defval=15) src = input(title='Source', defval=close) ma(type, src, len) => float result = 0 if type == 'HMA' // Hull result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) result emaHigh = ma(maType, high, len) emaLow = ma(maType, low, len) BBMC = ma(maType, close, len) useTrueRange = input(true) multy = input.float(0.2, step=0.05, title='Base Channel Multiplier') Keltma = ma(maType, src, len) range_1 = useTrueRange ? ta.tr : high - low rangema = ta.ema(range_1, len) upperk = Keltma + rangema * multy lowerk = Keltma - rangema * multy open_pos = open * 1 close_pos = close * 1 difference = math.abs(close_pos - open_pos) Hlv = int(na) Hlv := close > emaHigh ? 1 : close < emaLow ? -1 : Hlv[1] sslDown = Hlv < 0 ? emaHigh : emaLow color_bar = close > upperk ? #00c3ff : close < lowerk ? #ff0062 : color.gray color_ssl1 = close > sslDown ? #00c3ff : close < sslDown ? #ff0062 : na // 第一个基线:显示在K线下方,默认大小 plotshape(show_Baseline ? BBMC : na, style=shape.square, color=color_bar, title='MA Baseline', location=location.belowbar) show_Baseline_2 = input(title='Show Baseline 2', defval=true) maType_2 = input.string(title='SSL1 2 / Baseline Type 2', defval='HMA', options=['HMA']) len_2 = input(title='SSL1 2 / Baseline Length 2', defval=9) SSL3Type_2 = input.string(title='EXIT Type 2', defval='HMA', options=['HMA']) len3_2 = input(title='EXIT Length 2', defval=15) src_2 = input(title='Source 2', defval=close) ma_2(type, src_2, len_2) => float result = 0 if type == 'HMA' // Hull result := ta.wma(2 * ta.wma(src_2, len_2 / 2) - ta.wma(src_2, len_2), math.round(math.sqrt(len_2))) result emaHigh_2 = ma(maType_2, high, len_2) emaLow_2 = ma(maType_2, low, len_2) BBMC_2 = ma(maType_2, close, len_2) useTrueRange_2 = input(true) multy_2 = input.float(0.2, step=0.05, title='Base Channel Multiplier 2') Keltma_2 = ma(maType_2, src_2, len_2) range_1_2 = useTrueRange_2 ? ta.tr : high- low rangema_2 = ta.ema(range_1_2, len_2) upperk_2 = Keltma_2 + rangema_2 * multy_2 lowerk_2 = Keltma_2 - rangema_2 * multy_2 open_pos_2 = open * 1 close_pos_2 = close * 1 difference_2 = math.abs(close_pos_2 - open_pos_2) Hlv_2 = int(na) Hlv_2 := close > emaHigh_2 ? 1 : close < emaLow_2 ? -1 : Hlv_2[1] sslDown_2 = Hlv_2 < 0 ? emaHigh_2 : emaLow_2 color_bar_2 = close> upperk_2 ? #00c3ff : close < lowerk_2 ? #ff0062 : color.gray color_ssl1_2 = close > sslDown_2 ? #00c3ff : close < sslDown_2 ? #ff0062 : na // 第二个基线:显示在K线下方,偏移1个周期+缩小尺寸,避免重叠 plotshape(show_Baseline_2 ? BBMC_2 : na, style=shape.square, color=color_bar_2, title='MA Baseline 2', location=location.belowbar, offset=1, size=size.size_tiny)
内容的提问来源于stack exchange,提问作者chprot
相关产品推荐
相关产品推荐

