7.4 Buy rule based on RSI
Consider following day-trading strategy based on 14-day RSI:
- buy one unit if RSI <30 and
- otherwise no trade.
Evaluate this based on day trading:
14
day <- Cl(MSFT)
price <- (Cl(MSFT) - Op(MSFT))/Op(MSFT)
returnMSFT <-
c() #initialize vector
signal <- RSI(price, day) #rsi is the lag of RSI
rsi <-1:day+1] <- 0 #0 because no signal until day+1
signal [
for (i in (day+1): length(price)){
if (rsi[i] < 30){ #buy if rsi < 30
1
signal[i] <-else { #no trade all if rsi > 30
} 0
signal[i] <-
}
}reclass(signal,Cl(MSFT)) signal<-
To keep thing simple, we allow costless short selling. Otherwise, we only allow a sell after a buy.
Lag(signal)
trade <-*trade
ret<-returnMSFT names(ret) <- 'RSI'
charts.PerformanceSummary(ret)