Author

Topic: Using the R platform (Read 1763 times)

full member
Activity: 175
Merit: 100
full member
Activity: 175
Merit: 100
March 31, 2013, 06:08:43 PM
#7
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?
legendary
Activity: 1512
Merit: 1036
January 12, 2012, 05:40:48 AM
#6
Using R for block finding and pool round probabilities: https://bitcointalksearch.org/topic/m.494631
legendary
Activity: 1904
Merit: 1002
January 12, 2012, 03:46:53 AM
#5
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/

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

This keeps getting better.  I program in Ruby for my day job.
vip
Activity: 156
Merit: 103
Cleverly disguised as a responsible adult.
January 12, 2012, 03:44:16 AM
#4
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/

I found the Ruby interface to be fairly easy to use.
hero member
Activity: 532
Merit: 500
January 12, 2012, 03:28:21 AM
#3
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.
legendary
Activity: 1904
Merit: 1002
January 12, 2012, 03:11:23 AM
#2
Awesome... I've been meaning to look into R.  This will be a great starting place.
hero member
Activity: 532
Merit: 500
January 12, 2012, 03:08:51 AM
#1
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.

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 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.

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!

Jump to: