lib_unitLibrary "lib_unit"
functions for assertions and unit testing
method init(this)
Namespace types: Test
Parameters:
this (Test)
method is_true(this, expression, message)
assert that expression is true, if it's false a runtime error will be thrown
Namespace types: Test
Parameters:
this (Test)
expression (bool) : The value to be true
message (string) : The message to print in the runtime error
method is_false(this, expression, message)
assert that expression is false, if it's true a runtime error will be thrown
Namespace types: Test
Parameters:
this (Test)
expression (bool) : The value to be false
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown
Namespace types: Test
Parameters:
this (Test)
expression (string) : The value to test
expected (string) : The expected value
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown
Namespace types: Test
Parameters:
this (Test)
expression (int) : The value to test
expected (int) : The expected value
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown
Namespace types: Test
Parameters:
this (Test)
expression (float) : The value to test
expected (float) : The expected value
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown. This version is testing length, order and values
Namespace types: Test
Parameters:
this (Test)
expression (string ) : The array to test
expected (string ) : The expected array
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown. This version is testing length, order and values
Namespace types: Test
Parameters:
this (Test)
expression (int ) : The array to test
expected (int ) : The expected array
message (string) : The message to print in the runtime error
method equals(this, expression, expected, message)
assert if expression and expected are equal, if they don't match a runtime error will be thrown. This version is testing length, order and values
Namespace types: Test
Parameters:
this (Test)
expression (float ) : The array to test
expected (float ) : The expected array
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (string) : The value to test
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (int) : The value to test
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (float) : The value to test
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (string ) : The value to test
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (int ) : The value to test
message (string) : The message to print in the runtime error
method not_na(this, expression, message)
assert if expression is not na, if it is a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression (float ) : The value to test
message (string) : The message to print in the runtime error
method gt(this, expression1, expression2, message)
assert that expression1 > expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (int) : The value that should be greater
expression2 (int) : The value that should be lesser
message (string) : The message to print in the runtime error
method gt(this, expression1, expression2, message)
assert that expression1 > expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (float) : The value that should be greater
expression2 (int) : The value that should be lesser
message (string) : The message to print in the runtime error
method gte(this, expression1, expression2, message)
assert that expression1 >= expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (int) : The value that should be greater or equal
expression2 (int) : The value that should be lesser or equal
message (string) : The message to print in the runtime error
method gte(this, expression1, expression2, message)
assert that expression1 >= expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (float) : The value that should be greater or equal
expression2 (int) : The value that should be lesser or equal
message (string) : The message to print in the runtime error
method lt(this, expression1, expression2, message)
assert that expression1 < expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (int) : The value that should be lesser
expression2 (int) : The value that should be greater
message (string) : The message to print in the runtime error
method lt(this, expression1, expression2, message)
assert that expression1 < expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (float) : The value that should be lesser
expression2 (int) : The value that should be greater
message (string) : The message to print in the runtime error
method lte(this, expression1, expression2, message)
assert that expression1 <= expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (int) : The value that should be lesser or equal
expression2 (int) : The value that should be greater or equal
message (string) : The message to print in the runtime error
method lte(this, expression1, expression2, message)
assert that expression1 <= expression2, if it is not, a runtime error will be thrown.
Namespace types: Test
Parameters:
this (Test)
expression1 (float) : The value that should be lesser or equal
expression2 (int) : The value that should be greater or equal
message (string) : The message to print in the runtime error
Test
Fields:
strict (series__bool)
verbose (series__bool)
logger (|robbatt/lib_log/2;Logger|#OBJ)
Logging
lib_logLibrary "lib_log"
library for logging and debugging pine scripts
method init(this)
Namespace types: Logger
Parameters:
this (Logger)
method debug(this, message, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger to add the entry to
message (string) : The Message to add
condition (bool) : optional flag to enable disable logging of this entry dynamically (default: true)
method info(this, message, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger to add the entry to
message (string) : The Message to add
condition (bool) : optional flag to enable disable logging of this entry dynamically (default: true)
method success(this, message, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger to add the entry to
message (string) : The Message to add
condition (bool) : optional flag to enable disable logging of this entry dynamically (default: true)
method warning(this, message, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger to add the entry to
message (string) : The Message to add
condition (bool) : optional flag to enable disable logging of this entry dynamically (default: true)
method error(this, message, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger to add the entry to
message (string) : The Message to add
condition (bool) : optional flag to enable disable logging of this entry dynamically (default: true)
method debug_bar(this, message, bar, y, y_offset, last_only, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger object to check global min level condition
message (string) : The string to print
bar (int) : The bar to print the label at (default: bar_index)
y (float) : The price value to print at (default: high)
y_offset (float) : A price offset from y if you want to print multiple labels at the same spot
last_only (bool)
condition (bool)
method info_bar(this, message, bar, y, y_offset, last_only, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger object to check global min level condition
message (string) : The string to print
bar (int) : The bar to print the label at (default: bar_index)
y (float) : The price value to print at (default: high)
y_offset (float) : A price offset from y if you want to print multiple labels at the same spot
last_only (bool)
condition (bool)
method success_bar(this, message, bar, y, y_offset, last_only, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger object to check global min level condition
message (string) : The string to print
bar (int) : The bar to print the label at (default: bar_index)
y (float) : The price value to print at (default: high)
y_offset (float) : A price offset from y if you want to print multiple labels at the same spot
last_only (bool)
condition (bool)
method warning_bar(this, message, bar, y, y_offset, last_only, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger object to check global min level condition
message (string) : The string to print
bar (int) : The bar to print the label at (default: bar_index)
y (float) : The price value to print at (default: high)
y_offset (float) : A price offset from y if you want to print multiple labels at the same spot
last_only (bool)
condition (bool)
method error_bar(this, message, bar, y, y_offset, last_only, condition)
Namespace types: Logger
Parameters:
this (Logger) : Logger object to check global min level condition
message (string) : The string to print
bar (int) : The bar to print the label at (default: bar_index)
y (float) : The price value to print at (default: high)
y_offset (float) : A price offset from y if you want to print multiple labels at the same spot
last_only (bool)
condition (bool)
LogEntry
Fields:
timestamp (series__integer)
bar (series__integer)
level (series__integer)
message (series__string)
Logger
Fields:
min_level (series__integer)
color_logs (series__bool)
max_lines (series__integer)
line_idx (series__integer)
table_pos (series__string)
display (series__table)
log (array__|LogEntry|#OBJ)
JavaScript-style Debug ConsoleThis library provides a JavaScript-style debug console to Pine Coders. It supports the most commonly used utilities from the WHATWG Console Standard including the following:
• console.log
• console.debug
• console.info
• console.warn
• console.error
• console.assert
• console.count
• console.countReset
• console.group
• console.groupEnd
• console.clear
In addition to the WHATWG standard, this library also supports the following methods:
• console.show
• console.hide
FEATURES
• Follows the WHATWG Console Standard, which is widely adopted by all major JavaScript runtimes including browsers and Node.js.
• Provides an out-of-box UI with pre-configured theming, ensuring a clean and professional-looking console.
• Allows for easy UI customizations to fit your personal preferences.
• Has extremely simple import and initialization, making it easy to integrate with your existing codebase.
USAGE
1. Import this library:
import algotraderdev/Console/1
2. Initialize the console object:
var console = Console.new()
// You can also specify optional params to customize the look & feel.
var console = Console.new(
position = position.bottom_right,
max_rows = 50,
width = 0,
text_size = size.normal,
background_color = #000000CC,
timestamp_color = #AAAAAA,
info_message_color = #DDDDDD,
debug_message_color = #AAAAAA,
warn_message_color = #FFEB3B,
error_message_color = #ff3c00)
3. Use the console object to debug your code. Here are some examples:
// Basic logging
console.log('hello world!') // prints 'hello world'
console.warn('warn') // prints 'warn' in yellow
console.error('error') // prints 'error' in red
console.clear() // clears the console
// Assertion
console.assert(a.isEmpty(), 'array should be empty') // prints 'assertion failed: array should be empty' if the array is not empty
// Counter
console.count('fooFunction') // prints 'fooFunction: 1'
console.count('fooFunction') // prints 'fooFunction: 2'
console.countReset('fooFunction') // resets the counter
console.count('fooFunction') // prints 'fooFunction: 1'
// Group
console.log('A')
console.group()
console.log('B')
console.group()
console.log('C')
console.log('D')
console.groupEnd()
console.log('E')
console.groupEnd()
console.log('F')
// prints
// A
// B
// C
// D
// E
// F
// Hide and show
console.hide()
console.show()
Console📕 Console Library
🔷 Introduction
This script is an adaptation of the classic JavaScript console script. It provides a simple way to display data in a console-like table format for debugging purposes.
While there are many nice console/logger scripts out there, my personal goal was to achieve inline functionality and visual object (label, lines) logging .
🔷 How to Use
◼ 1. Import the Console library into your script:
import cryptolinx/Console/1
- or -
Instead of the library namespace, you can define a custom namespace as alias.
import cryptolinx/Console/1 as c
◼ 2. Create and init a new `` object.
The `init()` method is used to initialize the console object with default settings. It can be used to customize it.
// When using the `var` keyword in a declaration, the logs will act as ever-forwarding.
// Without `var`, the `console` variable will be redeclared every time `bar` is called.
// var console = Console.terminal.new(log_position=position.bottom_left, prefix = '> ', show_no = true)
- or -
If you has set up an alias before.
var console = c.terminal.new().init()
◼ 3. Logging
// inline ✨
array testArray = array.new(3, .0).log(console)
// basic
console.log(testArray)
// inline ✨
var testLabel = label.new(bar_index, close, 'Label Text').log(console)
// basic
console.log(testLabel)
// It is also possible to use `().` for literals ✨.
int a = 100
testCalc = (5 * 100).log(console) + a.log(console) // SUM: 600
console.
.empty()
.log('SUM' + WS + testCalc.tostring())
◼ 4. Visibility
Finally, we need to call the `show()` method to display the logged messages in the console.
console.show(true) // True by default. Simply turn it on or off
Logger Library For Pinescript (Logging and Debugging)Library "LoggerLib"
This is a logging library for Pinescript. It is aimed to help developers testing and debugging scripts with a simple to use logger function.
Pinescript lacks a native logging implementation. This library would be helpful to mitigate this insufficiency.
This library uses table to print outputs into its view. It is simple, customizable and robust.
You can start using it's .log() method just like any other logging method in other languages.
//////////////////
USAGE
//////////////////
-- Recommended: Please Read The Documentation From Source Code Below. It Is Much More Readable There And Will Be Updated Along With Newer Versions. --
Importing the Library
---------------------
import paragjyoti2012/LoggerLib/ as Logger
.init() : Initializes the library and returns the logger pointer. (Later will be used as a function parameter)
.initTable: Initializes the Table View for the Logger and returns the table id. (Later will be used as a function parameter)
parameters:
logger: The logger pointer got from .init()
max_rows_count: Number of Rows to display in the Logger Table (default is 10)
offset: The offset value for the rows (Used for scrolling the view)
position: Position of the Table View
Values could be:
left
right
top-right
(default is left)
size: Font Size of content
Values could be:
small
normal
large
(default is small)
hide_date: Whether to hide the Date/Time column in the Logger (default is false)
returns: Table
example usage of .initTable()
import paragjyoti2012/LoggerLib/1 as Logger
var logger=Logger.init()
var logTable=Logger.initTable(logger, max_rows_count=20, offset=0, position="top-right")
-------------------
LOGGING
-------------------
.log() : Logging Method
params: (string message, |string| logger, table table_id, string type="message")
logger: pass the logger pointer from .init()
table_id: pass the table pointer from .initTable()
message: The message to log
type: Type of the log message
Values could be:
message
warning
error
info
success
(default is message)
returns: void
///////////////////////////////////////
Full Boilerplate For Using In Indicator
///////////////////////////////////////
P.S: Change the | (pipe) character into square brackets while using in script (or copy it from the source code instead)
offset=input.int(0,"Offset",minval=0)
size=input.string("small","Font Size",options=|"normal","small","large"|)
rows=input.int(15,"No Of Rows")
position=input.string("left","Position",options=|"left","right","top-right"|)
hide_date=input.bool(false,"Hide Time")
import paragjyoti2012/LoggerLib/1 as Logger
var logger=Logger.init()
var logTable=Logger.initTable(logger,rows,offset,position,size,hide_date)
rsi=ta.rsi(close,14)
|macd,signal,hist|=ta.macd(close,12,26,9)
if(ta.crossunder(close,34000))
Logger.log("Dropped Below 34000",logger,logTable,"warning")
if(ta.crossunder(close,35000))
Logger.log("Dropped Below 35000",logger,logTable)
if(ta.crossover(close,38000))
Logger.log("Crossed 38000",logger,logTable,"info")
if(ta.crossunder(rsi,20))
Logger.log("RSI Below 20",logger,logTable,"error")
if(ta.crossover(macd,signal))
Logger.log("Macd Crossed Over Signal",logger,logTable)
if(ta.crossover(rsi,80))
Logger.log("RSI Above 80",logger,logTable,"success")
////////////////////////////
// For Scrolling the Table View
////////////////////////////
There is a subtle way of achieving nice scrolling behaviour for the Table view. Open the input properties panel for the table/indicator. Focus on the input field for "Offset", once it's focused, you could use your mouse scroll wheel to increment/decrement the offset values; It will smoothly scroll the Logger Table Rows as well.
/////////////////////
For any assistance using this library or reporting issues, please write in the comment section below.
I will try my best to guide you and update the library. Thanks :)
/////////////////////
Debug_Window_LibraryLibrary "Debug_Window_Library"
Provides a framework for logging debug information to a window on the chart.
consoleWrite(txt, maxLines) Adds a line of text to the debug window. The text is rolled off the bottom of the window as it fills up.
Parameters:
txt : - this is the text to be appended to the window
maxLines : - this is the size of the window in lines.
Returns: nothing
The example above shows the close value for the last 10 bars.
Here's the code.
//@version=5
indicator("Debug Library test Script", overlay=true)
import sp2432/Debug_Window_Library/1 as dbg
// add some text to the debug window
dbg .consoleWrite( str .tostring(close), 10)
Logging in Pine ScriptI'm building quite a lot of pretty complicated indicators/strategies in Pine Script. Quite often they don't work from the 1 try so I have to debug them heavily.
In Pine Script there are no fancy debuggers so you have to be creative. You can plot values on your screens, check them in the data window, etc.
If you want to display some textual information, you can plot some info as labels on the screen.
It's not the most convenient way, so with the appearance of tables in Pine Script, I decided to implement a custom logger that will allow me to track some useful information about my indicator over time.
Tables work much better for this kind of thing than labels. They're attached to your screen, you can nicely scale them and you can style them much better.
The idea behind it is very simple. I used few arrays to store the message, bar number, timestamp, and type of the message (you can color messages depend on the type for example).
There is a function log_msg that just append new messages to these arrays.
In the end, for the last bar, I create the table and display the last X messages in it.
In parameters, you can show/hide the entire journal, change the number of messages displayed and choose an offset. With offset, you can basically scroll through the history of messages.
Currently, I implemented 3 types of messages, and I color messages according to these types:
Message - gray
Warning - yellow
Error - red
Of course, it's a pretty simple example, you can create a much fancier way of styling your logs.
What do you think about it? Is it useful for you? What do you use to debug code in Pine Script?
Disclaimer
Please remember that past performance may not be indicative of future results.
Due to various factors, including changing market conditions, the strategy may no longer perform as good as in historical backtesting.
This post and the script don’t provide any financial advice.