Vivek Ajay
Vivek Ajay

Reputation: 11

Pine script coding - how to refer to an existing variable

I'm still learning pine, but I'm hoping someone can help me with what I'm trying to do: I already have a variable that can return two different values: either 1 or -1. I want to create a new variable that'll only generate a signal when the first variable is 1. How can I do that. This is what I have:

fD = 0
sD = 0


fD := hlc3 > fastMA ? 1 : hlc3 < fastMA ? -1 : nz(fDirection[1], 1)
sD := hlc3 > slowMA ? 1 : hlc3 < slowMA ? -1 : nz(sDirection[1], 1)```

I want to create a new variable ```fDPOS``` when ```fD``` is equal to 1; essentially ignoring the -1 value. How can I do that in pine?

Upvotes: 0

Views: 107

Answers (1)

alexgrover
alexgrover

Reputation: 1043

You can rewrite your script as follows:

fD = sign(hlc3 - fastMA)
sD = sign(hlc3 - slowMA)

The sign function returns 1 when the argument is greater than 0 and -1 when lower than 0.

Then regarding your question I understood that you want to generate a new value once fD = 1, this can be done with:

fDPOS = change(fD) > 0 ? 1 : 0

Upvotes: 1

Related Questions