2
$\begingroup$

Turning here since I haven't been able to find any help online.

I'm currently dealing with a high-frequency data set in R of some stock indicies and I'm currently trying to clean the data so that I will be able to work with it.

So, to get to the problem, I'm trying to incorporate the overnight returns of the indicies (i.e. prices in between 15:00 and 09:00 the following day) as done by Hansen & Lunde (2005) - that is, taking the median of the overnight prices and storing it as the 09:00 observation the next day.

Does anyone have any R-code on how to deal with this problem?

PS. I have been able to store the overnight median price as the 23:00-observation the previous day (i.e. the day that the overnight trading started). However, I haven't been able to change the date and time of these observations so that they end up at 09:00 the next day. An example is listed below:

2006-12-29 14:30:00 17320.00 2006-12-29 14:35:00 17315.00 2006-12-29 14:40:00 17315.00 2006-12-29 14:45:00 17322.50 2006-12-29 15:00:00 17325.00 2006-12-29 23:00:00 17321.25 <- THIS OBSERVATION I WOULD LIKE TO END UP AT 09:00 THE NEXT DAY

$\endgroup$
2
$\begingroup$

Does anyone have any R-code on how to deal with this problem?

It depends on what data structures you are using, but one approach is the following:

  1. Define a two-column array to hold date and time separately. This array will have as many rows as your current array. Alternatively, cbind() these two columns to your array.
  2. Define an array distinct_dates that will only have distinct dates in it (levels() is useful here).
  3. In the two-column array, set the date field to the next day --which you will get from distinct_dates-- in rows where timestamp starts with '23:'.
  4. In the two-column array, set the timestamp field to 09:00 in rows where timestamp starts with '23:'.
  5. Set the values of original date-timestamp column by merging the fields you processed in steps 3 & 4.
  6. Cleanup: Remove the two temporary columns (if you cbind-ed them to your original array) or delete the two-column array, accordingly.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.