Skip to content

Instantly share code, notes, and snippets.

@praetoriansentry
Created April 11, 2013 22:24
Show Gist options
  • Save praetoriansentry/5367698 to your computer and use it in GitHub Desktop.
Save praetoriansentry/5367698 to your computer and use it in GitHub Desktop.
Quick script to plot the last 24hrs of mtgox trade data.
library("rjson")
library("quantmod")
json_file <- "https://data.mtgox.com/api/2/BTCUSD/money/trades/fetch"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
mtgox <- json_data$data
trades <- do.call(rbind.data.frame, mtgox)
trades$bucket <- floor(trades$date / (60*5))
buckets <- unique(trades$bucket)
BTC.Dates <- lapply(buckets, function(x){
first(as.integer(as.character(trades[trades$bucket == x,]$date)))
})
BTC.Open <- lapply(buckets, function(x){
first(as.double(as.character(trades[trades$bucket == x,]$price)))
})
BTC.High <- lapply(buckets, function(x){
max(as.double(as.character(trades[trades$bucket == x,]$price)))
})
BTC.Low <- lapply(buckets, function(x){
min(as.double(as.character(trades[trades$bucket == x,]$price)))
})
BTC.Close <- lapply(buckets, function(x){
last(as.double(as.character(trades[trades$bucket == x,]$price)))
})
BTC.Volume <- lapply(buckets, function(x){
sum(as.double(as.character(trades[trades$bucket == x,]$amount)))
})
BTC <- data.frame(
"Open" = as.double(BTC.Open),
"High" = as.double(BTC.High),
"Low" = as.double(BTC.Low),
"Close" = as.double(BTC.Close),
"Volume" = as.double(BTC.Volume)
)
BTC <- as.xts(BTC, order.by=as.POSIXlt(unlist(BTC.Dates), origin="1970-01-01", tz="GMT"))
chartSeries(BTC)
addMACD()
addBBands()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment