为什么我的 label.new 函数只能返回几个月前?

Why does my label.new function only go back a couple months?

我刚刚了解文档中的 label.new 函数。当我将示例代码应用于图表时,它只返回大约 2.5 个月:

//@version=4
study("Bar Labels", overlay=true)

label.new(bar_index, high, style=label.style_none,
      text="x=" + tostring(bar_index) + "\ny=" + tostring(high))

我试图在某些条件下覆盖它:

bi = bar_index < 7572?1:0
label.new(bi, na, style=label.style_none,
          text="x=" + tostring(bar_index) + "\ny=" + tostring(high), yloc=yloc.belowbar)

在这种情况下,如果 bar_index 低于 2.5 个月前的那个日期,则图表上不会打印任何内容。

您的第二个代码段将无法工作,因为它试图从 0 号柱开始在 bar_index==1 处打印标签,这在当时是未来.标签和线条只有在使用 xlox=xloc.bar_time 时才能在将来绘制。这将起作用:

//@version=4
study("")
if bar_index < 7572
    label.new(bar_index, na, style=label.style_none,
      text="x=" + tostring(bar_index) + "\ny=" + tostring(high), yloc=yloc.belowbar)

垃圾收集器删除旧标签,只留下最后 ~50 个;这就是为什么您只看到您打印的部分内容的原因。您将在 Pine 用户手册中找到有用的信息: https://www.tradingview.com/pine-script-docs/en/v4/essential/Drawings.html#total-number-of-drawings


显示不同类型如何转换为 bool 类型的示例代码。您可以在 if 测试中使用所有这些 cond* 变量,因为 Pine 会自动将它们转换为 bool 或因为它们已经是 bool 类型。但是,您可能不会将它们全部用作 label.new() 函数中 x= 参数的参数,因为它需要一个 int:

//@version=4
study("")
cond1Bool   = bar_index < 7572
cond2Int    = bar_index < 7572 ? 1 : 0
cond3Float  = bar_index < 7572 ? 100.99 : 0
cond4Bool   = bar_index < 7572 ? true : false
cond5Na     = bar_index < 7572 ? na : na
if cond1Bool
    label.new(bar_index, na, style=label.style_none,
      text="x=" + tostring(bar_index) + "\ny=" + tostring(high), yloc=yloc.belowbar)