I have 15-minute OHLC data and want to convert to daily OHLC
but with the start of the day at 17:00:00.
This way, the resulting daily bar should span from 17:00:00 to 17:00:00, not from 00:00:00 to 00:00:00
_
x <- zoo(runif(25), order.by=seq(
as.POSIXct("2010-05-03 17:00:00"),
as.POSIXct("2010-05-06 17:00:00"),
by="15 min"
)
)
_
head(x)
2010-05-03 17:00:00 0.9788685
2010-05-03 17:15:00 0.5414294
2010-05-03 17:30:00 0.8435366
2010-05-03 17:45:00 0.3064713
2010-05-03 18:00:00 0.1395849
2010-05-03 18:15:00 0.9916730
Using xts : Changing the periodicity from 15m to 60m works nicely:
x_agg <- to.minutes(x,k=60, indexAt="startof")
head(x_agg)
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.9788685 0.30647133 0.3064713
2010-05-03 18:00:00 0.1395849 0.9916730 0.09497550 0.5301038
2010-05-03 19:00:00 0.3580554 0.4264711 0.11728640 0.1172864
2010-05-03 20:00:00 0.9791394 0.9791394 0.01904849 0.1643573
2010-05-03 21:00:00 0.3096280 0.9193756 0.30962797 0.8896507
2010-05-03 22:00:00 0.8125618 0.8976714 0.74335042 0.7433504
Using xts : Changing the periodicity from 15m to 1440m = 1Day does not work:
x_agg <- to.minutes(x,k=1440, indexAt="startof")
head(x_agg)
_
x.Open x.High x.Low x.Close
2010-05-03 17:00:00 0.9788685 0.991673 0.01904849 0.38669801
2010-05-04 02:00:00 0.1172864 0.991673 0.01904849 0.09497550
2010-05-05 02:00:00 0.5301038 0.991673 0.01904849 0.84353659
2010-05-06 02:00:00 0.3064713 0.991673 0.01904849 0.01904849
I don't know why the index changes to 02:00:00 - it should be 17:00:00 for all days.
How can this be done - using xts ? Thank you for your effort.
dput(x_agg)
structure(c(0.97886852407828, 0.117286398308352, 0.530103818513453,
0.306471328716725, 0.991673005977646, 0.991673005977646, 0.991673005977646,
0.991673005977646, 0.0190484928898513, 0.0190484928898513, 0.0190484928898513,
0.0190484928898513, 0.386698011076078, 0.0949754973407835, 0.843536590691656,
0.0190484928898513), .Dim = c(4L, 4L), .Dimnames = list(NULL,
c("x.Open", "x.High", "x.Low", "x.Close")), index = structure(c(1272898800,
1272931200, 1273017600, 1273104000), class = c("POSIXct", "POSIXt"
), tzone = ""), class = "zoo")
Most (all?) xts functions define time intervals using the
endpoints()
function, which creates the intervals based on offsets from the epoch. This is usually desirable, because it finds/creates breaks at round units of time (at the change of a minute, hour, etc).If I understand correctly, you want intervals defined by the first observation in your data. I can see how you would expect endpoints at 17:00:00, since that's the hour and minute of your first observation. I'm not sure you would have the same expectation if your first observation was at an irregular time like 17:01:32.741.
So, that's the backstory, and therefore I can't think of an easy/straight-forward way to get the result you want. But here's an attempt at a kludge, using
period.apply()
with custom endpoints and function.Now a hack to get endpoints for days defined by 17:00.
Now you can use
ep
in your call toperiod.apply()
to get OHLC by days, where 17:00:00 is the breakpoint.