R 将日内刻度数据帧转换为 timesiers
R converting intraday tick dataframe to timesiers
我有一个名为 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"))
产生以下错误:
Error in as.Date.default(x, ...) :
do not know how to convert 'x' to class “Date”
这是我希望看到的关于小时和分钟的内容。
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(头(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")
我有一个名为 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"))
产生以下错误:
Error in as.Date.default(x, ...) :
do not know how to convert 'x' to class “Date”
这是我希望看到的关于小时和分钟的内容。
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(头(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")