AlphaVantager时间序列设置YYYY MM DD HH MM SS

时间:2017-11-11 19:44:44

标签: r time-series quantmod

我使用R中的alphavantager包下载了数据。我正在尝试将分钟数据设置为时间序列xts对象。

rm(list = ls())

library(alphavantager)

AlphaKey <- av_api_key("YOUR API Key (Free) - (obtainable here: https://www.alphavantage.co/support/#api-key)  ")
print(AlphaKey)

args(av_get)

GOOG <- av_get(symbol = "GOOG", av_fun = "TIME_SERIES_INTRADAY", interval = "1min", outputsize = "full")
plot(GOOG$close)   

close_price <- GOOG$close

times <- as.POSIXct(GOOG$timestamp, format="%d/%m/%Y %H:%M")
times
times <-format(GOOG$timestamp, format="%H:%M:%S")
df <- cbind(times, close_price)

当我运行is.xts(df)时,我得到一个输出FALSE。我正在尝试使用以下格式将GOOG$timestamp设置为xts对象:YYYY MM DD HH MM SS,以便我可以从chartSeries包中运行quantmod

head(GOOG$timestamp)

[1] "2017-10-30 09:30:00 UTC"
[2] "2017-10-30 09:31:00 UTC"
[3] "2017-10-30 09:32:00 UTC"
[4] "2017-10-30 09:33:00 UTC"
[5] "2017-10-30 09:34:00 UTC"
[6] "2017-10-30 09:35:00 UTC"
  

我怎样才能在R?

中这样做

编辑:我从chartSeries函数得到的错误:

chartSeries(df, TA=NULL)

> Error in try.xts(x, error = "chartSeries requires an xtsible object") : 
  chartSeries requires an xtsible object

1 个答案:

答案 0 :(得分:2)

quantmod不同,alphavantager不会将数据转换为xts对象。因此,你必须自己做。这应该有效:

df <- xts(GOOG[,-1], order.by = as.POSIXct(GOOG$timestamp))
相关问题