【问题标题】:How do I convert 15-minute data to time series in datetime format so I can plot it using quantmod?如何将 15 分钟数据转换为日期时间格式的时间序列,以便我可以使用 quantmod 绘制它?
【发布时间】:2016-10-18 08:25:21
【问题描述】:

所以我得到了想要转换为 xts 的数据,以便我可以使用 quantmod 绘制它。 (是15m图表)

 > sample
                    Date   Open   High   Low  Close Vol.at.Price Volume
515 2016-06-15 pm 1:15:00 1.7381 1.7600 1.710 1.7399       1.7399 176000
516 2016-06-15 pm 1:30:00 1.7389 1.7400 1.725 1.7350       1.7350 152900
517 2016-06-15 pm 1:45:00 1.7350 1.7650 1.720 1.7550       1.7550 179900
518 2016-06-15 pm 2:00:00 1.7550 1.7600 1.740 1.7500       1.7500 130800
519 2016-06-15 pm 2:15:00 1.7550 1.7800 1.745 1.7800       1.7800 188400
520 2016-06-15 pm 2:30:00 1.7800 1.7899 1.730 1.7300       1.7300 256700
521 2016-06-15 pm 2:45:00 1.7300 1.7800 1.730 1.7664       1.7664 151900
522 2016-06-15 pm 3:00:00 1.7600 1.7700 1.740 1.7600       1.7600  74100
523 2016-06-15 pm 3:15:00 1.7501 1.8000 1.750 1.7850       1.7850 232300
524 2016-06-15 pm 3:30:00 1.7900 1.8200 1.760 1.7600       1.7600 183300
525 2016-06-15 pm 3:45:00 1.7600 1.7700 1.730 1.7400       1.7400 151600
526 2016-06-15 pm 4:00:00 1.7400 1.7599 1.650 1.7150       1.7150 443500
527 2016-06-15 pm 4:15:00 1.7100 1.7100 1.710 1.7100       1.7100   4628
528 2016-06-15 pm 6:30:00 1.7100 1.7100 1.710 1.7100       1.7100   2000
529 2016-06-15 pm 7:00:00 1.6900 1.6900 1.680 1.6900       1.6900   1050
530 2016-06-15 pm 8:00:00 1.6900 1.6900 1.690 1.6900       1.6900    100
> class(sample)
   [1] "data.frame"

起初,我认为使用 xts 函数可以完成这项工作,但后来我意识到它只是减少了几分钟的部分。

> sampled.after<-xts(sample[2:ncol(sample)],as.Date(sample$Date))
> sampled.after
             Open   High   Low  Close Vol.at.Price Volume
2016-06-15 1.7381 1.7600 1.710 1.7399       1.7399 176000
2016-06-15 1.7389 1.7400 1.725 1.7350       1.7350 152900
2016-06-15 1.7350 1.7650 1.720 1.7550       1.7550 179900
2016-06-15 1.7550 1.7600 1.740 1.7500       1.7500 130800
2016-06-15 1.7550 1.7800 1.745 1.7800       1.7800 188400
2016-06-15 1.7800 1.7899 1.730 1.7300       1.7300 256700
2016-06-15 1.7300 1.7800 1.730 1.7664       1.7664 151900
2016-06-15 1.7600 1.7700 1.740 1.7600       1.7600  74100
2016-06-15 1.7501 1.8000 1.750 1.7850       1.7850 232300
2016-06-15 1.7900 1.8200 1.760 1.7600       1.7600 183300
2016-06-15 1.7600 1.7700 1.730 1.7400       1.7400 151600
2016-06-15 1.7400 1.7599 1.650 1.7150       1.7150 443500
2016-06-15 1.7100 1.7100 1.710 1.7100       1.7100   4628
2016-06-15 1.7100 1.7100 1.710 1.7100       1.7100   2000
2016-06-15 1.6900 1.6900 1.680 1.6900       1.6900   1050
2016-06-15 1.6900 1.6900 1.690 1.6900       1.6900    100

如何将其转换为时间序列以便绘制蜡烛图?

【问题讨论】:

  • as.Date 仅返回时间戳的日期部分。使用as.POSIXct 保留完整的时间戳。
  • 除了显示 EDT 外,它仍然给我相同的结果。有没有其他办法?

标签: r datetime timestamp time-series quantmod


【解决方案1】:

as.Date 仅返回时间戳的日期部分。使用as.POSIXct 保留完整的时间戳。此外,您需要提供format 参数,以便as.POSIXct 理解您的日期格式。如果没有 format 参数,它只会将时间戳截断为日期部分。以下是几个例子:

as.Date 仅返回日期

as.Date("2016-06-15 pm 1:15:00")  
[1] "2016-06-15"  

as.POSIXct 不带 format 参数返回日期加上默认时区

as.POSIXct("2016-06-15 pm 1:15:00")  
[1] "2016-06-15 PDT"

将正确的format 添加到as.POSIXct 以获得完整的时间戳

as.POSIXct("2016-06-15 pm 1:15:00", format="%Y-%m-%d %p %I:%M:%S")
[1] "2016-06-15 13:15:00 PDT"

有关如何解释上述format 参数的详细信息,请参阅strptime 的帮助。

将正确的formattz 添加到as.POSIXct 以获得完整的时间戳和正确的时区

请注意,as.POSIXct 还将我的本地时区添加到结果 (PDT) 中。因此,您可能还应该添加一个时区参数以确保应用正确的时区(特别是如果您有来自不同时区的交易所的报价)。例如:

as.POSIXct("2016-06-15 pm 1:15:00", format="%Y-%m-%d %p %I:%M:%S", tz="America/New_York")
[1] "2016-06-15 13:15:00 EDT" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 2021-08-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2014-09-13
    相关资源
    最近更新 更多