Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created May 31, 2012 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/2844444 to your computer and use it in GitHub Desktop.
Save timelyportfolio/2844444 to your computer and use it in GitHub Desktop.
cdar and cvar allocation
#NONE OF THIS IS INVESTMENT ADVICE
#attempt to fuse some cdar and es (cvar) research
#3 sources for most of the ideas incorporated
#http://systematicinvestor.wordpress.com/2011/11/01/minimizing-downside-risk/
#http://www.rinfinance.com/agenda/2010/Carl+Peterson+Boudt_Tutorial.pdf
#Strub, Issam S., Trade Sizing Techniques for Drawdown and Tail Risk Control (May 21, 2012).
# Available at SSRN: http://ssrn.com/abstract=2063848
# Load Systematic Investor Toolbox (SIT)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)
require(quantmod)
require(PerformanceAnalytics)
getSymbols("DEXUSNZ",src="FRED")
getSymbols("DEXMXUS",src="FRED")
getSymbols("DEXUSEU",src="FRED")
currencies <- na.omit(merge(DEXUSNZ,1/DEXMXUS,DEXUSEU)) #,DEXMXUS*DEXUSNZ))
currencies <- currencies[endpoints(currencies,"months"),]
colnames(currencies) <- c("NZDollar","MexPeso","Euro") #",NZDollarInPeso")
currencies.roc <- ROC(currencies,type="discrete",n=1)
#--------------------------------------------------------------------------
# Create Efficient Frontier
#--------------------------------------------------------------------------
ia = create.historical.ia(na.omit(currencies.roc),12,symbols=colnames(currencies))
n = ia$n
# -1 <= x.i <= 1
# set min weight per position to -1 and max weight to 1
# if long only set lb = 0
constraints = new.constraints(n, lb = -1, ub = 1)
# SUM x.i = 1
# insure that portfolio is 100% invested so net leverage = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
# Alpha for used for CVaR and CDaR
# http://www.investopedia.com/articles/04/092904.asp
ia$parameters.alpha = 0.95
# create efficient frontier(s)
ef.risk = portopt(ia, constraints, 50, 'Risk')
ef.cvar = portopt(ia, constraints, 50, 'CVaR', min.cvar.portfolio)
ef.cdar = portopt(ia, constraints, 50, 'CDaR', min.cdar.portfolio)
# Plot multiple Efficient Frontiers
layout( matrix(1:4, nrow = 2) )
plot.ef(ia, list(ef.risk, ef.cvar, ef.cdar), portfolio.risk, F)
plot.ef(ia, list(ef.risk, ef.cvar, ef.cdar), portfolio.cvar, F)
plot.ef(ia, list(ef.risk, ef.cvar, ef.cdar), portfolio.cdar, F)
chart.Drawdown(currencies.roc,legend.loc="topleft",cex.legend=0.75,
main="Drawdown")
# Plot multiple Transition Maps
layout( matrix(1:4, nrow = 2) )
plot.transition.map(ef.risk)
plot.transition.map(ef.cvar)
plot.transition.map(ef.cdar)
chart.CumReturns(currencies.roc,legend.loc="topleft",cex.legend=0.75,
main="Cumulative Returns")
#let's explore each of the frontiers by looking
#at a performance summary chart of the 25th allocation
#in each efficient frontier
returns.ef <-as.xts(cbind(
apply(ef.risk$weight[25] * currencies.roc,MARGIN=1,sum),
apply(ef.cvar$weight[25] * currencies.roc,MARGIN=1,sum),
apply(ef.cdar$weight[25] * currencies.roc,MARGIN=1,sum))
,order.by=as.Date(index(currencies.roc)))
colnames(returns.ef) <- c("sd","cvar","cdar")
layout(c(1,1))
chart.CumReturns(returns.ef,legend.loc="topleft",
main="Performance of 25th Allocation of Each Efficient Frontier")
#use cdd to get conditional drawdown at risk
cdd <- rollapplyr(currencies.roc,FUN=CDD,width=12,na.pad=TRUE)
#use es to get conditional var at risk/expected shortfall
es <- rollapplyr(currencies.roc,FUN=ES,width=12,na.pad=TRUE)
layout(c(1,2)) #2 rows 1 column for plot of CDAR and ES
par(mar=c(2,3,2,1))
chart.TimeSeries(es,legend.loc="bottomleft",xlab=NULL,main="Expected Shortfall")
chart.TimeSeries(cdd,legend.loc="topleft",xlab=NULL,main="Conditional Drawdown (CDAR)")
#explore ma technique and then apply basic cdar position size approach
ma <- as.xts(apply(currencies,MARGIN=2,runMean,n=10),order.by=as.Date(index(currencies)))
#up to 3x gross and net leverage with long only buy of currency
#if above 10 month moving average
system.ma <- apply(lag(currencies > ma,k=1),MARGIN=2,as.numeric) * currencies.roc
layout(c(1,1))
chart.CumReturns(merge(currencies.roc,system.ma),col=rep(1:3,2),legend.loc="topleft",
main="Cumulative Returns of BuyHold and 10-month Mov Avg")
#use cdd to position size with 10 month moving average determining
#long or out
#variation of that presented in Cambridge Strategy paper
#which use Donchian style breakout for long and short
#arbitrarily chose 0.05 for target cdd of each position
#with a cap of 2x leverage for each for net/gross of 6x total
system.cdd.weight <- t(apply(0.05/cdd,MARGIN=1,function(x) {ifelse(x==-Inf | x > 2,2,x)}))
system.cdd <- system.cdd.weight * system.ma
perf.compare <- merge(currencies.roc,as.xts(cbind(apply(system.cdd,MARGIN=1,FUN=sum),
apply(system.ma,MARGIN=1,FUN=sum)),
order.by=as.Date(index(currencies))))
colnames(perf.compare)[4:5] <- c("CDDPositionSize","10mMovAvg")
chart.CumReturns(perf.compare,legend.loc="topleft",
main="Cumulative Return Comparison")
barplot(t(system.cdd.weight*apply(lag(currencies > ma,k=1),MARGIN=2,as.numeric)),
main="Weights of CDD System",legend.text=TRUE,
ylim=c(0,6),col=1:3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment