我用 sma 退出回测 rsi2 策略,但信号是错误的

i backtest rsi2 strategy with an sma exit but the signals are wrong

我已经在几个平台上回测了这个策略,但我是 R 的新手。逻辑是正确的,因为它适用于其他软件,但它不适用于 R。

如果 rsi(2)<10` 和收盘价 sma(10),则策略走多

你能帮帮我吗?我附上代码。

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)

这里做了很多猜测,因为你没有解释你想做什么:

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