Now for the Flipist strategy. This is the same strategy as before but now we can flip every hour if needed.
First the code. I added in everything to this code, from making the signals, making the charts, and outputting the statistics.
############################################
#Backtesting Flipist Method
############################################
# We will need the quantmod package for charting and pulling
# data and the TTR package to calculate RSI(2).
# You can install packages via: install.packages("packageName")
# install.packages(c("quantmod","TTR"))
library(quantmod)
library(TTR)
# Load data
x = last(y,4415) #gets the last 184 daily positions from the data (starts at July 23, 2011)
# Calculate the random indicator
set.seed(43) #include this to reproduce my results
x$flip <- rbinom(4415,1,0.5) #50% probability that 1 (heads/buy) will show and 50% probability that 0 (tails/sell) will show
# Create the buy (1) and sell (-1) signals
sigbuy <- ifelse(x$flip == 1, 1, 0)
sigsell <- ifelse(x$flip == 0, -1, 0)
# 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("flipist.png",width=720,height=720)
plot.zoo( cbind(eq_up, eq_dn),
ylab=c("Long","Short"), col=c("green","red"),
main="Flipist Strategy: 07-23-2011 to 01-22-2012" )
dev.off()
# Create a chart showing mtgoxUSD
png("flipistchart.png",width=720,height=720)
chartSeries(x, subset="last 4415 hours", type="line")
# Add the total equity line
addTA(eq_all)
dev.off()
# Evaluate the Strategy
# install.packages("PerformanceAnalytics")
require(PerformanceAnalytics)
# chart equity curve, daily performance, and drawdowns
png("performance.png",height=720,width=720)
charts.PerformanceSummary(ret)
dev.off()
# This function gives us some standard summary
# statistics for our trades.
tradeStats <- function(signals, returns) {
# Inputs:
# signals : trading signals
# returns : returns corresponding to signals
# Combine data and convert to data.frame
sysRet <- signals * returns * 100
posRet <- sysRet > 0 # Positive rule returns
negRet <- sysRet < 0 # Negative rule returns
dat <- cbind(signals,posRet*100,sysRet[posRet],sysRet[negRet],1)
dat <- as.data.frame(dat)
# Aggreate data for summary statistics
means <- aggregate(dat[,2:4], by=list(dat[,1]), mean, na.rm=TRUE)
medians <- aggregate(dat[,3:4], by=list(dat[,1]), median, na.rm=TRUE)
sums <- aggregate(dat[,5], by=list(dat[,1]), sum)
colnames(means) <- c("Signal","% Win","Mean Win","Mean Loss")
colnames(medians) <- c("Signal","Median Win","Median Loss")
colnames(sums) <- c("Signal","# Trades")
all <- merge(sums,means)
all <- merge(all,medians)
wl <- cbind( abs(all[,"Mean Win"]/all[,"Mean Loss"]),
abs(all[,"Median Win"]/all[,"Median Loss"]) )
colnames(wl) <- c("Mean W/L","Median W/L")
all <- cbind(all,wl)
return(all)
}
print(tradeStats(sig,ret))