0

I start by importing a dataset from Excel. My code is then as follows (BTC1 is what I have called the dataset when I imported it).

# Convert column `Date` to date
BTC1$Date <- as.POSIXlt(BTC1$Date,format="%Y:%M:%D", origin = "1899-12-30")

as.xts(BTC1$Date, order.by = BTC1$Date)
BTC_XTS1a <- xts(BTC1, order.by = as.Date(BTC1$Date, 1:731))


BTCRet <- CalculateReturns(BTC_XTS1a, -7)

This is what comes up:

Error in xtsAttributes(Returns) : object 'Returns' not found

Does anybody have any advice on where I am going wrong? I don't understand why 'Returns' are not found (the PerformanceAnalytics package is installed and called).

Thanks

Edit: Here is an example of the data:

dput(head(BTC1, 10))
structure(list(Date = structure(c(17897, 17898, 17899, 17900, 17901, 17902, 17903, 17904, 17905, 17906), class = "Date"), Price = c(3809.4, 3873.8, 3780.1, 3802.7, 3785.4, 4004.1, 3985.9, 3971, 3978, 3603.7 ), Open = c(3709.5, 3809.7, 3874.1, 3780.5, 3802.8, 3785.7, 4004.3, 3985.8, 3971, 3978.9), High = c(3814.3, 3894.8, 3875.8, 3823.9, 3846.7, 4034.1, 4028, 4070.5, 4014.2, 4007.7), Low = c(3664.4, 3768.1, 3753, 3720, 3769, 3758.5, 3945.5, 3943.1, 3957.5, 3562.1 ), Vol = c(469110, 554470, 450320, 488700, 468450, 529890, 500880, 514080, 473420, 697310), Change.% = c(0.0269, 0.0169, -0.0242, 0.006, -0.0046, 0.0578, -0.0045, -0.0037, 0.0017, -0.0941), Range = c(149.9, 126.7, 122.8, 103.9, 77.6999999999998, 275.6, 82.5, 127.4, 56.6999999999998, 445.6), Number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), row.names = c(NA, 10L), class = "data.frame")

  • Please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by giving us an example of your data with `dput(head(BTC1, 10))`. – phiver Feb 25 '21 at 12:12
  • Hi Phiver - I have updates the OP to include that. Thank you :) – James Tucker Feb 25 '21 at 17:22

1 Answers1

0

There are 2 issues. The first issue relates to the inclusion of the Date column when you create an xts object. This would create a character matrix instead of a numeric matrix. The Second issue is that the CalculateReturns function only needs an xts object and a method. The -7 doesn't belong here. If you want a period return, you need to use the function periodReturn from quantmod.

library(xts)
library(quantmod)
library(PerformanceAnalytics)

# 1. remove date column from the data frame when creating xts object. Date column goes to order.by
BTC_XTS1a <- xts(BTC1[, -1], order.by = as.Date(BTC1$Date))

# 2. remove the -7 from the CalculateReturns function.
BTCRet <- CalculateReturns(BTC_XTS1a)

data:

BTC1 <- structure(list(Date = structure(c(17897, 17898, 17899, 17900, 17901, 17902, 17903, 17904, 17905, 17906), 
                                        class = "Date"), 
                       Price = c(3809.4, 3873.8, 3780.1, 3802.7, 3785.4, 4004.1, 3985.9, 3971, 3978, 3603.7 ), 
                       Open = c(3709.5, 3809.7, 3874.1, 3780.5, 3802.8, 3785.7, 4004.3, 3985.8, 3971, 3978.9), 
                       High = c(3814.3, 3894.8, 3875.8, 3823.9, 3846.7, 4034.1, 4028, 4070.5, 4014.2, 4007.7),
                       Low = c(3664.4, 3768.1, 3753, 3720, 3769, 3758.5, 3945.5, 3943.1, 3957.5, 3562.1 ), 
                       Vol = c(469110, 554470, 450320, 488700, 468450, 529890, 500880, 514080, 473420, 697310), 
                       `Change.%` = c(0.0269, 0.0169, -0.0242, 0.006, -0.0046, 0.0578, -0.0045, -0.0037, 0.0017, -0.0941), 
                       Range = c(149.9, 126.7, 122.8, 103.9, 77.6999999999998, 275.6, 82.5, 127.4, 56.6999999999998, 445.6), 
                       Number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), 
                  row.names = c(NA, 10L), 
                  class = "data.frame")
phiver
  • 23,048
  • 14
  • 44
  • 56