View difference between Paste ID: 20LTvgCv and yQLpEiy8
SHOW: | | - or go back to the newest paste.
1-
1+
 
2
# MtGox trades history download ----------------------------------------
3-
3+
# v1.1
4
 
5
#official topic of the script: https://bitcointalk.org/index.php?topic=286755
6
#caused by mtgox issue related to server overload
7
#the result set will contain join dataset, bitcoincharts data has fewer attribiutes
8-
8+
9
 
10
#install.packages - RUN ONLY FIRST TIME
11-
11+
12
 
13
#loading packages
14
require(data.table) #http://cran.r-project.org/web/packages/data.table/index.html
15
require(RCurl) #http://cran.r-project.org/web/packages/RCurl/index.html
16
require(RJSONIO) #http://cran.r-project.org/web/packages/RJSONIO/index.html
17-
17+
18
 
19
#params
20
market <- 'mtgoxUSD'
21
output_chunks <- 1000000 #to do not write millions of rows at once, if you have low memory you can adjust this to smaller chunks
22
output_format <- c('csv','db') #possible values: 'csv', 'db', c('csv','db') #market is also sqlite table name
23
csv.sep <- ';'
24
csv.dec <- ','
25-
25+
26
 
27
cat('\n',as.character(Sys.time()),': processing started',sep='')
28
init_time <- proc.time()[[3]]
29
#download full history from bitcoincharts
30
#http://bitcoincharts.com/about/markets-api/
31
cat('\n',as.character(Sys.time()),': downloading trades csv from bitcoincharts',sep='')
32
bitcoincharts_dt <- data.table(read.csv(paste0('http://api.bitcoincharts.com/v1/csv/',market,'.csv'), header = FALSE, col.names = c('date','price','amount')))
33-
33+
34
 
35
#download most recent trades until reached time from bitcoincharts
36
#https://bitbucket.org/nitrous/mtgox-api/#markdown-header-moneytradesfetch
37
cat('\n',as.character(Sys.time()),': downloading recent trades from MtGox',sep='')
38
since <- paste0(bitcoincharts_dt[NROW(bitcoincharts_dt),date]-1,'999999')
39
mtgox_dt <- data.table()
40
repeat{
41
  mtgox_dt.batch <- fromJSON(rawToChar(getURLContent(curl = getCurlHandle(),
42
                                                     url = paste0('https://data.mtgox.com/api/2/BTC',substring(market,6,9),'/money/trades/fetch?since=',since),
43
                                                     useragent = R.version.string,
44
                                                     cainfo = 'ca-bundle.crt',
45
                                                     ssl.verifypeer = ssl.verify,
46
                                                     ssl.verifyhost = ssl.verify,
47
                                                     verbose = FALSE,
48
                                                     binary = TRUE)))
49
  if(mtgox_dt.batch[['result']] != 'success'){
50
    cat('\n',as.character(Sys.time()),': mtgox api returns error, retrying',sep='')
51
    next
52
  }
53
  mtgox_dt.batch <- rbindlist(mtgox_dt.batch$data)
54
  mtgox_dt <- rbindlist(list(mtgox_dt, mtgox_dt.batch))
55
  if(NROW(mtgox_dt.batch) < 1000) break
56
  since <- mtgox_dt.batch[NROW(mtgox_dt.batch),tid]
57
}
58-
58+
59
 
60-
bitcoincharts_dt <- bitcoincharts_dt[,c('date','price','amount','price_int','amount_int','tid','price_currency','item','trade_type','primary','properties') := list(date,price,amount,as.integer(NA),as.integer(NA),as.numeric(NA),as.character(NA),as.character(NA),as.character(NA),as.character(NA),as.character(NA))]
60+
61-
mtgox_dt <- mtgox_dt[,c('date','price','amount','price_int','amount_int','tid','price_currency','item','trade_type','primary','properties') := list(as.integer(date),as.numeric(price),as.numeric(amount),as.integer(price_int),as.integer(amount_int),as.numeric(tid),price_currency,item,trade_type,primary,properties)]
61+
bitcoincharts_dt <- bitcoincharts_dt[,c('date','price','amount','price_int','amount_int','tid','price_currency','item','trade_type','primary','properties') := list(date,price,amount,as.integer(NA),as.integer(NA),as.character(NA),as.character(NA),as.character(NA),as.character(NA),as.character(NA),as.character(NA))]
62
mtgox_dt <- mtgox_dt[,c('date','price','amount','price_int','amount_int','tid','price_currency','item','trade_type','primary','properties') := list(as.integer(date),as.numeric(price),as.numeric(amount),as.integer(price_int),as.integer(amount_int),tid,price_currency,item,trade_type,primary,properties)]
63
merged_dt <- rbindlist(list(bitcoincharts_dt[date < last(date),],mtgox_dt))
64-
64+
65
 
66
#output to csv
67
if('csv' %in% output_format){
68
  cat('\n',as.character(Sys.time()),': exporting to csv',sep='')
69
  chunks <- ceiling(NROW(merged_dt)/output_chunks)
70
  for(i in 1:chunks){
71
    if(i == 1) write.table(merged_dt[(1+(i-1)*output_chunks):min((i*output_chunks),NROW(merged_dt))], file = paste0('trades.',market,'.csv'), row.names = FALSE, sep = csv.sep, dec = csv.dec, col.names = TRUE)
72
    else if(i != chunks) write.table(merged_dt[(1+(i-1)*output_chunks):(i*output_chunks)], file = paste0('trades.',market,'.csv'), append = TRUE, row.names = FALSE, sep = csv.sep, dec = csv.dec, col.names = FALSE)
73
    else if(i == chunks & NROW(merged_dt) > (i*output_chunks)) write.table(merged_dt[(1+(i-1)*output_chunks):(i*output_chunks)], file = paste0('trades.',market,'.csv'), append = TRUE, row.names = FALSE, sep = csv.sep, dec = csv.dec, col.names = FALSE)
74
  }
75
  cat('\n',as.character(Sys.time()),': exported csv: ',paste0('trades.',market,'.csv'),sep='')
76-
76+
77
 
78
#output to sqlite db
79
if('db' %in% output_format){
80
  db_conn <- dbConnect(SQLite(), dbname = paste0('trades.db'))
81
  #if table with name equal to market param value already exists (was previously processed) in trades.db drop it manually before running the script
82
  cat('\n',as.character(Sys.time()),': exporting to db',sep='')
83
  chunks <- ceiling(NROW(merged_dt)/output_chunks)
84
  for(i in 1:chunks){
85
    if(i == 1) dbWriteTable(conn = db_conn, name = market, value = merged_dt[(1+(i-1)*output_chunks):min((i*output_chunks),NROW(merged_dt))], row.names = FALSE)
86
    else if(i != chunks) dbWriteTable(conn = db_conn, name = market, value = merged_dt[(1+(i-1)*output_chunks):(i*output_chunks)], append = TRUE, row.names = FALSE)
87
    else if(i == chunks & NROW(merged_dt) > (i*output_chunks)) dbWriteTable(conn = db_conn, name = market, value = merged_dt[(1+(i-1)*output_chunks):(i*output_chunks)], append = TRUE, row.names = FALSE)
88
  }
89
  dbDisconnect(db_conn)
90
  cat('\n',as.character(Sys.time()),': exported sqlite: ',paste0('trades.db'),sep='')
91
}
92
cat('\n',as.character(Sys.time()),': processing finished, total time ',proc.time()[[3]] - init_time,'s., bitcoin donation: 1BNBQw2k8u3L9DLnfLFAeHjs5vcEbGnarT',sep='')