hello josh, If I want to sell the stock when the R…
Bear - Jul 4, 2009
If I want to sell the stock when the RSI retereat from the 90 or buy it when the RSI above 10 again how can I modify the code?
You need to use a lagged value of RSI in addition to the current value. The code below shows how to do it.
price <- .xts(20*cumprod(1+rnorm(50)/100),Sys.Date()-50:1)
rsi <- RSI(price,2)
rsiLag <- lag(rsi)
sig <- rep(0,50)
sig <- ifelse(rsiLag > 90 & rsi < 90, -1, sig)
sig <- ifelse(rsiLag < 10 & rsi > 10, 1, sig)
So sig will be ‘1’ if yesterday’s RSI (rsiLag) is less than 10 and today’s RSI (rsi) is greater than 10.
Hope that helps,
Actually I am testing RSI(price,14), so the indicator may be under 20 or above 80 for a time and I use FOR loop instead of ifelse and lag but FOR is slow in R.
Is there any better solution?
The solution I posted above is better. You just have to change the “n” for the RSI function to “14” and the upper and lower bounds in the ifelse() statements to 80 and 20.
Is there a reason why the result is sometimes of typeof “list” instead of “doubles” ?
When I load my datas from 1970 with quantmod the result is typeof “list”; and when I start from 1980 the result is typeof “double”.
The result should always be an xts object. I’m not sure how you’re getting a “list”, but the high / low data prior to 1980 is poor for most Yahoo data.