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

如何缩短TradingView中Pine Script v5均线指标的图表显示文本?

自定义TradingView均线指标显示文本为紧凑格式

要将均线指标的显示文本改为类似MA EMA10,21,50,200 177979.52 18011.72 18001.40 17678.94的紧凑格式,可通过以下修改后的Pine Script v5代码实现:

//@version=5
indicator("EMA", shorttitle="MA", overlay=true, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na

// 均线1配置
show_ma1   = input(true, "MA №1", inline="MA #1")
ma1_type   = input.string("EMA", ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma1_source = input(close, "", inline="MA #1")
ma1_length = input.int(10, "", inline="MA #1", minval=1)
ma1_color  = input(#B2B5BE, "", inline="MA #1")  // 银灰色
ma1 = ma(ma1_source, ma1_length, ma1_type)
plot(show_ma1 ? ma1 : na, color = ma1_color, title="MA №1", display=display.data_window)

// 均线2配置
show_ma2   = input(true, "MA №2", inline="MA #2")
ma2_type   = input.string("EMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma2_source = input(close, ""     , inline="MA #2")
ma2_length = input.int(21, ""     , inline="MA #2", minval=1)
ma2_color  = input(#FFEB3B, ""     , inline="MA #2")  // 黄色
ma2 = ma(ma2_source, ma2_length, ma2_type)
plot(show_ma2 ? ma2 : na, color = ma2_color, title="MA №2", display=display.data_window)

// 均线3配置
show_ma3   = input(true, "MA №3", inline="MA #3")
ma3_type   = input.string("EMA"  , ""     , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma3_source = input(close, ""     , inline="MA #3")
ma3_length = input.int(50, ""     , inline="MA #3", minval=1)
ma3_color  = input(#FF7F00, ""     , inline="MA #3")  // 橙色
ma3 = ma(ma3_source, ma3_length, ma3_type)
plot(show_ma3 ? ma3 : na, color = ma3_color, title="MA №3", display=display.data_window)

// 均线4配置
show_ma4   = input(true, "MA №4", inline="MA #4")
ma4_type   = input.string("EMA"  , ""     , inline="MA #4", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma4_source = input(close, ""     , inline="MA #4")
ma4_length = input.int(200, ""     , inline="MA #4", minval=1)
ma4_color  = input(#00FF00, ""     , inline="MA #4")  //  lime绿色
ma4 = ma(ma4_source, ma4_length, ma4_type)
plot(show_ma4 ? ma4 : na, color = ma4_color, title="MA №4", display=display.data_window)

// 构建紧凑显示文本
var string type_prefix = ""
var array<int> lengths = array.new_int()
var array<float> values = array.new_float()

// 清空数组并重新收集启用的均线信息
array.clear(lengths)
array.clear(values)
type_prefix = ""

if show_ma1
    type_prefix := ma1_type == "SMMA (RMA)" ? "RMA" : ma1_type
    array.push(lengths, ma1_length)
    array.push(values, ma1)
if show_ma2
    if type_prefix == ""
        type_prefix := ma2_type == "SMMA (RMA)" ? "RMA" : ma2_type
    array.push(lengths, ma2_length)
    array.push(values, ma2)
if show_ma3
    if type_prefix == ""
        type_prefix := ma3_type == "SMMA (RMA)" ? "RMA" : ma3_type
    array.push(lengths, ma3_length)
    array.push(values, ma3)
if show_ma4
    if type_prefix == ""
        type_prefix := ma4_type == "SMMA (RMA)" ? "RMA" : ma4_type
    array.push(lengths, ma4_length)
    array.push(values, ma4)

// 拼接长度字符串
length_str = array.join(array.map(lengths, x => str.tostring(x)), ",")
// 拼接数值字符串,保留两位小数
value_str = array.join(array.map(values, x => str.tostring(x, "#.##")), " ")
// 最终显示文本
display_text = str.format("MA {0}{1} {2}", type_prefix, length_str, value_str)

// 创建并更新左上角标签
var label info_label = label.new(bar_index, high, display_text, color=color.gray, textcolor=color.white, style=label.style_label_left, size=size.small)
label.set_xy(info_label, bar_index, high)
label.set_text(info_label, display_text)

关键修改说明:

  • 隐藏默认显示:给每个plot添加display=display.data_window参数,关闭左上角单个均线的默认信息展示
  • 信息收集:遍历所有启用的均线,收集类型(将SMMA (RMA)简化为RMA)、长度和当前值
  • 文本拼接:将收集到的信息拼接成目标格式的紧凑字符串
  • 动态标签:使用var label创建固定在左上角的标签,每次K线更新时同步更新标签文本

内容的提问来源于stack exchange,提问作者user3848207

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:25:33