Bitcoin Forum
September 15, 2024, 06:19:51 AM *
News: Latest Bitcoin Core release: 27.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: R Script for CEX.IO Trades  (Read 1296 times)
praetoriansentry (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
November 19, 2013, 02:55:21 PM
 #1

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
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!