1

Recalling a variable

If you’re a novice programmer in Pine, you may have encountered the situation of needing that remember a value during cycles.

The graph draws the bars from left to right and from the beginning (or from a particular time), we can assign a value to a variable (although it will not draw) and keep this reminder over the time.

To remember a value between a bar and the next is needed to copy the previous value of this variable in the current variable.

variable = variable[1]

The first bar not have a previous bar. Then, to assign a first value you must to use the function nz( ) that detects whether a variable is uninitialized (empty) and if so, it assigns the initial value …

variable = nz(variable[1],23)

In this example the initial value is 23. This variable will always have the value 23 because this code copy its value from the previous bar.
If you need to assign other value when a condition occurs then you can use this code…

variable = condition? new_value : nz(variable[1],23)

PineScripts.

One Comment

  1. Hello,
    How do I say “last instance of a price change was up” even if the last change was several bars ago? Hope you can help! Thanks you.

Comments are closed.