Click and see the complete code
1) Determine 8 indexes which is representing each asset
2) Get Long Momentum (105 days) and Short Momentum (20 days) to rank
3) Determine the category with the lowest sum of the two ranks. (1st + 5th = 6)
4) To see if the current price is higher than the past 3-month average.
If it is higher, then do invest
required(quantmod, PerformanceAnalytics, TTR)
#get adjust prices
symbols <- c("NAESX", #small cap "PREMX", #emerging bond "VEIEX", #emerging markets "VFICX", #intermediate investment grade "VFIIX", #GNMA mortgage "VFINX", #S&P 500 index "VGSIX", #MSCI REIT "VGTSX", #total intl stock idx "VUSTX") #long term treasury (cash)
getSymbols(symbols, from="1990-01-01")
#save to Prices
prices <- list() for(i in 1:length(symbols)) { prices[[i]] <- Ad(get(symbols[i])[,6]) }
#change to Dataframe and remove NA values
prices <- cbind="" colnames="" do.call="" gsub="" na.omit="" prices="" span="" z="">->
#split cash apart from ranking calculation
cashPrices<-prices font="" prices="">-prices>
#calculate momentum
nShort <- 20="" span="">->
nLong <- 105="" span="">->
nSMA <- 3="" span="">->
momShort <- -="" 1="" lag="" nshort="" prices="" span="">->
momLong <- -="" 1="" lag="" nlong="" prices="" span="">->
PricesQ<-prices endpoints="" on="quarters" prices="" span="">-prices>
PricesM<-prices endpoints="" on="months" prices="" span="">-prices>
momShortQ<-momshort endpoints="" on="quarters" prices="" span="">-momshort>
momLongQ<-momlong endpoints="" on="quarters" prices="" span="">-momlong>
#rank by momentum
srank <- 1="" apply="" br="" lrank="" momlongq="" momshortq="" rank="" t="">->
#as there is a chance that both values are same, I put more value on long momentum
totRank <- 1.01="" 1="" apply="" function="" lrank="" max="" maxrank="" rankpos="" rankrow="" return="" span="" srank="" t="" totrank="">->
# check whether current price is higher than average of past 3 months prices.
PricesSMAsM<- apply="" index="" n="nSMA)," order.by="index(PricesM))" pricesq="" pricessmasm="" pricessmasq="" ricesm="" ricesq="" smafilter="" xts=""> PricesSMAsQ->
# find intersections between the two
lastPos<- br="" lastpos="" na.omit="" rankpos="" smafilter="">->
#invest to cash if there is nothing to invest
cash <- cash="" font="" join="inner" lastpos="" merge="" order.by="index(lastPos))" rowsums="" xts="">->
#calculate return
prices<-merge cashprices="" eturn.calculate="" font="" join="inner" lastpos="" na.omit="" prices="" return.portfolio="" returns="" stratrets="">-merge>
#evaluate the model
table.AnnualizedReturns(stratRets)
maxDrawdown(stratRets)
charts.PerformanceSummary(stratRets)
portfolio.returns
Annualized Return 0.1680
Annualized Std Dev 0.1599
Annualized Sharpe (Rf=0%) 1.0507
maxDrawndown=[1] 0.2978453
-reference-
http://blog.naver.com/htk1019/220924952051
0 Comment to "Multi-Asset Momentum Strategy in R"
Post a Comment