我想在 pinescript 中为 line.new 放置多个(3)标签,而缩小时它们不会重叠

问题描述 投票:0回答:1

不确定这是否可行,但肯定会很好。我有一个当前已发布的脚本,我正在将其改进为更好的版本。该脚本定位多种用户定义的移动平均线、用户定义的回顾、用户定义的分辨率,并使用这些移动平均线的标签绘制线条。例如,假设用户在 5 分钟图表上进行交易,并想知道 20 周期 SMA 在 15 分钟、每小时、4 小时和每日时间范围内的位置。该脚本将绘制一条从当前柱延伸的一定长度的线,并标记有回溯、MA 类型和分辨率。我遇到的问题是,因为这些都是用户选择的不同变量,所以我不能只为每一行制作一个标签(我不认为)。我必须使用三个标签(回顾、MA 类型、分辨率)。我有一种不错的方法来确定每个标签之间的距离,但如果可能的话,我想对此进行改进,因为当用户缩小标签时,标签会相互挤压。

我要做的一件事是添加一个文本大小的全局选项,以便用户可以将其变小(对视力不好的人没有帮助,但它是有用的)。但是,我想知道是否还有其他事情可以做。我真的只是希望回顾和分辨率标签与 matype 标签有固定的距离。我认为这是因为我使用的是 xloc.bar_time 而不是 bar_index,但我不确定,而且我不知道如何解决这个问题。

要完全清楚的是,我并不关心线标签集与其他线标签集重叠(如果 MA 靠得很近,实际上没有办法解决这个问题)。如果可能的话,我想改进的是单个移动平均线的三个标签被压扁。

谢谢!

代码如下。 matype 标签的 x 坐标与线的 x2 相同。回溯和解析标签的坐标横跨 matype 标签的两侧。



bar_index_duration = time - time[1]


////INPUTS FOR FIRST MOVING AVERAGE
//DISPLAY LINES AND LABELS?
onoff = input(defval=true, title='Display Line and Label?', group='Moving Average #1')


//LOOKBACK
i_ma1len = input.int(defval=9, title='Lookback', group='Moving Average #1')


//TYPE
i_ma1 = input.string(defval='EMA', options=['EMA', 'HMA', 'SMA', 'SMMA', 'VWMA', 'WMA'], title='Moving Average Type', group='Moving Average #1')
ma1 = i_ma1 == 'EMA' ? ta.ema(close, i_ma1len) : i_ma1 == 'HMA' ? ta.hma(close, i_ma1len) : i_ma1 == 'SMA' ? ta.sma(close, i_ma1len) : i_ma1 == 'SMMA' ? ta.rma(close, i_ma1len) : i_ma1 == 'VWMA' ? ta.vwma(close, i_ma1len) : i_ma1 == 'WMA' ? ta.wma(close, i_ma1len) : na

//RESOLUTION
i_ma1res = input.string(defval='1h', options=['5m', '12m', '15m', '30m', '1h', '4h', '8h', '12h', 'D', '3D', '4D', 'W', 'M'], title='Resolution', group='Moving Average #1')
ma1res = i_ma1res == '5m' ? '5' : i_ma1res == '12m' ? '12' : i_ma1res == '15m' ? '15' : i_ma1res == '30m' ? '30' : i_ma1res == '1h' ? '60' : i_ma1res == '4h' ? '240' : i_ma1res == '8h' ? '480' : i_ma1res == '12h' ? '720' : i_ma1res == 'D' ? 'D' : i_ma1res == '3D' ? '3D' : i_ma1res == '4D' ? '4D' : i_ma1res == 'W' ? 'W' : i_ma1res == 'M' ? 'M' : na 

//
line_color = input(color.purple, title='Line Color', group='Moving Average #1')
line_style = input.string(defval='Solid', options=['Dashed', 'Dotted', 'Solid'], title='Line Style', group='Moving Average #1')
line_width = input.int(defval=1, maxval=4, title="Line Width", group='Moving Average #1')
lkbk_term = input.int(defval=3, title='Moving Average Lookback Label Distance', group='Moving Average #1')
line_term = input(defval=25, title='Line Length and Moving Average Type Label', group='Moving Average #1')
res_term = input(defval=3, title='Resolution Label Distance', group='Moving Average #1')


//
_ma1_security = request.security(syminfo.tickerid, ma1res, ma1)


//LINES AND LABELS
x1 = timenow
y1 = _ma1_security
x2 = timenow + (line_term * bar_index_duration)
y2 = _ma1_security
xloc = xloc.bar_time
color = line_color
style = line_style == 'Dotted' ? line.style_dotted : line_style == 'Dashed' ? line.style_dashed : line_style == 'Solid' ? line.style_solid : na

ma1_line = onoff ? line.new(x1, y1, x2, y2, xloc, color=color, style=style, width=line_width) : na
line.delete(ma1_line[1])

ma1_lkbklabel = onoff ? label.new(x2 - (lkbk_term * bar_index_duration), y1, xloc=xloc, style=label.style_none, text=str.tostring(i_ma1len), textcolor=line_color) : na
label.delete(ma1_lkbklabel[1])

ma1_typelabel = onoff ? label.new(x2, y1, xloc=xloc, style=label.style_none, text=i_ma1, textcolor=line_color) : na
label.delete(ma1_typelabel[1])

