Pinescript initialisation

2019-08-06 06:01发布

问题:

I am trying to understand the following pinescript indicator code - https://www.tradingview.com/script/XzcIRUHv-JMA-RSX-Clone-LazyBear/

I am not sure what values variables like f90, f88 hold. Are they predefined short-cuts for something else? They seem to be arrays because they are used with index. E.g.:

f90_ = (nz(f90_[1]) == 0.0) ? 1.0 : (nz(f88[1]) <= nz(f90_[1])) ? nz(f88[1])+1 : nz(f90_[1])+1

回答1:

They are not built-in variables.

pine-script versions 1 and 2 allow you to access variables with [] in combination with nz() even though the variable is not yet declared. So, the following is valid in version 1 and version 2:

f90_ = (nz(f90_[1]) == 0.0) ? 1.0 : (nz(f88[1]) <= nz(f90_[1])) ? nz(f88[1])+1 : nz(f90_[1])+1

If you try this in //@version=3, you will get an Undeclared identifier error.

Let's shorten the code to the following:

//@version=2
study(title="JMA RSX Clone [LazyBear]", shorttitle="RSXC_LB", overlay=false)
length=input(14)

f90_ = (nz(f90_[1]) == 0.0) ? 1.0 : (nz(f88[1]) <= nz(f90_[1])) ? nz(f88[1])+1 : nz(f90_[1])+1
f88 = (nz(f90_[1]) == 0.0) and (length-1 >= 5) ? length-1.0 : 5.0 
plot(f90_, title="f90", color=orange, linewidth=4)
plot(f88, title="f88", color=red, linewidth=4)

Let's look at what happens to f90_ and f88 for the very first bar.

f90_ = (nz(f90_[1]) == 0.0) ? 1.0 : (nz(f88[1]) <= nz(f90_[1])) ? nz(f88[1])+1 : nz(f90_[1])+1

The condition here is (nz(f90_[1]) == 0.0). f90_[1] is basically asking the value of one previous bar, but this is the first bar (remember?), so there is no previous value. So, the answer is NaN (Not A Number).

Now, if you put this in nz(), it will return zero. Because nz() replaces NaN values with zeros.

So the condition will be true for the first bar, and f90_ will be assigned to 1.0.

Let's look at f88 now, again for the very first bar.

f88 = (nz(f90_[1]) == 0.0) and (length-1 >= 5) ? length-1.0 : 5.0 

The first condition here is (nz(f90_[1]) == 0.0). This should return true, because of the same reason above.

The second condition is (length-1 >= 5). This should also return true for the default input (14).

So, f88 will be assigned to 14-1 = 13 for the first bar.

I think you can continue from here on. Try to run the short code I provided and see the chart.



标签: pine-script