stochastic (OP)
|
|
January 28, 2012, 08:24:03 AM |
|
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))
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 08:35:20 AM |
|
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 08:41:33 AM |
|
You need emotional investor and EMA10/21 contrarian controls. I'm not sure how to implement the emotional investor one...
How would you do the controls then?
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 09:16:23 AM Last edit: January 29, 2012, 09:02:52 AM by dropoutbox |
|
***EDIT*** Calculated Expectancy Expectancy = (Probability of Win * Average Win) – (Probability of Loss * Average Loss) Flipist Daily Flip (1) buy / (-1) sell Signal # Trades % Win Mean Win Mean Loss Median Win Median Loss Mean W/L Median W/L 1 -1 93 52.68817 7.132123 -6.507944 3.636423 -4.839239 1.095910 0.7514452 2 0 1 0.00000 NaN NaN NA NA NaN NA 3 1 90 46.66667 4.551028 -4.431499 2.366660 -2.242254 1.026973 1.0554826 Flipist - Hourly Flip (1) buy / (-1) sell Signal # Trades % Win Mean Win Mean Loss Median Win Median Loss Mean W/L Median W/L 1 -1 2208 50.00000 1.123302 -1.034769 0.6283153 -0.6485819 1.085558 0.9687524 2 0 1 0.00000 NaN NaN NA NA NaN NA 3 1 2206 49.50136 1.063690 -1.027253 0.6050835 -0.5874410 1.035470 1.0300328 Expectancy = -4.0892 EMA10/21 Crossover using Daily data. There are 18 less trades when looking at the EMA10/21, buy (1) when EMA10 > EMA21, sell (-1) when EMA10 < EMA21 Signal # Trades % Win Mean Win Mean Loss Median Win Median Loss Mean W/L Median W/L 1 -1 132 56.81818 6.341372 -5.713935 2.840616 -4.183966 1.109808 0.678929 2 0 21 0.00000 NaN NaN NA NA NaN NA 3 1 52 57.69231 5.068756 -3.911978 3.199519 -1.747152 1.295702 1.831277 EMA10/21 Crossover using Hourly data. EMA10/21 Buy (1) when EMA10 crosses over EMA21, sell (-1) when EMA10 crosses below EMA21 Signal # Trades % Win Mean Win Mean Loss Median Win Median Loss Mean W/L Median W/L 1 -1 83 51.80723 0.8337976 -0.8799018 0.4516124 -0.5958119 0.9476030 0.7579782 2 0 4249 0.00000 NaN NaN NA NA NaN NA 3 1 83 46.98795 0.6240971 -1.4239146 0.4776158 -0.5503347 0.4382968 0.8678642 Expectancy = -45.00281
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 09:41:07 AM |
|
Using modified code from here and here. I ran some backtesting in R to compare the EMA 5 EMA 21 Crossover and a random buy/sell strategy based on a coin flip. Why did I do this? I was making my own trading strategies but got stuck on making one for bullish/bearish divergence and I thought some practice may help me find out what I am doing wrong. Why not generate your own? The R package rgp lets you evolve GAs. I've got some that consistently give me better than 'buy and hold'. In artificial intelligence, genetic programming (GP) is an evolutionary algorithm-based methodology inspired by biological evolution to find computer programs that perform a user-defined task. It is a specialization of genetic algorithms (GA) where each individual is a computer program. It is a machine learning technique used to optimize a population of computer programs according to a fitness landscape determined by a program's ability to perform a given computational task. Wow, thanks for the recommendation. I am an evolutionary biologist (or trying to be) and this makes a lot of sense. How have you applied GP to trading strategies?
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
miscreanity
Legendary
Offline
Activity: 1316
Merit: 1005
|
|
January 28, 2012, 10:08:11 AM |
|
Very nice work.
Also try expanding the time frame to daily & weekly scales.
|
|
|
|
organofcorti
Donator
Legendary
Offline
Activity: 2058
Merit: 1007
Poor impulse control.
|
|
January 28, 2012, 10:22:31 AM |
|
Sure - although tbh I've only been using rgp for a couple of weeks, before that I was using eureqa but I didn't find it flexible enough (it's really just for symbolic regression).
rgp is well documented but if you don't have a lot of experience with GAs (i don't) then the tutorials at rsymbolic.org are a bit incomplete. Still, I have plenty of algorithms generated. Some overfit and just buy below a certain level and sell above it. It all comes down to your fitness function and i think mine leaves a bit to be desired.
Some of the useful trading algo's I've got are of the longlag-shortlag=0 variety, just like the EMA10 EMA21 variety.
I'll be happy to share code I've done so far - I'm sure you can improve on it.
|
|
|
|
stochastic (OP)
|
|
January 28, 2012, 10:39:18 AM |
|
Sure - although tbh I've only been using rgp for a couple of weeks, before that I was using eureqa but I didn't find it flexible enough (it's really just for symbolic regression).
rgp is well documented but if you don't have a lot of experience with GAs (i don't) then the tutorials at rsymbolic.org are a bit incomplete. Still, I have plenty of algorithms generated. Some overfit and just buy below a certain level and sell above it. It all comes down to your fitness function and i think mine leaves a bit to be desired.
Some of the useful trading algo's I've got are of the longlag-shortlag=0 variety, just like the EMA10 EMA21 variety.
I'll be happy to share code I've done so far - I'm sure you can improve on it.
Thanks, if you can put some here or pm to me I will take a look at it. I requested some books on the subject.
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 10:49:06 AM |
|
Very nice work.
Also try expanding the time frame to daily & weekly scales.
I already have the daily scale up. I personally think bitcoin is too volatile to really hold it long term in large quantities, so holding a position for large amounts for weeks or months would be unrealistic if you are using margin.
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
miscreanity
Legendary
Offline
Activity: 1316
Merit: 1005
|
|
January 28, 2012, 11:03:51 AM |
|
I already have the daily scale up. I personally think bitcoin is too volatile to really hold it long term in large quantities, so holding a position for large amounts for weeks or months would be unrealistic if you are using margin.
Right now and for some time to come, yes. You'd have to manage a balanced position by scaling it instead of going all in each time. With Bitcoin, I use weekly data as an overall trending and reversal guide, then daily/hourly to spot scaling opportunities. It'd be more complex using multiple temporal dimensions, but even building on the simple EMA10/21 technique that way would be far more robust - even somewhat predictive. The greater the time-frame, the harder it is to reverse the trend.
|
|
|
|
rebuilder
Legendary
Offline
Activity: 1615
Merit: 1000
|
|
January 28, 2012, 11:21:42 AM |
|
Just popped in to say "nice work!"
I don't have the time to look through the whole thread right now, but I'll be sure to do it when I can.
|
Selling out to advertisers shows you respect neither yourself nor the rest of us. --------------------------------------------------------------- Too many low-quality posts? Mods not keeping things clean enough? Self-moderated threads let you keep signature spammers and trolls out!
|
|
|
gamer4156
|
|
January 28, 2012, 02:47:58 PM |
|
It says EMA5/21 on the title of the chart?
|
|
|
|
stochastic (OP)
|
|
January 28, 2012, 07:18:40 PM |
|
It says EMA5/21 on the title of the chart? Thanks for catching that. It should be EMA10
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
stochastic (OP)
|
|
January 28, 2012, 07:31:04 PM |
|
I already have the daily scale up. I personally think bitcoin is too volatile to really hold it long term in large quantities, so holding a position for large amounts for weeks or months would be unrealistic if you are using margin.
Right now and for some time to come, yes. You'd have to manage a balanced position by scaling it instead of going all in each time. With Bitcoin, I use weekly data as an overall trending and reversal guide, then daily/hourly to spot scaling opportunities. It'd be more complex using multiple temporal dimensions, but even building on the simple EMA10/21 technique that way would be far more robust - even somewhat predictive. The greater the time-frame, the harder it is to reverse the trend. Next time I will add in scaling. Anyone that wants to jump ahead check out here. R has some other tools such as blotter that can keep track of positions.
|
Introducing constraints to the economy only serves to limit what can be economical.
|
|
|
|