ma1_reslabel = onoff ? label.new(x2 + (res_term * bar_index_duration), y1, xloc=xloc, style=label.style_none, text=i_ma1res, textcolor=line_color) : na
label.delete(ma1_reslabel[1])```
label pine-script line pine-script-v5 overlap
1个回答
0
投票

已为每个移动平均线使用 3 个标签,为什么不只使用一个?

text=str.tostring(i_ma1len) + " | " str.tostring(i_ma1) + " | " + str.tostring(i_ma1Res)

如果您想更改位置,请考虑使用

style=label.style_right
并将标签透明度设置为 100

编辑: 我会尽力给你一个更完整的答案。因为您已经在该价格水平上创建了 label.new。在同一值上添加更多标签会导致重叠。通过将 3 个标签减少到一个,我们就不会有这个问题,因为文本都写在同一行上。

我添加了一个新的可选绘图方法,以防您想将其显示在线的右侧,以便使值处于同一水平。

您通过在输入参数中添加空格来使用的解决方法是不好的做法。为此,我创建了一个新变量,您可以在其中根据需要格式化字符串。

//@version=5
indicator("My script", overlay = true)

bar_index_duration = time - time[1]


////INPUTS FOR FIRST MOVING AVERAGE
//DISPLAY LINES AND LABELS?
onoff = input(defval=true, title='Display Line and Label?', group='Moving Average #1')


//LOOKBACK
i_ma1len = input.int(defval=9, title='Lookback', group='Moving Average #1')


//TYPE
i_ma1 = input.string(defval='EMA', options=['EMA', 'HMA', 'SMA', 'SMMA', 'VWMA', 'WMA'], title='Moving Average Type', group='Moving Average #1')
ma1 = i_ma1 == 'EMA' ? ta.ema(close, i_ma1len) : i_ma1 == 'HMA' ? ta.hma(close, i_ma1len) : i_ma1 == 'SMA' ? ta.sma(close, i_ma1len) : i_ma1 == 'SMMA' ? ta.rma(close, i_ma1len) : i_ma1 == 'VWMA' ? ta.vwma(close, i_ma1len) : i_ma1 == 'WMA' ? ta.wma(close, i_ma1len) : na

//RESOLUTION
i_ma1res = input.string(defval='1h', options=['5m', '12m', '15m', '30m', '1h', '4h', '8h', '12h', 'D', '3D', '4D', 'W', 'M'], title='Resolution', group='Moving Average #1')
ma1res = i_ma1res == '5m' ? '5' : i_ma1res == '12m' ? '12' : i_ma1res == '15m' ? '15' : i_ma1res == '30m' ? '30' : i_ma1res == '1h' ? '60' : i_ma1res == '4h' ? '240' : i_ma1res == '8h' ? '480' : i_ma1res == '12h' ? '720' : i_ma1res == 'D' ? 'D' : i_ma1res == '3D' ? '3D' : i_ma1res == '4D' ? '4D' : i_ma1res == 'W' ? 'W' : i_ma1res == 'M' ? 'M' : na 

//
line_color = input(color.purple, title='Line Color', group='Moving Average #1')
line_style = input.string(defval='Solid', options=['Dashed', 'Dotted', 'Solid'], title='Line Style', group='Moving Average #1')
line_width = input.int(defval=1, maxval=4, title="Line Width", group='Moving Average #1')
lkbk_term = input.int(defval=3, title='Moving Average Lookback Label Distance', group='Moving Average #1')
line_term = input(defval=25, title='Line Length and Moving Average Type Label', group='Moving Average #1')
res_term = input(defval=3, title='Resolution Label Distance', group='Moving Average #1')


//
_ma1_security = request.security(syminfo.tickerid, ma1res, ma1)


//LINES AND LABELS
x1 = timenow
y1 = _ma1_security
x2 = timenow + (line_term * bar_index_duration)
y2 = _ma1_security
xloc = xloc.bar_time
color = line_color
style = line_style == 'Dotted' ? line.style_dotted : line_style == 'Dashed' ? line.style_dashed : line_style == 'Solid' ? line.style_solid : na

ma1_line = onoff ? line.new(x1, y1, x2, y2, xloc, color=color, style=style, width=line_width) : na
line.delete(ma1_line[1])

//             EDIT
// ----------------- 

// You can create a new variable and format as you prefer
string lbl_1_str = "          " + str.tostring(i_ma1len) + "      " + i_ma1 + "        " + i_ma1res

// Then paas it the new variable as parameter to "text". 
ma1_l_label = onoff ? label.new(x2 - (lkbk_term * bar_index_duration), y1, xloc=xloc, style=label.style_none, text=lbl_1_str, textcolor=line_color) : na

// OPTIONAL: you can plot on the right side of the line in this way you'll have the label on the same leve of the line i added a label with transparence to 100
///ma1_l_label = onoff ? label.new(x2 - (lkbk_term * bar_index_duration), y1, xloc=xloc, style=label.style_label_left, text=lbl_1_str, textcolor=line_color, color=color.new(#000000,100)) : na

label.delete(ma1_l_label[1])

// ----------------- 

© www.soinside.com 2019 - 2024. All rights reserved.