Explaining nz
// 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
indicator("Explaining nz")

// 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/

// -------[             This is meant for Pine Script coders who have trouble with the use of 'nz'             ]-------


// -------[ 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   ]-------

options = input.string('A[1]', title='options', options=['A', 'A[1]', 'nz(A[1])', 'nz(A[1], 1)', 'B', 'nz(close[20])'])

// Let's start with this -> choose -> settings -> Options -> 'A':

A = 0
// plot -> A
plot(options == 'A' ? A : na)

// On every bar, also the first one, A is declared as 0 
// -> the code 'says' -> on every bar -> make a new variable which has the value 0

// Importantly -> everything is put in a 'log' -> this makes a 'series', so when plotting A, this is done out of the log and shows everything what happened bar per bar

// example and recap:

// plot A -> first bar -> A is declared as 0, at the close of this bar -> put data in log ( = 0) and delete A
//          second bar -> A is declared as 0, at the close of this bar -> put data in log ( = 0) and delete A
//           third bar -> A is declared as 0, at the close of this bar -> put data in log ( = 0) and delete A
//           forth bar -> A is declared as 0, at the close of this bar -> put data in log ( = 0) and delete A
// ...

// Therefore in this case we get a line with all zero's


// Now choose -> settings -> Options -> 'A[1]':
plot(options == 'A[1]' ? A[1] : na)

// -> the code 'says' -> On every bar -> Show the previous value (A[1] = previous A)
// -> From the log data -> data (A) of previous bar ([1]) is pulled out and plotted as a line
// plot -> A[1]


// Everything is the same, except the first one (nothing shows), why?

// There is NOTHING before the first bar -> it is Not Available (= n/a ) so if you want to show previous data of this first bar, this is impossible!
// The second bar is no problem, since the FIRST bar was declared as 0, so if you'd plot previous data on this second bar, then you'll get data from the first bar -> which is 0



// Now, if you do this -> choose -> settings -> Options -> 'nz(A[1])' : 

// plot -> nz(A[1]) 
plot(options == 'nz(A[1])' ? nz(A[1]) : na)

// -> the first bar is visible again

// Why? 

// A task is set on every bar -> when data between brackets - nz(data) - is n/a -> replace this data by 0
// First bar -> A[1] = n/a -> replaced by 0 

// without nz -> on the FIRST bar A[1] = n/a
// with    nz -> on the FIRST bar nz(A[1]) ->  A[1] = n/a -> replace this by 0 -> nz(A[1]) = 0 



// You can also replace the 0 by another number (in the case the variable uses numbers)

// example (Choose -> settings -> Options -> 'nz(A[1], 1)'):
plot(options == 'nz(A[1], 1)' ? nz(A[1], 1) : na)

// Since ONLY the first bar's data was n/a -> ONLY this data is altered, now by 1

// Why is all of this important?
// Because it is not just about the first bar

// example:
// Choose -> settings -> Options -> 'B'

B  = 0
B := close > close[20] ? 1 : B[1]

plot(options == 'B' ? B : na)

// (*)    :=  -> means -> adjust 'B' into '1' if 'close > close[20]' is indeed true -> ELSE ( : ) -> remember the previous value of 'B'


// Here the code says -> Replace B by 1 IF the close of this bar is greater than the close of 20 bars ago
// FIRST bar -> there is NO close found 20 bars back -> this task is impossible for this bar (and the following 20 bars)
// since this is impossible -> the code now must remember its previous value (B[1] -> see -> (*) ) but this is n/a ! So it remembers -> n/a
// This continues until 20 bars further -> which means you cannot do anything with these first 20 bars 



// Now choose -> settings -> Options -> 'nz(close[20])'
// This is exactly the same code but with nz -> the code replaces the unavailable data with 0

C  = 0
C := close > nz(close[20]) ? 1 : C[1]

plot(options == 'nz(close[20])' ? C : na)


// Now you can use data of these first 20 bars, or MORE IMPORTANTLY -> you wouldn't get into trouble with calculations with n/a


