Explaining VAR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fikira
//@version=5

// This script is part of a collection of educational scripts
// If you want to return to the open source INDEX page, click the link in the comments:
// -> https://www.tradingview.com/script/TUeC9XDq-Education-INDEX/

indicator("Explaining VAR", overlay=false, max_labels_count=500)
i_show = input.bool(false, title='show what happened 3 bars back (\'LOG\')')                

// -------[         This is meant for Pine Script coders who have trouble with the distinction between         ]-------
// -------[                    'var' declared variables and 'non var' declared variables                       ]-------


// -------[ Best to use on a Monthly Timeframe with a Ticker that exists longer then 2 years (example Bitcoin) ]-------
// -------[  Then open the console so you can read and follow the instructions and explanation in the script   ]-------


// [---------------------------------------------–]
// [   -> Enable 'show labels' in settings        ]
// [---------------------------------------------–]



// IMPORTANT to know, what happens on every bar is kept in a 'log', so if a variable is declared as var or not, the log always can be used to look back

// a (-> var a) = declared on the first bar and it holds its memory
// The code 'says' -> On the first bar 'A' IS 0 -> at the end of each bar -> put data in 'log', remember what happened to it -> move forward

// b is declared on every bar, again and again -> no long term memory
// The code 'says' -> On the EVERY bar 'B' IS 0 -> at the end of each bar -> put data in 'log', then just 'forget' 


// [---------------------------------------------–]
// [    -> Disable 'show labels' in settings      ]
// [---------------------------------------------–]



// So...

var a = 10 //  'Long Time Memory'
b     = 10 // 'Short Time Memory' -> what happened is recorded in the log -> variable then 'disappears' and gets rebuild agian the next bar


// [---------------------------------------------–]
// [  -> Look at the chart (first 5 bars 0 -> 4)  ]
// [---------------------------------------------–]


// Both a(green) and b(blue) have a value of 10 the first 5 bars
// 
// bar 1 (n° 0):
// - var a = 10
// - b     = 10
// 


// bar 1 (n° 0):
// - a checks in his memory (was 10) -> on this bar no extra task -> stays 10
// - b = again declared as 10 (new)  -> on this bar no extra task -> stays 10
// 
// bar 2 (n° 1):
// - a checks in his memory (was 10) -> on this bar no extra task -> stays 10
// - b = again declared as 10 (new)  -> on this bar no extra task -> stays 10
// 
// ...
// 
// 
// bar 4 (n° 3):
// - a checks in his memory (was 10) -> on this bar no extra task -> stays 10
// - b = again declared as 10 (new)  -> on this bar no extra task -> stays 10
// 


// [---------------------------------------------–]
// [  -> Look at the chart (next 10 bars 5 -> 14) ]
// [---------------------------------------------–]


// ---------------------[ //    FIRST TASK -> From bar 5 till ... -> add 1 on EVERY BAR     // ]---------------------
                                                                                            // ]
if bar_index >= 5                                                                           // ]
    a += 1                                                                                  // ] 
    b += 1                                                                                  // ]
                                                                                            // ]
// ---------------------[ // -------------------------------------------------------------- // ]---------------------


// 
// bar 5 (n° 4):
// - a checks in his memory (was 10) -> on this bar NO task                                       -> 10
// - b = again declared as 10 (new)  -> on this bar NO task                                       -> 10
// 
// bar 6 (n° 5):
// - a checks in his memory (was 10) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
// 
// bar 7 (n° 6):
// - a checks in his memory (was 11) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 12
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
// 
// bar 8 (n° 7):
// - a checks in his memory (was 12) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 13
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//
// ...
// 


// [---------------------------------------------–]
// [ -> Look at the chart (next 10 bars 15 -> 24) ]
// [---------------------------------------------–]


// ---------------------[ // SECOND TASK -> ONLY on bar 15 and bar 25 -> add 7 on THAT BAR  // ]---------------------
                                                                                            // ]
if bar_index == 15                                                                          // ]
    a += 7                                                                                  // ]
    b += 7                                                                                  // ]
                                                                                            // ]
// ---------------------[ // -------------------------------------------------------------  // ]---------------------



