phenance.com
Stock Data Pages | TCA League Tables | About

Analyzing Bitcoin in R - Part 5 - Manipulating the data frequency

Pub: 04.12.17

| By Anonymous

In Finance.

Tags: coding r bitcoin analysis .

For high, low, and close it is easy to select the maximum, minimum, and last values of each day:

#High
rawdailymax <- period.apply(rawdata$USD, INDEX = endpoints(rawdata, "days"), FUN = max)
indexClass(rawdailymax) <- "Date"
#Low
rawdailymin <- period.apply(rawdata$USD, INDEX = endpoints(rawdata, "days"), FUN = min)
indexClass(rawdailymin) <- "Date"
#Close
rawdailyclose <- rawdata$USD[endpoints(rawdata, "days")]
indexClass(rawdailyclose) <- "Date"

To get the open values of each day, we have to hack around a bit with a function (there may be better ways to do this):

#Open
startpoints <- function (x, on = "months", k = 1) {
  head(endpoints(x, on, k) + 1, -1)
}
rawdailyopen <- rawdata$USD[startpoints(rawdata, "days")]
indexClass(rawdailyopen) <- "Date"

Due to issues with date timing between the start and end dates of days (they have different time stamps), we have to align the two series better (again, this is not optimal, so you might want to figure out a better way):

.index(rawdailyopen) <- .index(rawdailyclose)

Then we can merge the different series together to have everything in one place.

rawdailyohlc <- merge.xts(rawdailyopen, rawdailymax, rawdailymin, rawdailyclose, rawdailyvol)
colnames(rawdailyohlc) <- c("Open", "High", "Low", "Close", "Volume")
head(rawdailyohlc)

Now we have a useful dataset which should be familiar if you have seen stock data before:

           Open High  Low Close    Volume
2011-09-13 5.80 6.00 5.65  5.97  58.37138
2011-09-14 5.58 5.72 5.52  5.53  61.14598
2011-09-15 5.12 5.24 5.00  5.13  80.14080
2011-09-16 4.82 4.87 4.80  4.85  39.91401
2011-09-17 4.87 4.87 4.87  4.87   0.30000
2011-09-18 4.87 4.92 4.81  4.92 119.81280

We can do some charting in the next post: Charting Bitcoin data

You can also jump to each section directly from here: