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

7

54

Is it possible to console.log variables, or the results of function invocation in pine-script? I'd like to convert a pine-script script into JavaScript, and I'd like to verify pinescript variables I'm moving into JavaScript are have the same value in the pinescript script v/s the JavaScript version. Any work arounds are welcome if this functionality does not exist. Thanks!

Englebert answered 7/2, 2018 at 4:47 Comment(1)
You can use plot(...) function to display values of variables e.g. plot(strategy.position_avg_price, 'Position Avg Price'). Variable will be displayed in Data Window you can find on the right side of the screen.Hellenize
P
9

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". But you can use plot to print, very short text above or below each tick. 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.

Precambrian answered 14/2, 2018 at 14:14 Comment(3)
So, you can't pass a variable to be logged using plot? How on earth do you debug a script without being able to log to the console?Englebert
The pine-script interpreter is very rudimentary and the editor will tell you if there are code errors, but not if the logic is wrong. There are 1000's of pine-scripts made by people, many of those are using various tricks to show values (as an indicator - top left) and move lines out of sight so only the axis numbers are highlighted. Please contact T.V. for improvements. SO is for programming specific problems where people already have some scripts to be fixed etc. as I'm sure you already know. Good Luck.Precambrian
Appreciate the response, but I don't think T.V. is very good at getting back to suggestions for improvement. One person asked the same question on their forum...three years ago, and no response lol If anyone would know the answer, it would be here in SO land.Englebert
F
80

NOTES

  1. Pine Logs were introduced since this answer was originally written. Read about them in this TradingView blog post.
  2. Alternately, the techniques described in this answer and in the Pine User Manual's section on debugging techniques may still be useful.
  3. RicardoSantos has a DebugConsole library which v5 users can access.

There is actually something similar to a console for Pine developers; it's the Data Window. We use it constantly to debug. The idea is to use plotchar() in this way:

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

enter image description here

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. 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. This is the AHK macro:

^+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: enter image description here

Foochow answered 16/2, 2020 at 1:50 Comment(0)
C
29

With pine v.4 there's new way to print text. 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
Chassidychassin answered 2/12, 2019 at 9:31 Comment(4)
couldn't find it in the docs but there was thisTwine
just found out about Ctrl+click-ing on the function in the editor to get the latest docsTwine
How can we get the text in console or somewhere else (outside canvas)? I am unable to select the result and copy paste, and currently I have to type each character @TwineSouse
@Souse there is no console in pine so it has to be in canvas. i use qwertzguy's answerTwine
S
14

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)
Serow answered 21/6, 2020 at 22:14 Comment(0)
P
9

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". But you can use plot to print, very short text above or below each tick. 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.

Precambrian answered 14/2, 2018 at 14:14 Comment(3)
So, you can't pass a variable to be logged using plot? How on earth do you debug a script without being able to log to the console?Englebert
The pine-script interpreter is very rudimentary and the editor will tell you if there are code errors, but not if the logic is wrong. There are 1000's of pine-scripts made by people, many of those are using various tricks to show values (as an indicator - top left) and move lines out of sight so only the axis numbers are highlighted. Please contact T.V. for improvements. SO is for programming specific problems where people already have some scripts to be fixed etc. as I'm sure you already know. Good Luck.Precambrian
Appreciate the response, but I don't think T.V. is very good at getting back to suggestions for improvement. One person asked the same question on their forum...three years ago, and no response lol If anyone would know the answer, it would be here in SO land.Englebert
P
6

What I do is, I use table to display the values i want to display. 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: enter image description here

Proportioned answered 30/9, 2021 at 19:33 Comment(0)
S
4

As mentioned by not2qubit, there is technically not a way to print stuff to the console of TradingView or so.

But we can create labels to "print" stuff, which is why I wrote this little function.

It will "print" the text you feed it on the latest bar_index. If you print multiple things, the labels will be stacked on top of each other.

Only tested on PineScript Version 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!")
Stepha answered 23/2, 2022 at 3:56 Comment(0)
U
2

Please see https://www.tradingview.com/blog/en/pine-logs-in-pine-script-40490/

Example:

    log.info("startBarIdx={0} last_bar_index={1} startOffset={2} isStartOffsetValid={3} startSourcePrice={4}", startBarIdx, last_bar_index, startOffset, isStartOffsetValid, startSourcePrice)

Where to check the logs

Unkenned answered 5/12, 2023 at 22:11 Comment(1)
That's the correct answer as of 2024! You can find Pine Logs in the Pine Editor in the same menu where "Save script as.." is.Hellenize

© 2022 - 2024 — McMap. All rights reserved.