Bitcoin Forum
May 10, 2024, 11:02:03 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Using the R platform  (Read 1743 times)
stochastic (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500


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.
1715382123
Hero Member
*
Offline Offline

Posts: 1715382123

View Profile Personal Message (Offline)

Ignore
1715382123
Reply with quote  #2

1715382123
Report to moderator
1715382123
Hero Member
*
Offline Offline

Posts: 1715382123

View Profile Personal Message (Offline)

Ignore
1715382123
Reply with quote  #2

1715382123
Report to moderator
I HATE TABLES I HATE TABLES I HA(╯°□°)╯︵ ┻━┻ TABLES I HATE TABLES I HATE TABLES
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
notme
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


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.
stochastic (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500


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: 156
Merit: 103


Cleverly disguised as a responsible adult.


View Profile WWW
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.

Dustin D. Trammell
Twitter: @druidian
PGP: E0DC F55C 9386 1691 A67F FB18 F6D9 5E52 FDA6 6E16
notme
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


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.
deepceleron
Legendary
*
Offline Offline

Activity: 1512
Merit: 1032



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: 175
Merit: 100


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: 175
Merit: 100


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:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!