csv 数据转换为 xts

csv data convert to xts

我有一个 csv 文件,其中包含 2016 年 nasdaq100 的 1 分钟数据。我想将其转换为 xts 以使用 quantstrat。导入后它看起来像这样:

          date     open     high      low    close volume adjusted
         <chr>    <dbl>    <dbl>    <dbl>    <dbl>  <int>    <int>
1 04.01.2016 14:30 48963818 48964272 48952363 48958789      0        0
2 04.01.2016 14:31 48923579 48940259  4891752 48940259      0        0
3 04.01.2016 14:32 48941753 48992466 48941753 48988589      0        0
4 04.01.2016 14:33 48992227 48992227 48948281 48965469      0        0
5 04.01.2016 14:34 48962915  4896418 48923838 48934326      0        0
6 04.01.2016 14:35 48931196 48963301 48931196 48954341      0        0

我使用代码

NASD_xts = xts(NASD, order.by=as.POSIXct(NASD$date, format="%d-%m-%y %H:%M")) 

得到这个结果。

     date               open       high       low        close      volume       adjusted
<NA> "04.01.2016 14:30" "48963818" "48964272" "48952363" "48958789" "         0" "0"     
<NA> "04.01.2016 14:31" "48923579" "48940259" " 4891752" "48940259" "         0" "0"     
<NA> "04.01.2016 14:32" "48941753" "48992466" "48941753" "48988589" "         0" "0"     
<NA> "04.01.2016 14:33" "48992227" "48992227" "48948281" "48965469" "         0" "0"     
<NA> "04.01.2016 14:34" "48962915" " 4896418" "48923838" "48934326" "         0" "0"     
<NA> "04.01.2016 14:35" "48931196" "48963301" "48931196" "48954341" "         0" "0"    

我的问题是第一行的NA值,应该是时间。所以我没有得到正确的 xts 来处理 quantstrat。

xts 应用于 'date' 列和 order.by 与 'date'

除外的列
library(xts)
library(lubridate)
xts(NASD[-1], order.by = mdy_hm(NASD$date))
#                        open     high      low    close volume adjusted
#2016-04-01 14:30:00 48963818 48964272 48952363 48958789      0        0
#2016-04-01 14:31:00 48923579 48940259  4891752 48940259      0        0
#2016-04-01 14:32:00 48941753 48992466 48941753 48988589      0        0
#2016-04-01 14:33:00 48992227 48992227 48948281 48965469      0        0
#2016-04-01 14:34:00 48962915  4896418 48923838 48934326      0        0
#2016-04-01 14:35:00 48931196 48963301 48931196 48954341      0        0

数据

NASD <- structure(list(date = c("04.01.2016 14:30", "04.01.2016 14:31", 
"04.01.2016 14:32", "04.01.2016 14:33", "04.01.2016 14:34", "04.01.2016 14:35"
), open = c(48963818L, 48923579L, 48941753L, 48992227L, 48962915L, 
48931196L), high = c(48964272L, 48940259L, 48992466L, 48992227L, 
4896418L, 48963301L), low = c(48952363L, 4891752L, 48941753L, 
48948281L, 48923838L, 48931196L), close = c(48958789L, 48940259L, 
48988589L, 48965469L, 48934326L, 48954341L), volume = c(0L, 0L, 
0L, 0L, 0L, 0L), adjusted = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("date", 
"open", "high", "low", "close", "volume", "adjusted"), row.names = c("1", 
"2", "3", "4", "5", "6"), class = c("tbl_df", "tbl", "data.frame"
))

您对 as.POSIXctformat 参数不正确。而且你不应该在你的 xts 对象中包含 date 列,因为日期时间已经包含在索引属性中,并且 xts 对象只能包含一个类型(因为它们是下面的矩阵)。

包括 date 列是导致 xts 对象中的其余列为字符的原因。由于 xts 对象只能包含单一类型,因此 data.frame 的所有列都被强制转换为通用类型(在本例中为字符)。

你的命令应该是:

NASD_xts <- xts(NASD[,-1],
                order.by = as.POSIXct(NASD$date, format = "%d.%m.%Y %H:%M"))

请注意,格式假定 date 列指定为月、日、年。您提供的示例数据中的月份和日期不明确。所以真正的格式可以是日、月、年(意思是日期可以是一月四日,也可以是四月一日)。