Bitcoin Forum

Economy => Service Discussion => Topic started by: praetoriansentry on November 19, 2013, 02:55:21 PM



Title: R Script for CEX.IO Trades
Post by: praetoriansentry on November 19, 2013, 02:55:21 PM
I thought I would share the script I made to plot CEX.IO data.  You can change the date and time ranges by adding a since parameter to the URL or by changing the bucket size.

Code:
library("rjson")
library("quantmod")

# Add a since parameter here to plot for a certain date range
json_file <- "https://cex.io/api/trade_history/GHS/BTC"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

cexio <- json_data

trades <- do.call(rbind.data.frame, cexio)

# Change the bucket size here
trades$bucket <- floor(as.integer(trades$date) / (60*5))
buckets <- unique(trades$bucket)

cexio.Dates <- lapply(buckets, function(x){
  first(as.integer(as.character(trades[trades$bucket == x,]$date)))
})
cexio.Open <- lapply(buckets, function(x){
  first(as.double(as.character(trades[trades$bucket == x,]$price)))
})
cexio.High <- lapply(buckets, function(x){
  max(as.double(as.character(trades[trades$bucket == x,]$price)))
})
cexio.Low <- lapply(buckets, function(x){
  min(as.double(as.character(trades[trades$bucket == x,]$price)))
})
cexio.Close <- lapply(buckets, function(x){
  last(as.double(as.character(trades[trades$bucket == x,]$price)))
})
cexio.Volume <- lapply(buckets, function(x){
  sum(as.double(as.character(trades[trades$bucket == x,]$amount)))
})

CEX <- data.frame(
  "Open" = as.double(cexio.Open),
  "High" = as.double(cexio.High),
  "Low" = as.double(cexio.Low),
  "Close" = as.double(cexio.Close),
  "Volume" = as.double(cexio.Volume)
)

CEX <- as.xts(CEX, order.by=as.POSIXlt(unlist(cexio.Dates), origin="1970-01-01", tz="GMT"))

chartSeries(CEX)
addMACD()
addBBands()



https://i.imgur.com/CyfQSjI.png