I am about to use the R platform to do some back-testing on bitcoin trading on MTGOX and wanted to share some of the ways to input the data into R.
R is a programming language for statistical computing and graphics. It is free and can be downloaded at
R-project.
First we need to get some data. The only data I found was the raw data from MTGOX and
bitcoincharts. So I got the data from bitcoincharts and formatted so I could use it in a financial analysis package.
getMTGOX <- function ( days ) {
url1 = "http://bitcoincharts.com/t/trades.csv?symbol=mtgoxUSD"
url2 = "&start="
url3 = "&end="
a = Sys.Date()
b = as.POSIXlt(a) #Convert to unixtime
time = days * 86400
past = b - time
c = b - 0
together = sprintf("%s%s%d%s%d", url1, url2, past, url3, c)
data = read.csv(together, header=FALSE)
colnames(data)<-c("time","price","volume") #Change column names
data$time=as.POSIXct(data$time, origin="1970-01-01") #change to time format
return(data)
}
When getMTGOX() is called. Just enter the number of days you want to use in the data. For example if you want 200 days type in:
x <- getMTGOX( 200 )
This function does not take into account the trading that is happening today. It uses yesterday as the last trading day.
If you want to include the current trading then use:
getMTGOXcurrent <- function ( days ) {
url1 = "http://bitcoincharts.com/t/trades.csv?symbol=mtgoxUSD"
url2 = "&start="
a = Sys.Date()
b = as.POSIXlt(a) #Convert to unixtime
time = days * 86400
past = b - time
together = sprintf("%s%s%d", url1, url2, past)
data = read.csv(together, header=FALSE)
colnames(data)<-c("time","price","volume") #Change column names
data$time=as.POSIXct(data$time, origin="1970-01-01") #change to time format
return(data)
}
This function downloads the data, puts it in a dataframe, makes some column names, formats the unixtime into POSIX, and then returns the data frame.
Now what can we do with this data?
I like to use the
Quantmod library for financial trading modeling and graphics. (Installing packages is simple. If you don't know how send a reply and I can direct you.) You can get a preview of what quantmod can do
here.
Right now the data is like ticker data. We could run it in quantmod but we won't be able to do cool things like technical indicators. So we need to change it to a format it can use really well. Use the following function to change the current data downloaded from bitconcharts.com into OHLCV (Open, High, Low, Close, Volume) format.
ohlc <- function(ttime,tprice,tvolume,fmt)
{
ttime.int <- format(ttime,fmt)
data.frame(time = ttime[tapply(1:length(ttime),ttime.int,function(x) {head(x,1)})],
.Open = tapply(tprice,ttime.int,function(x) {head(x,1)}),
.High = tapply(tprice,ttime.int,max),
.Low = tapply(tprice,ttime.int,min),
.Close = tapply(tprice,ttime.int,function(x) {tail(x,1)}),
.Volume = tapply(tvolume,ttime.int,function(x) {sum(x)}),
.Adjusted = tapply(tprice,ttime.int,function(x) {tail(x,1)}))
}
x <- getMTGOX( 200 )
x.1day <- ohlc(x$time,x$price, x$volume,"%Y%m%d")
We still are not done.
x.1day <- xts(x[,-1], order.by=x[,1])
x.1day <- as.xts(x.1day)
After doing that enter:
chartSeries(x.1day)
and have fun modeling!