pine-script 中 console.log 的等价物是什么?

What is the equivalent of console.log in pine-script?

console.log variablespine-script 中函数的结果如何? 我想将 pine-script 脚本转换为 javascript,并且我想验证我正在转换的脚本是否与原始脚本相同。 如果此功能不存在,欢迎使用任何变通方法。 谢谢!

This answer is now outdated. Please consult other more recent answers.


无法将pine-script中的文本或变量打印到任何形式的“控制台”。但是你可以使用plot来打印,上面或下面的非常短的文本每个滴答声。但是,文本是静态的,您不能使用变量来更改它。

您还可以使用各种技巧在图表的非常有限的指标字段(左上角)中显示值 window。或者将图表中的线条和图表移出可见区域,但仍能看到轴突出显示。

请查看他们的 Wiki 并查阅 1000 多个其他用户的脚本以了解有关如何操作的详细信息。

pine v.4 提供了打印文本的新方法。您可以为此使用标签:

//@version=4
study("Print text values", overlay=true)
x = bar_index
y = close
txt = tostring(close)
label.new(x, y, txt) // print value of close

NOTES

  1. While this answer's content is not deprecated, the Pine User Manual now contains a page on debugging techniques explaining much of this answer's content, and more.
  2. RicardoSantos has a very nice DebugConsole library which v5 users can access.

对于 Pine 开发人员来说,实际上有一个类似于控制台的东西;这是 数据 Window。我们经常用它来调试。想法是这样使用plotchar()

plotchar(bar_index, "Bar Index", "", location = location.top)

这不会破坏指标的刻度,也不会在其中打印任何内容,但它会在数据 Window 中显示一个值,如 PineCoders FAQ's section on debugging 的第二个问题中所述.当您将鼠标移到图表栏上时,variable/expression 的相应值将显示在数据 Window 中。常见问题解答解释了可用于在图表上进行调试的其他有用技术,因为这有时效率更高。


我们使用 AutoHotkey 宏从先前复制到剪贴板的变量或表达式创建所需的 plotchar() 语句。这是 AHK 宏:

^+C:: SendInput plotchar(^v, "^v", "", location.top){Return}

数据 Window 也是一个很好的选择,作为需要显示许多值的脚本的显示面板,例如我们的 Backtesting & Trading Engine,它广泛使用它:

如果您只想打印单个值(不是在每个柱上),您可以这样做:

if (barstate.islast)
    label.new(bar_index, 0, "Your value here, ex: " + syminfo.tickerid)

我所做的是,我使用table来显示我想要显示的值。这在重播栏中就像魔术一样。这是一个例子:

//@version=4
study("My Script", overlay=true)

sma20 = sma(close, 20)
text = "sma 20: " + tostring(sma20)

tableColumn = 1
tableRow = 1
var table panel = table.new(position.top_right, tableColumn, tableRow)
if barstate.islast
    table.cell(panel, 0, 0, text, bgcolor=color.black, text_color=color.white)

结果如下:

正如 not2qubit 所提到的,技术上没有办法将内容打印到 TradingView 左右的控制台。

但是我们可以创建标签来“打印”东西,这就是我编写这个小函数的原因。

它将“打印”您在最新 bar_index 上输入的文本。 如果您打印多件东西,标签将堆叠在一起。

仅在 PineScript 版本 5 上测试

var global_print_counter = array.new_int()
array.push(global_print_counter, 0)
print(string txt = "") => 
    if txt != "" and barstate.islast
        int print_counter = array.get(global_print_counter, 0)
        printLabel = label.new(x=bar_index, y=high + (print_counter * 75), textcolor=color.white, color=color.black, text=txt)
        array.set(global_print_counter, 0, print_counter + 1)

示例:

print("Hello World!")
print("Hello World, again!")