bar_index inspectorThis is a tool for developers who are working with index based plots and find themselves adding the bar_index to their indicators on a regular basis during development and debugging.
What it does:
shows the bar_index in the status line and data window.
plots optional labels (bar index + time) into the chart every 10 bars
Debug
Profiling: array of UDTs vs UDT of arraysUsing Stopwatch Library by PineCoders, I am trying to test which is faster, an array of user-defined type (UDT) objects vs an object with many child arrays.
The task is to store and manipulate array of objects having total 9 values: 4 floats, 4 strings and 1 int.
Option 1: create a UDT with 9 fields and store an array of such UDT objects.
Option 2: create a UDT with 9 arrays individually for each value.
The test task is of three stages:
Populate array(s) with some (timenow) values - in the options you can choose how many values to push into the array/arrays. Note that max size of array(s) is set independently, so you can push 1000 of elements into an array capped at 100 max size and as new elements will be pushed (added to the end) the old exceeding elements will be shifted (removed from the beginning)
Write - write to random elements of the array. Two options for writing to a UDT object: (1) assign to each field independently, (2) create a UDT object and use array.set() function.
Read - read from random elements of the array.
In the options you can how many times per bar to run each of the steps (same number for each step).
I tested by adding three indicators to the chart and choosing different options for each:
1. Array of UDT's where writing is done by creating a new UDT from the values and then using set(udt)
2. Array of UDT's where writing is done by assigning the value of each of the properties of the UDT individually (saving time on creating of a new object).
3. UDT of arrays.
As of 16 Arpil 2023 the UDT of arrays seems about 20-30% faster than the array of UDT's with setting each property without creating new UDT object.
HTF Bars Discrepancy Detector (for debugging)Illustrate discrepancies between two symbols of the same higher timeframe.
Sometimes:
- HTF data can come for one symbol but not for another
- or come with gaps (e.g. after HTF bar 3 in the next chart TF's candle we have HTF bar 5 for one or both symbols)
ANTEYA ProfilingThis script allows to compare which code is more efficient by running it inside a loop for a certain time or a certain number of iterations.
Just paste your pieces of code as Option 1 and Option 2 inside the loop (see comments in the script).
Signal ViewerThe "Signal Viewer" script is a debugging tool that can be used for the signal of a Signal Indicator script like the "Two MA Signal Indicator" or the "Template Signal Indicator". This script will visualize the signal based on the convention that was defined in the settings. Also, alerts will be produced based on this convention. It's useful to be used before you connect the signal indicator script to a template strategy like the "Template Trailing Strategy" script. You can cross-validate the correctness of the signal that the indicators emit and make sure it is aligned with the expected behavior after the decomposition of the signal using the convention described in the settings. Please make sure that the connection in the "Signal Viewer" script matches the convention used by the template strategy script.
Debug tool - tableWhen having a script with lot's of values, it can be difficult to seek the values you need to debug
For example, here, multiple values aren't visible anymore (right side chart)
————————————————————————————————————————————————————————————————
This script show a way where you can show the values in a table on 1 particular bar, with 2 options:
1)
'middle' -> here the script uses chart.left_visible_bar_time and chart.right_visible_bar_time to calculate the middle
the values of that bar (in orange box) is shown, you can check the value by putting your mouse cursor on that bar:
Just zooming in/out, or scrolling through history will automatically show you the middle and the values of that bar.
Using the arrows on your keyboard will allow you to go 1 bar further/back each time.
(Give it some time to load though, also, sometimes you need to click anywhere on the chart before arrows start working)
2)
'time' -> settings -> Date -> the orange bar will be placed on the chosen bar, the values will be placed in the table as well.
————————————————————————————————————————————————————————————————
If the table interfere with the candles, you can alter the position without changing the placement of the orange bar -> settings -> position table
This script holds lots of values, just to show the difference between values placed on the chart, and values, placed in the table.
To make more clear how the script works, an 'example' (v_rsi1 / rsi1) is highlighted in the code itself
Cheers!
Simple debug functionSimple method I used to debug problem in my script.
For loop generates 5 numbers from the given depth. At present, depth is 9
Rules for generating the combinations are as follows:
First number is always 1
Two even numbers should not be adjacent to each other and two odd numbers should not be adjacent to each other
Numbers should be ordered in incremental order.
Print all possible combinations
While the logic above is just a random one. Debug method can be reused for any program.
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.