OPEN-SOURCE SCRIPT

Pine Execution Map

853
Pine Script Execution Map

Overview:
This is an educational script for Pine Script developers. The script includes data structure, functions/methods, and process to capture and print Pine Script execution map of functions called while pine script execution.

Map of execution is produced for last/latest candle execution.

The script also has example code to call execution map methods and generate Pine Execution map.

Use cases:
  • Pine script developers can get view of how the functions are called
  • This can also be used while debugging the code and know which functions are called vs what developer expect code to do
  • One can use this while using any of the open source published script and understand how public script is organized and how functions of the script are called.



Code components:

User defined type
Pine Script®
type EMAP
    string group 
    string sub_group
    int level
array<EMAP> emap = array.new<EMAP>()


method called internally by other methods to generate level of function being executed
Pine Script®
method id(string tag) =>
    if(str.startswith(tag, "MAIN"))
        exe_level.set(0, 0)
    else if(str.startswith(tag, "END"))
        exe_level.set(0, exe_level.get(0) - 1)
    else 
        exe_level.set(0, exe_level.get(0) + 1)
    exe_level.get(0)


Method called from main/global scope to record execution of main scope code. There should be only one call to this method at the start of global scope.
Pine Script®
method main(string tag) =>
    this = EMAP.new()
    this.group := "MAIN"
    this.sub_group := tag 
    this.level := "MAIN".id()
    emap.push(this)


Method called from main/global scope to record end of execution of main scope code. There should be only one call to this method at the end of global scope.
Pine Script®
method end_main(string tag) =>
    this = EMAP.new()
    this.group := "END_MAIN"
    this.sub_group := tag 
    this.level := 0
    emap.push(this)


Method called from start of each function to record execution of function code
Pine Script®
method call(string tag) =>
    this = EMAP.new()
    this.group := "SUB"
    this.sub_group := tag
    this.level := "SUB".id() 
    emap.push(this)


Method called from end of each function to record end of execution of function code
Pine Script®
method end_call(string tag) =>
    this = EMAP.new()
    this.group := "END_SUB"
    this.sub_group := tag 
    this.level := "END_SUB".id()
    emap.push(this)


Pine code which generates execution map and show it as a label tooltip.
Pine Script®
if(barstate.islast)
    for rec in emap 
        if(not str.startswith(rec.group, "END"))
            lvl_tab = str.repeat("", rec.level+1, "\t")
            txt = str.format("=> {0}  {1}>  {2}", lvl_tab, rec.level, rec.sub_group)
            debug.log(txt)
    debug.lastr()


Snapshot 1:
This is the output of the script and can be viewed by hovering mouse pointer over the blue color diamond shaped label
syot kilat

Snapshot 2:
How to read the Pine execution map
syot kilat

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.