Dear Stack overflow community, I am trying to compute the returns of an asset using periodReturn's quantmod functions:
HLCtest is a "High Low Close" object, head(HLCtest) gives:
## Date High low Close
##1 1991-01-01 GMT 1517.93 1517.93 1517.93
##2 1991-01-02 GMT 1509.58 1487.96 1505.10
##3 1991-01-03 GMT 1540.22 1500.54 1539.50
##4 1991-01-04 GMT 1552.15 1533.77 1547.66
##5 1991-01-07 GMT 1524.38 1501.26 1507.87
##6 1991-01-08 GMT 1507.37 1474.79 1500.24
> yearlyReturn(HLCtest, subset=NULL, type='arithmetic', leading=TRUE)
I get the following error message:
> Error in try.xts(x) :
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
I used:
> strftime(HLCtest$Date, format ="", tz="GMT", usetz = TRUE)
to make sure the date format was ISO8601 standard.
and also:
> is.HLC(HLCtest)
to make sure the object was "High Low Close" object.
Could someone please tell me what is this error message telling me and how to fix it?
yearlyReturn
doesn't know how to convert your data.frame to an xts object. This is because it uses try.xts
to attempt to convert it to an xts object, and try.xts
expects the data.frame row names to contain the dates.
The easiest solution is to create an xts object yourself, and then pass that to yearlyReturn
.
# sample data
y <- structure(list(Date = structure(c(7670, 7671, 7672, 7673, 7676,
7677), class = "Date"), High = c(1517.93, 1509.58, 1540.22, 1552.15,
1524.38, 1507.37), low = c(1517.93, 1487.96, 1500.54, 1533.77,
1501.26, 1474.79), Close = c(1517.93, 1505.1, 1539.5, 1547.66,
1507.87, 1500.24)), .Names = c("Date", "High", "low", "Close"),
row.names = c(NA, -6L), class = "data.frame")
# make xts object
x <- xts(y[,-1], y[,1])
# call *lyReturn
dailyReturn(x)
# daily.returns
# 1991-01-01 0.000000000
# 1991-01-02 -0.005500912
# 1991-01-03 0.020297036
# 1991-01-04 0.007745647
# 1991-01-07 -0.017891312
# 1991-01-08 -0.011158635