7.6 Comparing rules
We can compare filter and RSI rules directly.
We start with some initalization:
14
day <- Cl(MSFT)
price <- (Cl(MSFT) - Op(MSFT))/Op(MSFT)
returnMSFT <-
price/Lag(price) - 1 # % price change
r <-0.005 #threshold
delta <-c(0) # first date has no signal
filter.signal <-
RSI(price, day) #rsi is the lag of RSI
rsi <- c() #initialize vector
RSI.signal <-1:day+1] <- 0 #0 because no signal until day+1 RSI.signal [
We calculate the trading signal for filter rule:
for (i in 2: length(price)){
if (r[i] > delta){
1
filter.signal[i]<-else
} 0
filter.signal[i]<-
}reclass(filter.signal,Cl(MSFT)) filter.signal<-
We calculate the trading signal for RSI rule:
for (i in (day+1): length(price)){
if (rsi[i] < 30){ #buy if rsi < 30
1
RSI.signal[i] <-else { #no trade all if rsi > 30
} 0
RSI.signal[i] <-
}
}
reclass(RSI.signal,Cl(MSFT)) RSI.signal<-
We now all apply our trading rules are based on yesterday signal:
Lag(filter.signal,1)
filter.trade <- returnMSFT*filter.trade
filter.ret <-names(filter.ret) <- 'filter'
Lag(RSI.signal,1)
RSI.trade <- returnMSFT*RSI.trade
RSI.ret <-names(RSI.ret) <- 'RSI'
Now compare the two trading rules:
cbind(filter.ret, RSI.ret)
retall <-charts.PerformanceSummary(retall,
main="filter v.s. RSI")