In the RSI code above, we have written that:
signal [1:day+1] <- 0 for (i in (day+1): length(price)){ if (rsi[i] < 30){ signal[i] <- 1 }else { signal[i] <- 0 } }
A more efficient but less readable code is to avoid counting:
for (i in 1:length(price)){ signal[i] <- 0 if (isTRUE(rsi[i] < 30)){ signal[i] <- 1 } }