A function helps us to streamline the code, it takes a value in seconds and changes it to a data and time since the beginning of the epoch that is human readable and can be used by xts:
sec_to_date = function(sec) {
as.POSIXct(sec, origin = '1970-01-01', tz = 'UTC')
}
I create a new column which contains the second time stamp converted to a date and time value.
Sys.setenv(TZ = "UTC")
rawdata$datetime <- sec_to_date(rawdata[,1])
I use the new date and time column to order the other two columns (USD price of Bitcoin and the Volume in Bitcoin).
library(xts)
rawdata <- xts(x = rawdata[,2:3], order.by = rawdata$datetime, tz = "UTC")
The data is now in a format that allows us to easily select rows by date and time and to process it further.
This shows all the rows between 8:00 and 9:00 o'clock:
rawdata["T08:00/T09:00"]
This shows all the rows in the dataset in the year 2011:
rawdata["2011"]
This shows all the rows on the 1st of January 2014:
rawdata["2014-01-01"]
We could directly use the transaction level data, however, we can start simpler with daily and monthly data. See how in the next post: Using xts to summarize Bitcoin transaction data
You can also jump to each section directly from here:
- Introduction to Bitcoin analysis with R
- Retrieving Bitcoin transaction data
- Part 2 - Reading the bitcoin data in to R
- Using the xts package and dates
- Using xts to summarize Bitcoin transaction data
- Setting up Bitcoin data in OHLC format
- Charting Bitcoin data
- Prliminary return analysis with plots
- Preliminary return analysis with rolling windows
- Technical analysis plots of Bitcoin
- Bitcoin's future price path
- Evaluating a portfolio
- Evaluating a stock portfolio
- Copulas and extreme values with Bitcoin
- Copulas and extreme value, many assets