我有一个称为SPX的日内数据框,其中包含SPX索引的5分钟刻度数据。
它当前在一个数据框中,我希望将其转换为一个美妙的时间序列。
这是当前的样子:
timestamp open high low close volume
1 2020-04-03 09:35:00 2516. 2524. 2513. 2522. 0
2 2020-04-03 09:40:00 2523. 2528. 2519. 2528. 45796799
3 2020-04-03 09:45:00 2528. 2538. 2526. 2533. 46888484
4 2020-04-03 09:50:00 2533. 2535. 2527. 2528 37476420
5 2020-04-03 09:55:00 2528. 2530. 2518 2523. 39367782
6 2020-04-03 10:00:00 2523. 2531. 2522. 2526. 33511443
我尝试使用经典方法进行转换
SPXintraday <- xts(SPX[,-1], order.by=as.Date(SPX[,1], "%Y-%m-%d %H:%M"))
产生以下错误:
as.Date.default(x,...)中的错误:
不知道如何将“ x”转换为“日期”类
这是我希望尊重小时和分钟的内容。
timestamp open high low close volume
2020-04-03 09:35:00 2516. 2524. 2513. 2522. 0
2020-04-03 09:40:00 2523. 2528. 2519. 2528. 45796799
2020-04-03 09:45:00 2528. 2538. 2526. 2533. 46888484
2020-04-03 09:50:00 2533. 2535. 2527. 2528 37476420
2020-04-03 09:55:00 2528. 2530. 2518 2523. 39367782
2020-04-03 10:00:00 2523. 2531. 2522. 2526. 33511443
有人对此有经验吗?
先感谢您!
dput(head(SPX,10)):
structure(list(timestamp = structure(c(1585906500, 1585906800,
1585907100, 1585907400, 1585907700, 1585908000, 1585908300, 1585908600,
1585908900, 1585909200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), open = c(2515.98, 2523.1399, 2527.8401, 2533.0701,
2527.8401, 2522.6499, 2526.3101, 2523.5601, 2529.73, 2525.25),
high = c(2524.45, 2527.9399, 2538.1799, 2535.24, 2530.28,
2531.02, 2533.4399, 2533.75, 2530.1899, 2525.25), low = c(2513.1201,
2519.1101, 2526.46, 2526.99, 2518, 2522.27, 2523.3501, 2523.24,
2524.28, 2511.3), close = c(2522.47, 2527.8701, 2532.99,
2528, 2522.71, 2526.1399, 2523.6799, 2529.8501, 2525.1899,
2512.05), volume = c(0, 45796799, 46888484, 37476420, 39367782,
33511443, 36109023, 31197642, 30499476, 40302400)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
完整代码:
#AlphaVantage
library("alphavantager")
library("xts")
#retrieve data
av_api_key("KEY")
SPX <- av_get(symbol = "SPX", av_fun = "TIME_SERIES_INTRADAY", interval = "5min", outputsize = "full")
#Convert to time series
SPXintraday <- ts(SPX[,-1], order.by=as.Date(SPX[,1], "%Y-%m-%d %H:%M"))
由于您的数据已经是时间戳格式,因此您可以使用
SPXintraday <- xts(SPX[,-1], order.by=SPX[,1])
或如你所发现
SPXintraday <- xts(SPX[,-1], order.by=SPX$timestamp, "%Y-%m-%d %H:%M")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句