0

我正在尝试计算 14 天回溯期的 RSI。我的 excel 文件中有三列 - 日期、打开、关闭。但是当我在 if 语句中检查 dff>0 时,会引发错误 - if (dff > 0) { 中的错误:参数的长度为零。下面是我的代码。有什么建议为什么会发生这种情况?

library(readxl)
data <- read_excel(address of file)
data <- data.frame(data)
rows <- nrow(data)
RSI <- rep(0,rows)
for(i in 14:rows){
  gain <- 0
  cnt_gain <- 0
  cnt_loss <- 0
  loss <- 0
  for(j in i-13:i){
    dff <- data[j,3]-data[j,2]
    if(dff > 0){
      gain <- gain + dff
      cnt_gain <- cnt_gain + 1
    }
    else{
      loss <- loss -dff
      cnt_loss <- cnt_loss + 1
    }
  }
  avg_gain <- gain/cnt_gain
  avg_loss <- loss/cnt_loss
  RSI[i] <- 100 - 100/(1+ avg_gain/avg_loss)
}
4

0 回答 0