R converting intraday tick dataframe to timesiers

别等时光非礼了梦想. 提交于 2021-02-17 03:01:03

问题


I have an intraday dataframe called SPX containing 5 minute tick data of the SPX index.

It is currently in a dataframe and I wish to convert it into a wonderful timeseries.

This is what it looks like currently:

   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

I have tried to convert it using the classic method

SPXintraday <- xts(SPX[,-1], order.by=as.Date(SPX[,1], "%Y-%m-%d %H:%M")) 

which produces the following error:

Error in as.Date.default(x, ...) :

do not know how to convert 'x' to class “Date”

this is what I would like to see respecting the hours and minutes.

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

Does anybody have some experience with this?

thank you in advance!

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"))

Full code:

#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"))

回答1:


As your data is already in a timestamp format, you can use

SPXintraday <- xts(SPX[,-1], order.by=SPX[,1])

or as you discovered

SPXintraday <- xts(SPX[,-1], order.by=SPX$timestamp, "%Y-%m-%d %H:%M")


来源:https://stackoverflow.com/questions/61412080/r-converting-intraday-tick-dataframe-to-timesiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!