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

TradingView自定义指标需求:为价格刻度添加带色水平位标签

Pine Script指标修改:在价格刻度添加水平位标签(区分xx000/xx500颜色)

需求说明

现有TradingView自定义Pine Script指标,可在US30、NAS100等品种图表绘制xx000、xx500类整数水平位,但未在右侧价格刻度处标注水平位价格。需要实现:

  • 在右侧价格刻度处添加对应水平位的价格标签
  • xx500结尾的水平位标签设为蓝色
  • xx000结尾的水平位标签设为粉色

现有指标代码

//@version=5
indicator('LVL', overlay=true)

var levels = input(5, title='Number of levels')
var lineColor = input(color.rgb(255, 00, 255), 'Line color')
var lineWidth = input(1, title='Line width')


var spacing = if syminfo.ticker == 'XAUUSD'
    25000
else if syminfo.ticker == 'XAGUSD'
    250000
else if syminfo.ticker == 'SPX500USD'
    500
else if syminfo.ticker == 'NAS100USD'
    5000
else if syminfo.ticker == 'US30USD'
    5000
else if syminfo.ticker == 'US30'
    500
else if syminfo.ticker == 'BTCUSDT'
    50000
else if syminfo.ticker == 'ETHUSDT'
    25000
else if syminfo.type == 'crypto'
    500
else if syminfo.type == 'forex'
    500
else if syminfo.ticker == 'USOIL'
    1000
else
    2500



var step = syminfo.mintick * spacing
if barstate.islast
    //label.new(bar_index, low, text=syminfo.type, yloc=yloc.abovebar)
    for counter = 0 to levels - 1 by 1
        price = if syminfo.type == 'index'
            math.ceil(close / 4) * 4
        else
            close

        stepUp = math.ceil(price / step) * step + counter * step
        stepDown = math.floor(price / step) * step - counter * step
        line.new(bar_index, stepUp, bar_index - 1, stepUp, xloc=xloc.bar_index, extend=extend.both, color=lineColor, width=lineWidth, style=line.style_solid)
        line.new(bar_index, stepDown, bar_index - 1, stepDown, xloc=xloc.bar_index, extend=extend.both, color=lineColor, width=lineWidth, style=line.style_solid)

label.new(chart.right_visible_bar_time, levels, str.tostring(levels), xloc=xloc.bar_time)
label.new(chart.right_visible_bar_time, levels, str.tostring(levels), xloc=xloc.bar_time)

尝试过的无效代码

label.new(chart.right_visible_bar_time, levels, str.tostring(levels), xloc=xloc.bar_time)
label.new(chart.right_visible_bar_time, levels, str.tostring(levels), xloc=xloc.bar_time)

修改后的完整代码

//@version=5
indicator('LVL', overlay=true)

var levels = input(5, title='Number of levels')
var lineColor = input(color.rgb(255, 00, 255), 'Line color')
var lineWidth = input(1, title='Line width')
// 新增标签颜色配置
var color_xx000 = input(color.new(color.pink, 0), title="xx000标签颜色")
var color_xx500 = input(color.new(color.blue, 0), title="xx500标签颜色")


var spacing = if syminfo.ticker == 'XAUUSD'
    25000
else if syminfo.ticker == 'XAGUSD'
    250000
else if syminfo.ticker == 'SPX500USD'
    500
else if syminfo.ticker == 'NAS100USD'
    5000
else if syminfo.ticker == 'US30USD'
    5000
else if syminfo.ticker == 'US30'
    500
else if syminfo.ticker == 'BTCUSDT'
    50000
else if syminfo.ticker == 'ETHUSDT'
    25000
else if syminfo.type == 'crypto'
    500
else if syminfo.type == 'forex'
    500
else if syminfo.ticker == 'USOIL'
    1000
else
    2500



var step = syminfo.mintick * spacing
// 计算用于判断结尾的基准值(step的2倍,对应xx000和xx500的间隔)
var step_double = step * 2

if barstate.islast
    for counter = 0 to levels - 1 by 1
        price = if syminfo.type == 'index'
            math.ceil(close / 4) * 4
        else
            close

        stepUp = math.ceil(price / step) * step + counter * step
        stepDown = math.floor(price / step) * step - counter * step
        
        // 绘制水平位线
        line.new(bar_index, stepUp, bar_index - 1, stepUp, xloc=xloc.bar_index, extend=extend.both, color=lineColor, width=lineWidth, style=line.style_solid)
        line.new(bar_index, stepDown, bar_index - 1, stepDown, xloc=xloc.bar_index, extend=extend.both, color=lineColor, width=lineWidth, style=line.style_solid)
        
        // 处理stepUp的标签
        mod_up = stepUp % step_double
        label_color_up = mod_up == 0 ? color_xx000 : color_xx500
        label.new(chart.right, stepUp, str.tostring(stepUp, "#.####"), 
                  xloc=xloc.bar_index, yloc=yloc.price, 
                  color=color.new(color.white, 100), textcolor=label_color_up,
                  style=label.style_label_left, size=size.small)
        
        // 处理stepDown的标签
        mod_down = stepDown % step_double
        label_color_down = mod_down == 0 ? color_xx000 : color_xx500
        label.new(chart.right, stepDown, str.tostring(stepDown, "#.####"), 
                  xloc=xloc.bar_index, yloc=yloc.price, 
                  color=color.new(color.white, 100), textcolor=label_color_down,
                  style=label.style_label_left, size=size.small)

关键修改说明

  1. 新增标签颜色配置:添加可自定义的xx000和xx500标签颜色参数,方便调整
  2. 循环内绑定标签绘制:在绘制每条水平位线的循环中同步创建对应价格标签,确保每个水平位都有标注
  3. 价格结尾判断逻辑:通过step_double = step * 2作为基准,用取模运算区分xx000(模为0)和xx500(模为step)结尾的价格
  4. 标签位置优化:使用chart.right固定标签在右侧价格刻度区域,设置透明背景+左侧标签样式,避免遮挡图表
  5. 清理无效代码:删除原代码中无效的测试标签语句

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:10:16