使用 for 循环为许多股票估计一夜之间 returns,并将其存储在以股票名称作为列名称的数据框中

Estimate overnight returns for many stocks using a for loop and store it in a dataframe with stock names as column names

我正在尝试使用 for 循环估计一夜之间 returns 许多股票,并将其存储在数据框中,以股票名称作为列名称。 trade 有原始的日内数据,trade2 有清理过的日内数据。 list.namess 有股票名称。这是我的代码:

require(xts)
require(highfrequency)
OvernightRet<-list()
list.namess<- list.files(pattern="*.IS Equity")
list.namess<- list.namess[2]
for(Q in 1:length(list.namess)){
  trade<-readRDS(list.namess[Q])
  trade<-xts(trade[,-1], order.by = trade[,1])
  colnames(trade)[c(1,2)]<-c("PRICE", "SIZE")
  #Unduplicating
  trade2<-do.call(rbind, lapply(split(trade,"days"), mergeTradesSameTimestamp))
  trade2<-trade2[,1]

  fun.first= function(x) first(x)
  fun.last= function(x) last(x)
  A=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.first))
  B=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.last))
  OvernightRetA <- (as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))
  colnames(OvernightRetA)<-list.namess[Q]
  OvernightRet[[Q]]<-OvernightRetA
}
df.OvernightRet<-do.call(merge, OvernightRet)

但是,它给出了错误,可能是因为无法重命名 OvernightRetA:

    Error in `colnames<-`(`*tmp*`, value = "ACEM IS Equity.rds") : 
      attempt to set 'colnames' on an object with less than two dimensions
    In addition: There were 50 or more warnings (use warnings() to see the first 50)
    > df.OvernightRet<-do.call(merge, OvernightRet)

Error in as.data.frame(x) : argument "x" is missing, with no default

因为 trade 和 trade2 很大,不适合 dput。我正在发布给定的 Open(A)、Close(B) 和名称列表 (list.namess) 以重现错误。

dput(head(A,10))
structure(c(231.9, 236.35, 230, 226.85, 229.05, 225.7, 226.95, 
224.55, 227, 234.65), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459481850, 
1459741066, 1459827433, 1459913867, 1460000236, 1460086630, 1460345867, 
1460432285, 1460518631, 1460950628), tzone = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))


dput(head(B,10))
structure(c(235.35, 231.2, 226.1, 229.05, 226.45, 225.75, 224.55, 
223.75, 231.1, 228.6), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459508732, 
1459767943, 1459854348, 1459940748, 1460027143, 1460113538, 1460374518, 
1460465873, 1460545568, 1460977541), tzone = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))

dput(list.namess) "ACEM IS Equity.rds"

请帮我解决这个错误。

我认为问题正如错误消息所暗示的那样,是您试图将列 header 分配给单个值。您可以通过将上面的行更改为:

来解决此问题

OvernightRetA <- as.data.frame(as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))