Bitcoin Forum
December 11, 2016, 02:28:09 AM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Using the R platform  (Read 1554 times)
stochastic
Hero Member
*****
Offline Offline

Activity: 532


View Profile
January 12, 2012, 08: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!


Introducing constraints to the economy only serves to limit what can be economical.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481423289
Hero Member
*
Offline Offline

Posts: 1481423289

View Profile Personal Message (Offline)

Ignore
1481423289
Reply with quote  #2

1481423289
Report to moderator
1481423289
Hero Member
*
Offline Offline

Posts: 1481423289

View Profile Personal Message (Offline)

Ignore
1481423289
Reply with quote  #2

1481423289
Report to moderator
1481423289
Hero Member
*
Offline Offline

Posts: 1481423289

View Profile Personal Message (Offline)

Ignore
1481423289
Reply with quote  #2

1481423289
Report to moderator
notme
Legendary
*
Offline Offline

Activity: 1540


View Profile
January 12, 2012, 08:11:23 AM
 #2

Awesome... I've been meaning to look into R.  This will be a great starting place.

https://www.bitcoin.org/bitcoin.pdf
While no idea is perfect, some ideas are useful.
12jh3odyAAaR2XedPKZNCR4X4sebuotQzN
stochastic
Hero Member
*****
Offline Offline

Activity: 532


View Profile
January 12, 2012, 08: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.

Introducing constraints to the economy only serves to limit what can be economical.
dustintrammell
VIP
Full Member
*
Offline Offline

Activity: 153



View Profile
January 12, 2012, 08: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.
notme
Legendary
*
Offline Offline

Activity: 1540


View Profile
January 12, 2012, 08: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.

https://www.bitcoin.org/bitcoin.pdf
While no idea is perfect, some ideas are useful.
12jh3odyAAaR2XedPKZNCR4X4sebuotQzN
deepceleron
Legendary
*
Offline Offline

Activity: 1470



View Profile WWW
January 12, 2012, 10:40:48 AM
 #6

Using R for block finding and pool round probabilities: https://bitcointalk.org/index.php?topic=38785.msg494631#msg494631

MusX
Full Member
***
Offline Offline

Activity: 177


View Profile
March 31, 2013, 11: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?

MusX
Full Member
***
Offline Offline

Activity: 177


View Profile
November 23, 2013, 01:00:59 PM
 #8

you might be interested in: https://bitcointalk.org/index.php?topic=343504

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!