Simple loop question. Trying to find a loop (not preferred) or anything else to come up with a signal for stoch. if Diff <1, >-1, 1, else 0.
library(quantmod)
getSymbols("SPY", src="yahoo", from="2013-01-01", to="2015-05-01")
y <- na.omit(merge(SPY, SMI(Cl(SPY))))
y$diff <- y$SMI - y$signal
# if Diff <1, >-1, then 1, else 0.
If your question regarding your comment to @MrFlick is that you want to map the Difference (y$diff) for values below -1 to -1, values above 1 to 1 and the remaining values to 0 this line will do it:
y$diff.map <- ifelse(y$diff>1,1,ifelse(y$diff< -1,-1,0))
In reply to your updated comment the line below will map values between -1 and +1 to 1 and values outside this range to zero.
y$diff.map <- ifelse(y$diff >= -1 & y$diff <= 1,1,0)