简体   繁体   English

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

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

How does one console.log variables , or the results of functions in pine-script ?一个 console.log variablespine-script函数的结果如何? I'd like to convert a pine-script script into javascript , and I'd like to verify that the script I'm converting is the same as the original.我想将pine-script脚本转换为javascript ,并且我想验证我正在转换的脚本是否与原始脚本相同。 Any work arounds are welcome if this functionality does not exist.如果此功能不存在,欢迎使用任何解决方法。 Thanks!谢谢!

NOTE: 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.注意:虽然这个答案的内容没有被弃用,但 Pine 用户手册现在包含一个关于调试技术页面,解释了这个答案的大部分内容等等。


There is actually something similar to a console for Pine developers;实际上有一些类似于 Pine 开发人员的控制台; it's the Data Window .它是数据窗口 We use it constantly to debug.我们经常使用它来调试。 The idea is to use plotchar() in this way:这个想法是以这种方式使用plotchar()

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

在此处输入图片说明

This will not disrupt the indicator's scale and won't print anything in it, but it will show a value in the Data Window, as is explained in the second question of the PineCoders FAQ's section on debugging .这不会破坏指标的刻度,也不会在其中打印任何内容,但会在数据窗口中显示一个值,如PineCoders 常见问题解答关于调试的部分的第二个问题中所述。 As you move your mouse over chart bars, the corresponding value of the variable/expression will show in the Data Window.当您将鼠标移到图表栏上时,变量/表达式的相应值将显示在数据窗口中。 The FAQ explains other useful techniques that can be used to debug on the chart, as that is sometimes more efficient.常见问题解答解释了可用于在图表上进行调试的其他有用技术,因为这有时更有效。


We use an AutoHotkey macro that creates the required plotchar() statement from a variable or expression previously copied to the clipboard.我们使用AutoHotkey宏从之前复制到剪贴板的变量或表达式创建所需的plotchar()语句。 This is the AHK macro:这是 AHK 宏:

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

The Data Window is also a great option as a display panel for scripts requiring the display of many values, such as our Backtesting & Trading Engine , which makes extensive use of it:数据窗口也是一个很好的选择,作为需要显示许多值的脚本的显示面板,例如我们的回测和交易引擎,它广泛使用了它: 在此处输入图片说明

With pine v.4 there's new way to print text. pine v.4 提供了一种打印文本的新方法。 You can use labels for that:您可以为此使用标签:

//@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

If you just want to print a single value (not at every bar), you can do:如果您只想打印单个值(不是在每个条形上),您可以执行以下操作:

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

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


There is no way to print text or variables in pine-script to any form of "console".无法将 pine-script 中的文本或变量打印到任何形式的“控制台”。 But you can use plot to print, very short text above or below each tick.但是您可以使用 plot 打印每个刻度上方或下方的非常短的文本。 The text, however, is static and you cannot use variables to change it.但是,文本是静态的,您不能使用变量来更改它。

You can also use various tricks to show values in the very limited indicator fields (on top-left) of the chart window.您还可以使用各种技巧在图表窗口的非常有限的指标字段(左上角)中显示值。 Or move lines and plots out of visible are of chart, but still see the axis highlights.或者将线条和绘图移出图表的可见区域,但仍会看到轴突出显示。

Please check their Wiki and consult the 1000's of other user's scripts to find out the details on how to do it.请检查他们的 Wiki 并查阅其他用户的 1000 多个脚本以了解有关如何执行此操作的详细信息。

As mentioned by not2qubit, there is technically not a way to print stuff to the console of TradingView or so.正如 not2qubit 所提到的,技术上没有办法将内容打印到 TradingView 左右的控制台。

But we can create labels to "print" stuff, which is why I wrote this little function.但是我们可以创建标签来“打印”东西,这就是我写这个小 function 的原因。

It will "print" the text you feed it on the latest bar_index.它将“打印”您在最新的 bar_index 上输入的文本。 If you print multiple things, the labels will be stacked on top of each other.如果您打印多件东西,标签将堆叠在一起。

Only tested on PineScript Version 5仅在 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)

Example:例子:

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

What I do is, I use table to display the values i want to display.我所做的是,我使用 table 来显示我想要显示的值。 And this works like magic in replay bar.这就像重播栏中的魔术一样。 Here's an example:下面是一个例子:

//@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)

And here's the result:结果如下: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM