vendredi 29 mai 2015

if-else conditions depending on previous value

I want to create a vector z which contains only 1 or -1. If 1 or -1 is needed depends on the Value of RSI:

If RSI is greater than 70, z should be -1
If RSI is less than 30, z should be 1

For all other cases: z should have the same number as the previous z.
That means z = z(t-1)

RSI is a vector containing numbers between 0 and 100. So far I have used the ifelse condition.

rsi <- RSI(YENDOL, n=14)

z <- 0

z <- ifelse(rsi >= 70,-1,z)
z <- ifelse(rsi <= 30,1,z)

With this I have created a z vector containing 0, 1 and -1. The next step would be to change the zeros into 1 or -1 depending on the previous value (z t-1). That is the point where I'm stuck. I need the vector later on to multiply it with another vector.

Aucun commentaire:

Enregistrer un commentaire