Get startedGet started for free

Make irregular intra-day data regular

Earlier you learned how to create a regular daily series from irregular daily data. Now you will create regular intra-day data from an irregular series.

Intra-day financial data often does not span a full 24 hours. Most markets are usually closed part of the day. This exercise assumes markets open at 9AM and close at 4PM Monday-Friday.

Your data may not have an observation exactly at the market open and/or close. So, you would not be able to use start() and end() as you could with the daily data. You need to specify the start and end date-times to create this regular sequence.

The regular date-time sequence will include periods when markets are closed, but you can use xts' time-of-day subsetting to extract only the periods the market is open.

This exercise is part of the course

Importing and Managing Financial Data in R

View Course

Exercise instructions

  • Finish the command to create a regular 30-minute date-time sequence between 09:00 Monday and 16:00 Friday.
  • Set the values for x and order.by to create a zero-width xts object.
  • Create merged_xts by merging irregular_xts and regular_xts. Use the fill argument to replace NA with their previous value using na.locf().
  • Use xts' time-of-day subsetting to extract observations between 9AM and 4PM every day. Assign the result to trade_day.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a regular date-time sequence
regular_index <- seq(as.POSIXct("2010-01-04 __:__"), as.POSIXct("2010-01-08 __:__"), by = "30 min")

# Create a zero-width xts object
regular_xts <- xts(x = ___, order.by = ___)

# Merge irregular_xts and regular_xts, filling NA with their previous value
merged_xts <- merge(___, ___, fill = ___)

# Subset to trading day (09:00-16:00)
trade_day <- merged_xts[___]
Edit and Run Code