R XTS package to.minutes – Unable to create 15m an

2019-06-10 21:20发布

问题:

I am trying to use R XTS package to.minutes to create 15m and 30m time series from 5m. I have an xts object, which is date-time followed by OHLC. Info about xts object x is below:

head(x) shows the following:

                   High    Low   Open  Close Volume

2010-05-03 09:00:00 106.08 105.95 106.06 106.00 1055 2010-05-03 09:05:00 106.03 105.75 106.00 105.77 4369 2010-05-03 09:10:00 105.77 105.59 105.77 105.68 4125 2010-05-03 09:15:00 105.84 105.66 105.69 105.80 2457 2010-05-03 09:20:00 105.89 105.71 105.80 105.83 1788 2010-05-03 09:25:00 105.89 105.78 105.84 105.78 977

str(x) shows the following:

> str(x)

‘zoo’ series from 2010-05-03 09:00:00 to 2013-06-10 14:30:00 Data: num [1:222473, 1:5] 106 106 106 106 106 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "High" "Low" "Open" "Close" ... Index: POSIXct[1:222473], format: "2010-05-03 09:00:00" "2010-05-03 09:05:00" "2010-05-03 09:10:00" ...

str(head(index(x))) shows the following:

head(str(index(x)))

POSIXct[1:222473], format: "2010-05-03 09:00:00" "2010-05-03 09:05:00" "2010-05-03 09:10:00" ... NULL

When I convert the time series to 15m, the series starts at 09:10:00 followed by 15 minutes increments instead of starting at 09:15:00 followed by 15 minutes increments

> head(to.minutes(x, k=15))

                  x.Open x.High  x.Low x.Close x.Volume

2010-05-03 09:10:00 106.08 105.95 105.77 105.68 9549 2010-05-03 09:25:00 105.84 105.78 105.69 105.78 5222 2010-05-03 09:40:00 105.80 105.92 105.62 106.12 9727 2010-05-03 09:55:00 106.17 106.00 106.01 106.22 6320 2010-05-03 10:10:00 106.26 106.07 106.15 106.14 8422 2010-05-03 10:25:00 106.57 106.38 106.15 106.37 10422

The same issue when I convert the time series to 30m, the series starts at 09:25:00 followed by 30 minutes increments instead of starting at 09:30:00 followed by 30 minutes increments

> head(to.minutes(x, k=30))
                  x.Open x.High  x.Low x.Close x.Volume

2010-05-03 09:25:00 106.08 105.95 105.69 105.78 14771 2010-05-03 09:55:00 105.80 106.00 105.62 106.22 16047 2010-05-03 10:25:00 106.26 106.38 106.15 106.37 18844 2010-05-03 10:55:00 106.37 106.27 106.01 106.00 17193 2010-05-03 11:25:00 106.04 106.20 105.95 106.29 9075 2010-05-03 11:55:00 106.34 106.35 106.24 106.39 8517

I also tried the same using 1 minute data and had the same issue. Any thought on what might be causing this issue and how to resolve it? Thanks

回答1:

This is pretty clearly described on the ?to.minutes help page. The default is for the groups to start at the end of your data and work backwards so it doesn't necessarily pay attention to what the first value is. However, you can explicity set the indexAt= parameter to "startof". For example

x <- zoo(runif(25), order.by=seq(as.POSIXct("2010-05-03 09:00:00"), 
    as.POSIXct("2010-05-03 11:00:00"), by="5 min"))

to.minutes15(x)

#                         x.Open    x.High      x.Low    x.Close
# 2010-05-03 09:10:00 0.35570172 0.3557017 0.04524480 0.04524480
# 2010-05-03 09:25:00 0.78939084 0.7893908 0.44032175 0.44032175
# 2010-05-03 09:40:00 0.05272398 0.5381755 0.05272398 0.53817548
# 2010-05-03 09:55:00 0.02198503 0.1113298 0.02198503 0.11132980
# 2010-05-03 10:10:00 0.78785210 0.8804505 0.04152860 0.04152860
# 2010-05-03 10:25:00 0.79317091 0.9497044 0.54751546 0.94970444
# 2010-05-03 10:40:00 0.03886176 0.7425681 0.03886176 0.06614893
# 2010-05-03 10:55:00 0.58684500 0.5868450 0.02794687 0.14291696
# 2010-05-03 11:00:00 0.11713868 0.1171387 0.11713868 0.11713868

versus

to.minutes15(x, indexAt="startof")

#                         x.Open    x.High      x.Low    x.Close
# 2010-05-03 09:00:00 0.35570172 0.3557017 0.04524480 0.04524480
# 2010-05-03 09:15:00 0.78939084 0.7893908 0.44032175 0.44032175
# 2010-05-03 09:30:00 0.05272398 0.5381755 0.05272398 0.53817548
# 2010-05-03 09:45:00 0.02198503 0.1113298 0.02198503 0.11132980
# 2010-05-03 10:00:00 0.78785210 0.8804505 0.04152860 0.04152860
# 2010-05-03 10:15:00 0.79317091 0.9497044 0.54751546 0.94970444
# 2010-05-03 10:30:00 0.03886176 0.7425681 0.03886176 0.06614893
# 2010-05-03 10:45:00 0.58684500 0.5868450 0.02794687 0.14291696
# 2010-05-03 11:00:00 0.11713868 0.1171387 0.11713868 0.11713868


标签: r xts