public
Last active

cdar and cvar allocation

  • Download Gist
cdar and cvar allocation.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.