To convert the data to hourly is pretty easy. There is some function in one of the quantitative financial packages in R. But I forget which one. But that one function I had before can do the same thing with only a slight modification.
ohlc <- function(ttime,tprice,tvolume,fmt) {
ttime.int <- format(ttime,fmt)
data.frame(time = ttime[tapply(1:length(ttime),ttime.int,function(x) {head(x,1)})],
mtgoxUSD.Open = tapply(tprice,ttime.int,function(x) {head(x,1)}),
mtgoxUSD.High = tapply(tprice,ttime.int,max),
mtgoxUSD.Low = tapply(tprice,ttime.int,min),
mtgoxUSD.Close = tapply(tprice,ttime.int,function(x) {tail(x,1)}),
mtgoxUSD.Volume = tapply(tvolume,ttime.int,function(x) {sum(x)}),
mtgoxUSD.Adjusted = tapply(tprice,ttime.int,function(x) {tail(x,1)}))
}
data <- ohlc(data$time,data$price, data$volume,"%Y%m%d%H") #converts data in CSV to OHLC,[b] notice the %H[/b]
The original data is taken from bitcoincharts and then put through this function. The output is hourly data.
I also modified the code so that only crossovers indicate buy and sell signals. Here is the new code for the EMA10/EMA21 crossover.
***EDIT*** Named the graph wrong.
library(quantmod)
library(TTR)
# Load presorted data
x = last(y,4436) #gets the last 4436 hourly positions from the data
# Calculate the EMA indicators
x$ema10 <- EMA(Cl(x),10)
x$ema21 <- EMA(Cl(x),21)
x$ema10.old = as.double(lag(x$ema10))
x$ema21.old = as.double(lag(x$ema21))
#omit NA's
x = na.omit(x)
# Create the long and short signals
sigbuy = ifelse ((x$ema10 > x$ema21) & (x$ema10.old < x$ema21.old), 1, 0) #buy signal. yesterday's EMA10 was below yesterday's EMA21 and today it crossed over
sigsell = ifelse ( (x$ema10 <= x$ema21) & (x$ema10.old > x$ema21.old), -1, 0) #sell signal. yesterday's EMA10 was above yesterday's EMA21 and today it crossed under
# Lag signals to align with days in market,
# not days signals were generated
sigbuy <- lag(sigbuy,1) # Note k=1 implies a move *forward*
sigsell <- lag(sigsell,1) # Note k=1 implies a move *forward*
# Replace missing signals with no position
# (generally just at beginning of series)
sigbuy[is.na(sigbuy)] <- 0
sigsell[is.na(sigsell)] <- 0
# Combine both signals into one vector
sig <- sigbuy + sigsell
# Calculate Close-to-Close returns
ret <- ROC(Cl(x))
ret[1] <- 0
# Calculate equity curves
eq_up <- exp(cumsum(ret*sigbuy))
eq_dn <- exp(cumsum(ret*sigsell*-1))
eq_all <- exp(cumsum(ret*sig))
# Equity Chart
png(filename="emacrossnew.png",width=720,height=720)
plot.zoo( cbind(eq_up, eq_dn), plot.type="single", ylab=c("Long","Short"), col=c("green","red"), main="EMA10/21 Crossover Strategy:\n 2011-07-23 to 2012-01-22" )
dev.off()
# Create a chart showing mtgoxUSD
png("EMAcrosschart-new.png",width=720,height=720)
chartSeries(x, subset="last 4415 hours", type="line")
# Add the total equity line and EMA lines
addEMA(n=c(10,21),col=c("orange","blue") )
addTA(eq_all)
dev.off()
# Evaluate the Strategy
# install.packages("PerformanceAnalytics")
require(PerformanceAnalytics)
# chart equity curve, daily performance, and drawdowns
png("performance-EMAcrossnew.png",height=720,width=720)
charts.PerformanceSummary(ret)
dev.off()