使用quantmod:Function和for-loop在R中对交易策略进行回溯测试

使用quantmod:Function和for-loop在R中对交易策略进行回溯测试,r,quantmod,R,Quantmod,我正在使用R、quantmod和PerformanceAnalytics软件包。作为回溯测试策略的一部分,我试图创建一个信号/持股向量,根据RSI的价值告诉我是否应该买入/卖出/持有股票。如果RSI+2天:长度(价格)不是你所期望的:优先于+(请参见?语法)。其计算结果为day+(2:length(price))。您需要(第2天):长度(价格)。还要注意,xts对象是一个具有索引属性的矩阵,而矩阵只是一个具有dim属性的向量。对矩阵调用length,返回观察总数(向量的length)。您应该改用

我正在使用R、quantmod和PerformanceAnalytics软件包。作为回溯测试策略的一部分,我试图创建一个信号/持股向量,根据RSI的价值告诉我是否应该买入/卖出/持有股票。如果RSI+2天:长度(价格)不是你所期望的<代码>:优先于
+
(请参见
?语法
)。其计算结果为
day+(2:length(price))
。您需要
(第2天):长度(价格)
。还要注意,xts对象是一个具有
索引
属性的矩阵,而矩阵只是一个具有
dim
属性的向量。对矩阵调用
length
,返回观察总数(向量的
length
)。您应该改用
nrow


for
循环之前预先分配整个结果向量也是一种很好的做法。您当前的代码附加到
holdings
每次您调用
holdings[i]您的代码中都有拼写错误:
ris
不是
rsi
。您好,刚刚编辑过,它仍然不工作<代码>ret1Hi谢谢!:)成功了。我不太擅长区分像c()、length()和nrow()这样的东西,但现在我可以看出nrow()是更好的选择。我想我会通过更多的练习变得更好。再次感谢@Siangeeo:还要注意,在向量上调用
nrow
将返回
NULL
,因此如果不确定,使用
nrow
(和
NCOL
)更安全。
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)
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) 
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)
}