0
votes

I am trying to calculate the daily realised / historical volatility for a large dataset of equities by using the spotvol function from the highfrequency package. For some reason I keep getting POSIX (timestamp) errors and I have tried a number of different methods to convert my sample to an xts object.

A sample of the dataset:

2015-01-02 10:00:00  80.15
2015-01-02 11:00:00  81.00
2015-01-02 12:00:00  80.50
2015-01-02 13:00:00  80.40
2015-01-02 14:00:00  80.15
2015-01-02 15:00:00  81.00
2015-01-02 16:00:00  80.50
2015-01-05 10:00:00  80.40
2015-01-05 11:00:00  80.95
2015-01-05 12:00:00  81.05
2015-01-05 13:00:00  80.85
2015-01-05 14:00:00  80.50
2015-01-05 15:00:00  80.40
2015-01-05 16:00:00  80.95

The code that I used to convert to the xts object is as follows:

vol_df2 <- vol_df1 %>%
  mutate(time_stamp = as.POSIXct(time_stamp, format = "%Y-%m-%d %H:%M:%S"))

vol_df3 <- as.xts(vol_df2, order.by = vol_df2$time_stamp, tz = "", unique = F)

The code for trying to calculate the daily spotvol from 1 hour time buckets is as follows: NOTE: My actual data is of a high frequency nature, so I have provided aggregate data for each hour over 2 days.

  spotvol(vol_df3, method = "detper", on = "hours", k = 1,
          marketopen = "10:00:00", marketclose = "16:00:00")

The errors that I get are as follows:

Error in as.POSIXlt.character(x, tz, ...): 
character string is not in a standard unambiguous format
  do not know how to convert 'time' to class “POSIXct”
In addition: Warning messages:
1: All formats failed to parse. No formats found. 
2: All formats failed to parse. No formats found. 
3: In min.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to min; returning Inf
4: In max.default(numeric(0), na.rm = FALSE) :
  no non-missing arguments to max; returning -Inf
5: All formats failed to parse. No formats found.

and

Error in set(x, j = name, value = value) : 
Supplied 1054304 items to be assigned to 1054377 items of column
'PRICE'. If you wish to 'recycle' the RHS please use rep() to make this
intent clear to readers of your code.

The research I have done so far has turned up nothing as I have simply followed the instructions for converting my timestamp and also converting to the xts object. As I am running a Linux system I came across this: https://github.com/Rdatatable/data.table/issues/1619

I have also tried using the data.table method and this also produces the error regarding the unambiguous format of the POSIX timestamp.

It's like I am missing arguments in the commands for spotvol, however, I couldn't figure out whether there are defaults that can be changed from the documentation found here: https://www.rdocumentation.org/packages/highfrequency/versions/0.6.3/topics/spotvol

EDIT: The only error I am receiving now is the last one in the list regarding the Error in set...When I change fill=TRUE to fill=FALSE in the aggregatePrice part of the spotvol function the spotvol seems to want to calculate for a bit but still throws the Error in set error.

Any help on this would be greatly appreciated. Cheers :)

1

1 Answers

0
votes

The problem lies in this line of code:

vol_df3 <- as.xts(vol_df2, order.by = vol_df2$time_stamp, tz = "", unique = F)

If you look at what it contains it is this:

                    time_stamp            PRICE  
2015-01-02 10:02:00 "2015-01-02 10:02:00" "80.15"
2015-01-02 11:15:00 "2015-01-02 11:15:00" "81.00"
2015-01-02 12:31:00 "2015-01-02 12:31:00" "80.50"
2015-01-02 15:59:00 "2015-01-02 15:59:00" "80.40"
2015-01-05 10:01:00 "2015-01-05 10:01:00" "80.40"
2015-01-05 11:24:00 "2015-01-05 11:24:00" "80.95"
2015-01-05 14:21:00 "2015-01-05 14:21:00" "81.05"
2015-01-05 15:59:00 "2015-01-05 15:59:00" "80.85"

xts objects are a matrix and if you just select the whole data.frame including the timestamp it will be a character matrix. You need to exclude the timestamp column like this:

vol_df3 <- as.xts(vol_df2[, names(vol_df2) != "time_stamp"], 
                          order.by = vol_df2$time_stamp, tz = "", unique = F)

which results in this:

                     [,1]
2015-01-02 10:02:00 80.15
2015-01-02 11:15:00 81.00
2015-01-02 12:31:00 80.50
2015-01-02 15:59:00 80.40
2015-01-05 10:01:00 80.40
2015-01-05 11:24:00 80.95
2015-01-05 14:21:00 81.05
2015-01-05 15:59:00 80.85

And then spotvol will not return with an error.