// bar 15 (n° 14):
// - a checks in his memory (was 19) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 20
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//
// bar 16 (n° 15) :
// - a checks in his memory (was 20) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 21
//                                 also on this bar EXTRA task ('SECOND' task, see code) -> add 7 -> 28
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//                                 also on this bar EXTRA task ('SECOND' task, see code) -> add 7 -> 18
// 
// bar 17 (n° 16):
// - a checks in his memory (was 28) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 29
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
// 
// bar 18 (n° 17):
// - a checks in his memory (was 29) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 30
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
// 
// ...
//


// [---------------------------------------------–]
// [ -> Look at the chart (next 10 bars 24 -> ... ]
// [---------------------------------------------–]


// ---------------------[ // THIRD TASK -> ONLY on bar 25 -> add 9 on THAT BAR              // ]---------------------
                                                                                            // ]
if bar_index == 25                                                                          // ]
    a += 9                                                                                  // ]
    b += 9                                                                                  // ]
                                                                                            // ]
// ---------------------[ // -------------------------------------------------------------  // ]---------------------

// bar 25 (n° 24):
// - a checks in his memory (was 36) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 37
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//
// bar 26 (n° 25):
// - a checks in his memory (was 37) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 38
//                                 also on this bar EXTRA task ('SECOND' task, see code) -> add 9 -> 47
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//                                 also on this bar EXTRA task ('SECOND' task, see code) -> add 9 -> 20
//
// bar 27 (n° 26):
// - a checks in his memory (was 45) -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 48
// - b = again declared as 10 (new)  -> on this bar EXTRA task ('FIRST'  task, see code) -> add 1 -> 11
//
// ...
//
// Again, IMPORTANT to know, what happens on every bar is kept in a log, so if a variable is declared as var or not, the log always can be used to look back
//
// Log = series
//

// [------------------------------------------------------------------------------------------–---------–]
// [  Go to settings -> disable 'show labels' for visibility and ENABLE 'show what happened 3 bars back' ]
// [------------------------------------------------------------------------------------–---------------–]


// THIRD task -> on EVERY bar -> show what happened 3 bars back
//
                                                                                            // ]
plot(a, color=i_show ? color.new(color.lime, 85) : color.new(color.lime, 0), title='a')     // ]
plot(b, color=i_show ? color.new(color.blue, 85) : color.new(color.blue, 0), title='b')     // ]
                                                                                            // ]
// ---------------------[ // -------------------------------------------------------------  // ]---------------------


// ---------------------[ // THIRD TASK -> on EVERY bar -> 'show what happened 3 bars back' // ]---------------------
                                                                                            // ]
plot(i_show ? a[3] : na, color=color.lime, title='a')                                       // ]
plot(i_show ? b[3] : na, color=color.blue, title='b')                                       // ]
                                                                                            // ]
// ---------------------[ // -------------------------------------------------------------- // ]---------------------


//
// When 'show what happened 3 bars back' is enabled -> you'll see both lines moving 3 bars further
// this because the task is -> Show on EVERY bar what happened 3 bars ago, this effect is the same on bot a and b
//
// The first 3 bars will show nothing, because 3 bars before bar 0 there was nothing, also 3 bars before bar 1, 2, 3 there was nothing
// The 4th bar ('3') can show the value 3 bars back -> the value of '0' -> 10





i_lab = input.bool(false, title='show labels')
if barstate.isfirst and i_lab
    label.new(bar_index    , 10, text='It all\nstarts\nhere'                                                          , size=size.small)
    label.new(bar_index    , 10, text='var a\ndeclared once\nit holds his memory', style=label.style_label_upper_right, size=size.small)
    label.new(bar_index    , 10, text='b \n declared \nevery bar'                , style=label.style_label_upper_left , size=size.small)
    label.new(bar_index + 1, 10, text='b \n declared \nevery bar'                , style=label.style_label_upper_left , size=size.small)
    label.new(bar_index + 2, 10, text='b \n declared \nevery bar'                , style=label.style_label_upper_left , size=size.small)
    label.new(bar_index + 3, 10, text='b \n ...'                                 , style=label.style_label_upper_left , size=size.small)

label.new(bar_index, a, text=str.tostring(bar_index), color=color.new(color.blue, 100), textcolor=color.new(color.lime, 10), style=label.style_label_down, size=size.small)
label.new(bar_index, b, text=str.tostring(bar_index), color=color.new(color.blue, 100), textcolor=color.new(color.blue, 10), style=label.style_label_up  , size=size.small)

