使用quantmod回溯测试R中的交易策略:函数中的函数和for循环

时间:2015-10-11 13:01:41

标签: r quantmod

我正在使用R,quantmod和Performanceanalystics软件包。作为回测策略的一部分,我正在尝试根据RSI的值创建一个信号/持有向量,告诉我是否应该买入/卖出/持有股票。如果RSI <30,买入(因此持有量增加1),如果RSI在30和30之间。 50,不要做任何事情(所以持股与昨天持平)。如果RSI> = 50,则卖出所有物品(因此持有量变为零)。此后,使用Performanceanalytics中的dailyReturn()函数计算并生成返回图表。

请注意,RSI()是一个“价格”和“天”的函数,而dailyReturn()函数也需要“价格”

我已经能够完美地使用下面的代码了。

library(quantmod)
library(PerformanceAnalytics)
getSymbols("IBM", src= "yahoo", from = "2000-01-01", to ="2015-09-25")

rsi<-RSI(Cl(IBM),14)
rsi<-lag(rsi,1)
rsi[is.na(rsi)] <- 0 

holdings1 <-c() #initialize the vector
holdings1[1:14+1]<-0  #Not holding any shares of IBM from 1st to the 15th day
for (i in 14+2: length(Cl(IBM))){ #assume start buying from the 16th day onwards since we are using RSI where n=14
 if (rsi[i]<30){ #if RSI<30, we buy one share of IBM
  holdings1[i]<-holdings1[i-1]+1  
  } else if (rsi[i]>50){ # if RSI>50, we sell all holdings
  holdings1[i]<-0
 } else 
   holdings1[i]<- holdings1[i-1] # remains the same: if 30<=RSI<=50 we don't do anything, so same holdings as prior
 }
size1<-reclass(holdings1,Cl(IBM))
ret1<-dailyReturn(Cl(IBM))*size1
charts.PerformanceSummary(ret1)

但我需要创建一个名为“size1()”的函数,它接受“价格”和“日”(教授说,我不做计算)。当我尝试这样做时,RStudio告诉我“滞后错误(rsi,1):找不到对象'rsi'”。这是为什么?在函数中创建函数或向量是不合法的吗?或者我应该以与上面第一个不同的方式构建我的代码?功能代码(价格,日期)如下:

library(quantmod)
library(PerformanceAnalytics)
getSymbols("IBM", src= "yahoo", from = "2000-01-01", to ="2015-09-25") #download IBM, from the stipulated range of dates

size1<-function(price,day){
  ris<-RSI(price,day)
  ris<-lag(rsi,1)
  rsi[is.na(rsi)] <- 0
  holdings1<-c()
  holdings1[1:day+1]<-0
 for (i in day+2: length(price)){ #assume start buying from the 15th day onwards since we are using RSI, n=14
  if (rsi[i]<30){ #if RSI<30, we buy one share of IBM
    holdings1[i]<-holdings1[i-1]+1  
  } else if (rsi[i]<50){ # if 30<RSI<50, we don't buy or sell, so that the holdings does not change
    holdings1[i]<-holdings1[i-1]
  } else 
    holdings1[i]<-0 # sell all if RSI>50
  size<-reclass(holdings1,price)
  }
}

ret1<-dailyReturn(Cl(IBM))*size1(Cl(IBM),14)
charts.PerformanceSummary(ret1) 

1 个答案:

答案 0 :(得分:2)

day+2:length(price)不是您的期望。 :优先于+(请参阅?Syntax)。它评估为day + (2:length(price))。你想要(day+2):length(price)。另请注意,xts对象是具有index属性的矩阵,矩阵只是具有dim属性的向量。在矩阵上调用length将返回观察总数(向量的length)。您应该使用nrow代替。

for循环之前预先分配整个结果向量也是一种很好的做法。当holdings大于holdings[i] <-的当前长度时,每当您致电i时,您当前的代码都会附加到holdings

此外,您的功能目前不返回任何内容。看起来您打算返回size对象。请注意,您不需要在每次循环迭代时重新创建该对象。

size1 <- function (price, day) {
    rsi <- RSI(price, day)
    rsi <- lag(rsi, 1)
    rsi[is.na(rsi)] <- 0
    holdings1 <- integer(nrow(price))
    # assume start buying from the 15th day onwards,
    # since we are using RSI, n=14
    for (i in (day+1):nrow(price)) {
        if (rsi[i] < 30) {
            # if RSI<30, we buy one share of IBM
            holdings1[i] <- holdings1[i - 1] + 1
        }
        else if (rsi[i] < 50) {
            # if 30<RSI<50, we don't buy or sell,
            # so that the holdings does not change
            holdings1[i] <- holdings1[i - 1]
        } else {
            # sell all if RSI>50
            holdings1[i] <- 0
        }
    }
    reclass(holdings1, price)
}