jonathancornelissen / highfrequency

The highfrequency package contains an extensive toolkit for the use of highfrequency financial data in R. It contains functionality to manage, clean and match highfrequency trades and quotes data. Furthermore, it enables users to: calculate easily various liquidity measures, estimate and forecast volatility, and investigate microstructure noise and intraday periodicity.
147 stars 63 forks source link

Errors with data.table for rMinRVar and rMPVar #100

Closed fcocquemas closed 11 months ago

fcocquemas commented 1 year ago

Hello,

Thanks for your hard work on the package! I've been trying to evaluate it for integrated variance estimations. I ran into a couple of errors with a data.table input, with rMinRVar and rMPVar but not with the other estimators.

library(data.table) # 1.14.8
library(highfrequency) # 1.0.0

df <- structure(list(
  DT = structure(
    c(1594128600, 1594128660, 1594128720, 
      1594128780, 1594128840, 1594128900, 1594128960, 1594129020, 1594129080, 
      1594129140, 1594129200, 1594129260, 1594129320, 1594129380, 1594129440, 
      1594129500, 1594129560, 1594129620, 1594129680, 1594129740), 
    class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
  RET = c(0, 0.0003862381, -0.0016121864, -0.0009737015, 0.002679348, 
          0.0021805763, 0.00092983246, -0.00041151047, -0.0017676353, 
          -0.00049209595, -0.0022382736, -0.0020422935, 0.003068924, 
          -0.00036001205, 0.00045347214, 0.0008120537, 0.00073194504, 
          -0.0011444092, 0.0015034676, 0.00050497055)
), 
row.names = c(NA, -20L), class = c("data.table", "data.frame")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
)

# Realized Variance (rRVar) works
rRVar(df, alignBy="minutes", alignPeriod=1)
# Out:
#            DT         RVar
# 1: 2020-07-07 4.398549e-05

# Median realized variance (rMedRVar) works
rMedRVar(df, alignBy="minutes", alignPeriod=1)
# Out:
#            DT     rMedRVar
# 1: 2020-07-07 5.658462e-05

# Minimum realized variance (rMinRVar) fails
rMinRVar(df, alignBy="minutes", alignPeriod=1)
# Out:
# Error: Not a matrix.

# Realized quadpower variance (rQPVar) works
rQPVar(df, alignBy="minutes", alignPeriod=1)
# Out:
#            DT          RET
# 1: 2020-07-07 2.342847e-09

# Realized multipower variance (rMPVar) fails
rMPVar(df, alignBy="minutes", alignPeriod=1)
# Out:
# Error in setnames(x, value) : 
#  Can't assign 2 names to a 1 column data.table

# Realized semivariance (rSVar) works
rSVar(df, alignBy="minutes", alignPeriod=1)
# Out:
# $`2020-07-07`
# $`2020-07-07`$rSVardownside
# [1] 1.770339e-05
# 
# $`2020-07-07`$rSVarupside
# [1] 2.62821e-05

Am I doing something wrong?

Converting to xts seems to solve the issue, but I'd rather not, if I can help it.

Thank you so much!

Best, François

onnokleen commented 1 year ago

Thanks for spotting this error. I found the bug and it will be fixed in the next release.

onnokleen commented 11 months ago

@kboudt Can you close this issue as the new version is on CRAN?

fcocquemas commented 11 months ago

Thank you for the fix!