i have backtested this strategy on several platforms but i m new to R. the logic is correct as it works with other softwares however it doesnt work right with R.
the strategy go long if rsi(2)<10` and the close price sma(10)
Can you please help me? i m attaching the code.
getSymbols("spy",from ="1995-01-01", to="2016-05-13")
rsi <- RSI(Cl(SPY),2)
smashort<-SMA(Cl(SPY),10)
signal<-ifelse(Cl(SPY)<smashort &rsi<10,1,ifelse(Lag(signal,1)>0 & Cl(SPY)<smashort, 1,0))
signal<-lag(signal,1)
signal[is.na(signal)] <- 0
ret <- ROC(Cl(SPY))
ret[1] <- 0
equity<-exp(cumsum(ret*signal))
plot(equity)
Doing a lot of guessing here as you don't explain what you're trying to do:
library(quantmod)
getSymbols("spy",from ="1995-01-01", to="2016-05-13")
rsi <- RSI(Cl(SPY),2)
closes <- Cl(SPY)
smashort <- SMA(closes,10)
signal <- ifelse((rsi < 10) & (closes <= smashort), 1, 0)
length(signal[signal == 1])
# 535 buy signals where rsi is less than 10
# and SPY close is less than the 10 period moving average