我是“ R”的新手,我想编写一个脚本来回测根据EMA的买卖策略。我根据来自Web的一些参考编写以下代码。但是,该脚本在第72行出现了错误消息,但我无法弄清楚问题所在。有人可以帮助解决我的问题吗?提前致谢。

library(quantmod)
stock1<-getSymbols("^DJI",src="yahoo",from="2010-01-01",auto.assign=F)


stock1<-na.locf(stock1)
stock1$EMA9<-EMA(Cl(stock1),n=9)
stock1$EMA19<-EMA(Cl(stock1),n=19)
stock1$EMACheck<-ifelse(stock1$EMA9>stock1$EMA19,1,0)
stock1$EMA_CrossOverUp<-ifelse(diff(stock1$EMACheck)==1,1,0)
stock1$EMA_CrossOverDown<-ifelse(diff(stock1$EMACheck)==-1,-1,0)


stock1<-stock1[index(stock1)>="2010-01-01",]

stock1_df<-data.frame(index(stock1),coredata(stock1))

colnames(stock1_df)<-c("Date","Open","High","Low","Close","Volume","Adj","EMA9","EMA19","EMACheck","EMACheck_up","EMACheck_down")
head(stock1_df)

#To calculate the number of crossoverup transactions during the duration from 2010-01-01

sum(stock1_df$EMACheck_up==1 & index(stock1)>="2010-01-01",na.rm=T)

stock1_df$Date[stock1_df$EMACheck_up==1 & index(stock1)>="2010-01-01"]

sum(stock1_df$EMACheck_down==-1 & index(stock1)>="2010-01-01",na.rm=T)

stock1_df$Date[stock1_df$EMACheck_down==-1 & index(stock1)>="2010-01-01"]

#To generate the transcation according to the strategy

transaction_dates<-function(stock2,Buy,Sell)
{
Date_buy<-c()
Date_sell<-c()
hold<-F
stock2[["Hold"]]<-hold
for(i in 1:nrow(stock2)) {
  if(hold == T) {
    stock2[["Hold"]][i]<-T
    if(stock2[[Sell]][i] == -1) {
      #stock2[["Hold"]][i]<-T
      hold<-F
    }
  } else {
    if(stock2[[Buy]][i] == 1) {
      hold<-T
      stock2[["Hold"]][i]<-T
    }
  }
}


stock2[["Enter"]]<-c(0,ifelse(diff(stock2[["Hold"]])==1,1,0))
stock2[["Exit"]]<-c(ifelse(diff(stock2[["Hold"]])==-1,-1,0),0)

Buy_date <- stock2[["Date"]][stock2[["Enter"]] == 1]
Sell_date <- stock2[["Date"]][stock2[["Exit"]] == -1]

if (length(Sell_date)<length(Buy_date)){
  #Sell_date[length(Sell_date)+1]<-tail(stock2[["Date"]],n=2)[1]
  Buy_date<-Buy_date[1:length(Buy_date)-1]

}

return(list(DatesBuy=Buy_date,DatesSell=Sell_date))
}

#transaction dates generate:

transactionDates<-transaction_dates(stock1_df,"EMACheck_up","EMACheck_down")

transactionDates

num_transaction1<-length(transactionDates[[1]])

Open_price<-function(df,x) {df[as.integer(rownames(df[df[["Date"]]==x,]))+1,][["Open"]]}
transactions_date<-function(df,x) {df[as.integer(rownames(df[df[["Date"]]==x,]))+1,][["Date"]]}

transactions_generate<-function(df,num_transaction)
{
price_buy<-sapply(1:num_transaction,function(x) {Open_price(df,transactionDates[[1]][x])})
price_sell<-sapply(1:num_transaction,function(x) {Open_price(df,transactionDates[[2]][x])})
Dates_buy<-as.Date(sapply(1:num_transaction,function(x) {transactions_date(df,transactionDates[[1]][x])}))
Dates_sell<-as.Date(sapply(1:num_transaction,function(x) {transactions_date(df,transactionDates[[2]][x])}))


transactions_df<-data.frame(DatesBuy=Dates_buy,DatesSell=Dates_sell,pricesBuy=price_buy,pricesSell=price_sell)
#transactions_df$return<-100*(transactions_df$pricesSell-transactions_df$pricesBuy)/transactions_df$pricesBuy
transactions_df$Stop_loss<-NA
return(transactions_df)
}

transaction_summary<-transactions_generate(stock1_df,num_transaction1)
transaction_summary$Return<-100*(transaction_summary$pricesSell-transaction_summary$pricesBuy)/transaction_summary$pricesBuy
transaction_summary

最佳答案

您的代码在此行失败:

transactionDates<-transaction_dates(stock1_df,"EMACheck_up","EMACheck_down")

原因是stock1_df的前19条记录在“ EMACheck_up”和“ EMACheck_down”列中包含NA值。

head(stock1_df)
  EMACheck_up EMACheck_down
1          NA            NA
2          NA            NA
3          NA            NA
4          NA            NA
5          NA            NA
6          NA            NA


您可以通过在运行有问题的代码行之前运行na.locf来解决您的问题。

stock1_df <- na.locf(stock1_df)

transactionDates <-
  transaction_dates(stock1_df, "EMACheck_up", "EMACheck_down")


跳过前19行(或第一个月)也可以。

如果您想在回测策略中做更多的事情,则可能需要研究quantstrat。但是,您现在所拥有的就可以了。

关于r - 使用R进行库存分析的回测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52227176/

10-11 05:01