'Understanding a code snippet in pine script

I'm new to pine-script. I'm trying to understand an indicator 'HalfTrend' by Alex Orekhov (everget) at TradingView.

I'm having hard time in understanding following snippet, may you please explain this:

if not na(trend[1]) and trend[1] != 1 

Does the above line mean the following:

na(trend[1]) //check if trend[1] exists

trend[1] != 1 //if trend[1] exists, check if it is not equal to 1

And not of the whole expression

In other words, are we checking if trend[1] exists and if it is equal to 1, am i right???



Solution 1:[1]

Yes, your understanding is correct. na() will check if it is NaN. [1] refers to the previous value of the series.

Since it refers to a historical value with [1], for the very first bar of the chart it will return na. Because there is no previous value yet. That's why that check is there for.

//@version=5
indicator("My script")
trend = 0
plot(trend[1])

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius