Bitcoin Forum

Economy => Trading Discussion => Topic started by: stochastic on January 12, 2012, 08:08:51 AM



Title: Using the R platform
Post by: stochastic on January 12, 2012, 08:08:51 AM
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 (http://www.r-project.org/).

First we need to get some data.  The only data I found was the raw data from MTGOX and bitcoincharts (http://www.bitcoincharts.com).  So I got the data from bitcoincharts and formatted so I could use it in a financial analysis package.

Code:
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:

Code:
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:

Code:
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 (http://www.quantmod.com/) 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 (http://www.r-chart.com/2010/06/stock-analysis-using-r.html).

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.

Code:
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)}))
}

Code:
x <- getMTGOX( 200 )
x.1day <- ohlc(x$time,x$price, x$volume,"%Y%m%d")

We still are not done.

Code:
x.1day <- xts(x[,-1], order.by=x[,1])
x.1day <- as.xts(x.1day)

After doing that enter:
Code:
chartSeries(x.1day)

and have fun modeling!

http://img406.imageshack.us/img406/3958/picture19k.png


Title: Re: Using the R platform
Post by: notme on January 12, 2012, 08:11:23 AM
Awesome... I've been meaning to look into R.  This will be a great starting place.


Title: Re: Using the R platform
Post by: stochastic on January 12, 2012, 08:28:21 AM
Awesome... I've been meaning to look into R.  This will be a great starting place.

R is nice to use.  When you play around leave some comments here about your R adventures.


Title: Re: Using the R platform
Post by: dustintrammell on January 12, 2012, 08:44:16 AM
R is nice to use.  When you play around leave some comments here about your R adventures.

I used R for some other stuff quite a while ago:

http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/ (http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/)

I found the Ruby interface to be fairly easy to use.


Title: Re: Using the R platform
Post by: notme on January 12, 2012, 08:46:53 AM
R is nice to use.  When you play around leave some comments here about your R adventures.

I used R for some other stuff quite a while ago:

http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/ (http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/)

I found the Ruby interface to be fairly easy to use.

This keeps getting better.  I program in Ruby for my day job.


Title: Re: Using the R platform
Post by: deepceleron on January 12, 2012, 10:40:48 AM
Using R for block finding and pool round probabilities: https://bitcointalk.org/index.php?topic=38785.msg494631#msg494631


Title: Re: Using the R platform
Post by: MusX on March 31, 2013, 11:08:43 PM
thank you for this code.
I started to interest R after trying this.
Since recent update of digest R package it is now possible to make private api calls.
Are there still people using R as trading platform?


Title: Re: Using the R platform
Post by: MusX on November 23, 2013, 01:00:59 PM
you might be interested in: https://bitcointalk.org/index.php?topic=343504