Selecting trading rules based on past performance

661 Views Asked by At

I am trying to develop a trading system using Quantmod, PerformanceAnalytics and Systematic Investors Toolbox.

I want to create and test a number of simple trading rules (Prices > SMA), (rsi 2 < 0.5 = long) etc etc (this part works fine) based on daily data.

I then want to rank these strategies based on their performance over the previous X days. Then I want to select the top 3 strategies and invest 50% in the top 1, 30% in the second best and 20% in the third best. This is where my problem lies as I have no idea how to do this.

I have looked into some of the functionalities of the Systematic Investor Toolbox or in the Rank function and have looked at past questions but have not succeeded in getting these to work.

Eventually I would like to only rebalance the strategies weight once every month, but lets take one problem at a time.

Below is the code I have so far to test the strategies and create rolling performances:

###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
# Load historical data
#****************************************************************** 
load.packages('quantmod','PerformanceAnalytics')   
tickers = 'SPY'

models <- new.env()
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1950-01-01', env = data, auto.assign = T)
for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)        
bt.prep(data, align='remove.na', dates='1950::2013')

#*****************************************************************
# Code Strategies
#****************************************************************** 
prices = data$prices  
n = len(tickers)  
nperiods = nrow(prices)

#Define indicators
sma.long = bt.apply.matrix(prices, SMA, 200)
dv = bt.apply(data, function(x) { DV(HLC(x), 2, TRUE) } )   
rsi2 = bt.apply.matrix(prices, RSI, 2)

# Buy & Hold    
data$weight[] = 1
models$buy.hold = bt.run(data) 

# Simple TF
data$weight[] = NA
data$weight[] = iif(prices>sma.long,1,0)
data$weight[] = na.locf(data$weight[])
TFweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$tf = bt.run.share(data, commission=0.005, trade.summary=T)

#Trend following + simple dv
data$weight[] = NA
data$weight[] = iif(prices>sma.long,iif(cross.dn(dv,0.5),1,iif(cross.up(dv,0.5),0,NA)),0)
data$weight[] = na.locf(data$weight[])
TFDVweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)
models$tfsimpledv = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion prices > prices - 6 days
data$weight[] = NA
data$weight[] = iif(prices < lag(prices,1),1,iif(prices>lag(prices,1),0,NA))
data$weight[] = na.locf(data$weight[])
MRD1weight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MR1days = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion rsi
data$weight[] = NA
data$weight[] = iif(rsi2<50,1,iif(rsi2>50,0,NA))
data$weight[] = na.locf(data$weight[])
MRrsiweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsi = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion rsi
data$weight[] = NA
data$weight[] = iif(rsi2<50 & prices < lag(prices,1),2,iif(rsi2>50,0,NA))
data$weight[] = na.locf(data$weight[])
MRrsi1dweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsi1d = bt.run.share(data, commission=0.005, trade.summary=T)


#Mean Reversion rsi scaling
data$weight[] = NA
data$weight[] = iif(rsi2<5 ,2,iif(rsi2<10,1.5,iif(rsi2<15,1,iif(rsi2<20,0.5,iif(rsi2>95,-2,iif(rsi2>90,-1.5,iif(rsi2>85,-1,iif(rsi2>80,-0.25,0))))))))
data$weight[] = na.locf(data$weight[])
MRrsiscaling = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsiscaling = bt.run.share(data, commission=0.005, trade.summary=T)


models$EQW = bt.run.share(data, commission=0.01, trade.summary=T)

#calculate daily returns
dailyRMRrsiscaling = diff(log(models$MRrsiscaling$equity))
dailyRMRrsi1d = diff(log(models$MRrsi1d$equity))
dailyRMRrsi = diff(log(models$MRrsi$equity))
dailyRTF = diff(log(models$tf$equity))
dailyRTFsimpledv = diff(log(models$tfsimpledv$equity))

#caculate rolling returns
rollingMRrsiscaling = apply.rolling(dailyRMRrsiscaling, FUN="mean", width=252)
rollingMRrsi1d = apply.rolling(dailyRMRrsi1d, FUN="mean", width=252)
rollingMRrsi = apply.rolling(dailyRMRrsi, FUN="mean", width=252)
rollingTF = apply.rolling(dailyRTF, FUN="mean", width=252)
rollingTFsimpledv = apply.rolling(dailyRTF, FUN="mean", width=252)


plotbt.custom.report(models$MRrsiscaling ,models$MRrsi1d, models$MRrsi, models$MR1days, models$tf,  models$tfsimpledv, models$buy.hold) 

This is basically what I have. I can create and test the strategies but I haven't a clue how to automatically rank the strategies and select only the top 3.

Please let me know if you need any more information! Any help would be greatly appreciated! Thanks in advance

1

There are 1 best solutions below

0
On

You can use several functions from PerformanceAnalytics. For example:

rest=cbind(dailyRMRrsi1d,dailyRMRrsi,dailyRTF,dailyRTFsimpledv)
charts.PerformanceSummary(rest)