如何在Rate Of Change指标的Hline上添加可自定义对齐的文本?
带自定义文本标注的ROC概率区间指标
以下是修改后的完整代码,实现了每条概率水平线的自定义文本输入和左/中/右对齐功能,所有配置项均已汉化:
//@version=5 indicator(title="带自定义文本的变动率指标(ROC)", shorttitle="ROC-自定义概率线", format=format.price, precision=2, timeframe="", timeframe_gaps=true) // ROC核心计算参数 length = input.int(1, minval=1, title="计算周期") source = input(close, title="数据源") roc = 100 * (source - source[length])/source[length] plot(roc, color=#2962FF, title="ROC曲线") // -------------------------- 正区间概率线配置 -------------------------- // 50%概率线(+25%) text_pos_50 = input.string("50% (+25%)", title="正50%区间文本", inline="pos50") align_pos_50 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="pos50") hline(0.5, color=#edde35, title="正50%概率线") var label lbl_pos_50 = label.new(chart.right, 0.5, text_pos_50, color=color.new(color.white, 100), textcolor=#edde35, style=label.style_none) label.set_text(lbl_pos_50, text_pos_50) label.set_y(lbl_pos_50, 0.5) label.set_x(lbl_pos_50, chart.left if align_pos_50 == "左" else chart.middle if align_pos_50 == "中" else chart.right) // 75%概率线(+38%) text_pos_75 = input.string("75% (+38%)", title="正75%区间文本", inline="pos75") align_pos_75 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="pos75") hline(1.0, color=#2950de, title="正75%概率线") var label lbl_pos_75 = label.new(chart.right, 1.0, text_pos_75, color=color.new(color.white, 100), textcolor=#2950de, style=label.style_none) label.set_text(lbl_pos_75, text_pos_75) label.set_y(lbl_pos_75, 1.0) label.set_x(lbl_pos_75, chart.left if align_pos_75 == "左" else chart.middle if align_pos_75 == "中" else chart.right) // 95%概率线(+48%) text_pos_95 = input.string("95% (+48%)", title="正95%区间文本", inline="pos95") align_pos_95 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="pos95") hline(2.5, color=#787B86, title="正95%概率线") var label lbl_pos_95 = label.new(chart.right, 2.5, text_pos_95, color=color.new(color.white, 100), textcolor=#787B86, style=label.style_none) label.set_text(lbl_pos_95, text_pos_95) label.set_y(lbl_pos_95, 2.5) label.set_x(lbl_pos_95, chart.left if align_pos_95 == "左" else chart.middle if align_pos_95 == "中" else chart.right) // 98%概率线(+49%) text_pos_98 = input.string("98% (+49%)", title="正98%区间文本", inline="pos98") align_pos_98 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="pos98") hline(3.5, color=#c73030, title="正98%概率线") var label lbl_pos_98 = label.new(chart.right, 3.5, text_pos_98, color=color.new(color.white, 100), textcolor=#c73030, style=label.style_none) label.set_text(lbl_pos_98, text_pos_98) label.set_y(lbl_pos_98, 3.5) label.set_x(lbl_pos_98, chart.left if align_pos_98 == "左" else chart.middle if align_pos_98 == "中" else chart.right) // -------------------------- 负区间概率线配置 -------------------------- // 50%概率线(-25%) text_neg_50 = input.string("50% (-25%)", title="负50%区间文本", inline="neg50") align_neg_50 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="neg50") hline(-0.5, color=#edde35, title="负50%概率线") var label lbl_neg_50 = label.new(chart.right, -0.5, text_neg_50, color=color.new(color.white, 100), textcolor=#edde35, style=label.style_none) label.set_text(lbl_neg_50, text_neg_50) label.set_y(lbl_neg_50, -0.5) label.set_x(lbl_neg_50, chart.left if align_neg_50 == "左" else chart.middle if align_neg_50 == "中" else chart.right) // 75%概率线(-36%) text_neg_75 = input.string("75% (-36%)", title="负75%区间文本", inline="neg75") align_neg_75 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="neg75") hline(-1.0, color=#2950de, title="负75%概率线") var label lbl_neg_75 = label.new(chart.right, -1.0, text_neg_75, color=color.new(color.white, 100), textcolor=#2950de, style=label.style_none) label.set_text(lbl_neg_75, text_neg_75) label.set_y(lbl_neg_75, -1.0) label.set_x(lbl_neg_75, chart.left if align_neg_75 == "左" else chart.middle if align_neg_75 == "中" else chart.right) // 95%概率线(-48%) text_neg_95 = input.string("95% (-48%)", title="负95%区间文本", inline="neg95") align_neg_95 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="neg95") hline(-2.5, color=#787B86, title="负95%概率线") var label lbl_neg_95 = label.new(chart.right, -2.5, text_neg_95, color=color.new(color.white, 100), textcolor=#787B86, style=label.style_none) label.set_text(lbl_neg_95, text_neg_95) label.set_y(lbl_neg_95, -2.5) label.set_x(lbl_neg_95, chart.left if align_neg_95 == "左" else chart.middle if align_neg_95 == "中" else chart.right) // 98%概率线(-49%) text_neg_98 = input.string("98% (-49%)", title="负98%区间文本", inline="neg98") align_neg_98 = input.string("右", title="对齐方式", options=["左", "中", "右"], inline="neg98") hline(-3.5, color=#c73030, title="负98%概率线") var label lbl_neg_98 = label.new(chart.right, -3.5, text_neg_98, color=color.new(color.white, 100), textcolor=#c73030, style=label.style_none) label.set_text(lbl_neg_98, text_neg_98) label.set_y(lbl_neg_98, -3.5) label.set_x(lbl_neg_98, chart.left if align_neg_98 == "左" else chart.middle if align_neg_98 == "中" else chart.right)
功能说明
- 每条概率水平线对应独立的文本输入框,可自由修改显示内容
- 支持为每条线选择左/中/右三种对齐方式,文本自动定位到对应位置
- 文本颜色与对应水平线保持一致,视觉风格统一
- 使用
var label确保标签仅初始化一次,避免重复绘制导致的性能问题
内容的提问来源于stack exchange,提问作者lvbx9
相关产品推荐
相关产品推荐

