0

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? 

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @alexgrover So by saying fDPOS = change(fD) > 0 ? 1 : 0 you're only taking in values/ setting fDPOS = 1 when fD is equal to greater than 0?
Question about the change function @alexgrover : looks like the sign function returns either 1 or -1 for fD and I want fDPOS value to be equal to fD value when fD is equal to 1. Doesn't change function take the difference between current and previous value? So if I use change and my previous value was 1 and current value is 1 wouldn't that set fDPOS = 1-1 = 0?
I understood "once", my mistake, so you can use instead fDPOS = max(fD,0), here fDPOS will be equal to 1 when fD == 1, else 0.
Thank you @alexgrover ! Much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.