SHOW:
|
|
- or go back to the newest paste.
| 1 | ||
| 2 | # MtGox trades history download ---------------------------------------- | |
| 3 | - | # v1.2 |
| 3 | + | # v1.3 |
| 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 | #it should handle other currencies | |
| 9 | ||
| 10 | #install.packages - RUN ONLY FIRST TIME | |
| 11 | #install.packages(c('data.table','RCurl','RJSONIO','RSQLite'))
| |
| 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 | require(RSQLite) #http://cran.r-project.org/web/packages/RSQLite/index.html | |
| 18 | ||
| 19 | #params | |
| 20 | market <- 'mtgoxUSD' | |
| 21 | output_chunks <- 100000 #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 | ssl.verify <- FALSE #before changing to TRUE put certificate file 'ca-bundle.crt' in the working directory, by default directory of the script: http://curl.haxx.se/docs/caextract.html | |
| 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 | cat('\n',as.character(Sys.time()),': bitcoincharts trades downloaded',sep='')
| |
| 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 | cat('\n',as.character(Sys.time()),': MtGox recent trades downloaded',sep='')
| |
| 59 | ||
| 60 | - | #join datasets |
| 60 | + | #transform datasets |
| 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))]
|
| 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.numeric(NA),as.numeric(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)]
|
| 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.numeric(price_int),as.numeric(amount_int),tid,price_currency,item,trade_type,primary,properties)]
|
| 63 | #merge datasets | |
| 64 | merged_dt <- rbindlist(list(bitcoincharts_dt[date < last(date),],mtgox_dt)) | |
| 65 | cat('\n',as.character(Sys.time()),': merged ',NROW(bitcoincharts_dt),' bitcoincharts rows and ',NROW(mtgox_dt),' MtGox rows, current total is ',NROW(merged_dt),' rows',sep='')
| |
| 66 | ||
| 67 | #output to csv | |
| 68 | if('csv' %in% output_format){
| |
| 69 | cat('\n',as.character(Sys.time()),': exporting to csv',sep='')
| |
| 70 | chunks <- ceiling(NROW(merged_dt)/output_chunks) | |
| 71 | for(i in 1:chunks){
| |
| 72 | 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)
| |
| 73 | 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)
| |
| 74 | else if(i == chunks) write.table(merged_dt[(1+(i-1)*output_chunks):NROW(merged_dt)], file = paste0('trades.',market,'.csv'), append = TRUE, row.names = FALSE, sep = csv.sep, dec = csv.dec, col.names = FALSE)
| |
| 75 | } | |
| 76 | cat('\n',as.character(Sys.time()),': exported csv: ',paste0('trades.',market,'.csv'),sep='')
| |
| 77 | } | |
| 78 | ||
| 79 | #output to sqlite db | |
| 80 | if('db' %in% output_format){
| |
| 81 | db_conn <- dbConnect(SQLite(), dbname = paste0('trades.db'))
| |
| 82 | #if table with name equal to market param value already exists (was previously processed) in trades.db drop it manually before running the script | |
| 83 | cat('\n',as.character(Sys.time()),': exporting to db',sep='')
| |
| 84 | chunks <- ceiling(NROW(merged_dt)/output_chunks) | |
| 85 | for(i in 1:chunks){
| |
| 86 | 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) | |
| 87 | 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) | |
| 88 | else if(i == chunks) dbWriteTable(conn = db_conn, name = market, value = merged_dt[(1+(i-1)*output_chunks):NROW(merged_dt)], append = TRUE, row.names = FALSE) | |
| 89 | } | |
| 90 | dbDisconnect(db_conn) | |
| 91 | cat('\n',as.character(Sys.time()),': exported sqlite: ',paste0('trades.db'),sep='')
| |
| 92 | } | |
| 93 | cat('\n',as.character(Sys.time()),': processing finished, total time ',proc.time()[[3]] - init_time,'s., bitcoin donation: 1BNBQw2k8u3L9DLnfLFAeHjs5vcEbGnarT',sep='') |