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

如何在Pine Script中设置自定义单元格文本大小以放大TradingView水印?

TradingView Pine Script v5 水印文本尺寸调整方案

一、支持用户自定义选择内置文本尺寸

Pine Script v5 的table组件文本大小仅支持内置尺寸枚举值(无法直接设置自定义像素值),但可以添加输入选项让用户自行选择尺寸。修改后的代码如下:

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

// 添加文本尺寸选择输入
wm_size = input.string("huge", title = "文本尺寸", options = ["tiny", "small", "normal", "large", "huge"])
wm_col = input.color(color.new(color.gray, 75), title = "", inline = "WM")
watermark_txt = input.string("Custom Text", title = "输入水印文本")

// 将输入的字符串转换为对应的size枚举值
size_enum = switch wm_size
    "tiny" => size.tiny
    "small" => size.small
    "normal" => size.normal
    "large" => size.large
    "huge" => size.huge

var watermark = table.new("middle_center", 1, 1)

if barstate.isfirst
    table.cell_set_text(watermark, 0, 0, text = watermark_txt)
    table.cell_set_text_color(watermark, 0, 0, text_color = wm_col)
    table.cell_set_text_size(watermark, 0, 0, text_size = size_enum)

二、实现比size.huge更大的文本效果

如果内置的size.huge仍无法满足需求,可以改用label组件创建水印——label的文本在相同尺寸枚举下显示效果会更大,且支持更多样式调整:

//@version=5
indicator("Watermark (Large)", overlay = true)

wm_size = input.string("huge", title = "文本尺寸", options = ["tiny", "small", "normal", "large", "huge"])
wm_col = input.color(color.new(color.gray, 75), title = "")
watermark_txt = input.string("Custom Text", title = "输入水印文本")

size_enum = switch wm_size
    "tiny" => size.tiny
    "small" => size.small
    "normal" => size.normal
    "large" => size.large
    "huge" => size.huge

// 创建居中的label水印
var watermark_label = label.new(bar_index, na, watermark_txt, 
     xloc = xloc.bar_index, yloc = yloc.price,
     style = label.style_none, color = na, textcolor = wm_col,
     size = size_enum, text_align = text.align_center)

// 保持label始终居中
if barstate.islast
    label.set_xy(watermark_label, bar_index, ta.highest(ta.high, 50) * 1.02) // 可调整y轴位置避免遮挡K线

补充说明

  • 内置尺寸枚举的显示大小会随图表缩放比例自动适配,无法固定像素值。
  • 使用label时,可通过修改label.set_xy()的y轴参数灵活调整水印的垂直位置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 01:42:47