REIT Momentum in Quantstrat

[This article was first published on Timely Portfolio, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I took a short break from quantstrat to do some REIT analysis REITs for Everybody Might Now Mean REITs for Nobody.  Now let’s link the two by incorporating The Aleph Blog momentum bucket strategy in quantstrat.

From TimelyPortfolio

In all this, I have discovered significant weaknesses in my understanding of position sizing in quantstrat.  For testing, I would like to be able to size my position at entry to be (Account Equity at entry)/(Price of symbol).  However, it appears that the account equity calculation for each period is not made until after order entry.  Please let me know if I am missing something.  Here is my hacked workaround for some performance reporting and analysis.

From TimelyPortfolio

R code (click to download):

require(PerformanceAnalytics)   # clear out evironment
# much cleaner thanks to Guy Yollin
try(rm(list=ls(pos=.instrument),pos=.instrument),silent=TRUE)   #set up bucketing function to generate signal
bucket_signal <- function(price,nbuckets,nper) {
	momscore <- price/runMean(price,n=nper)-1	
	breaks <- quantile(momscore, probs = seq(0, 1, 1/nbuckets),na.rm=TRUE)
	buckets <- cut(momscore, breaks=breaks, labels=FALSE)
	signal <- as.xts(buckets,	
	colnames(signal) <- "bucket_signal"
	signal[,1] <- 0
}   #get NAREIT data
#I like NAREIT since I get back to 1971
#see how to get it in previous post
#much easier though to get Wilshire REIT since 1977 from FRED
#also it is daily instead of monthly
#i'll use this for simplicity
stock.str <- "WILLREITIND"
#get OHLC all filled with monthly closes
index(WILLREITIND) <- as.Date(index(WILLREITIND))
#set up currency and stock
tradeSize = 1   #name strategy, account, and portfolio same
name_all <- "momBuckets"
#set up strategy
strat <- strategy(name_all)   #set up portfolio and account
initOrders(portfolio=name_all,initDate=initDate)   #set up indicator
#do bucket indicator
strat <- add.indicator(strategy = strat, name = "bucket_signal",
#set up signal
strat <- add.signal(strategy = strat, name="sigThreshold",
	arguments = list(threshold=3,column="bucket_signal",
strat <- add.signal(strategy = strat, name="sigThreshold",
	arguments = list(threshold=3,column="bucket_signal",
#set up rule
strat <- add.rule(strategy=strat,name="ruleSignal",
	arguments = list(sigcol="bucket.gte.3", sigval=TRUE,
	 orderside='long', pricemethod='market', replace=FALSE),
	 type='enter', path.dep=TRUE)
strat <- add.rule(strategy=strat,name="ruleSignal",
	arguments = list(sigcol="", sigval=TRUE,
	 orderqty="all", ordertype='market',
	 orderside='long', pricemethod='market', replace=FALSE),
	 type='exit', path.dep=TRUE)   #run strategy and portfolio
out <- applyStrategy(strategy=strat,portfolios=name_all)
#break up steps of applyStrategy for debugging
#strat.ind <- applyIndicators(strategy=strat,mktdata=get(stock.str))
#strat.sig <- applySignals(strategy=strat,indicators=strat.ind)
#,Dates=NULL,signals=strat.sig,mktdata=mktdata,path.dep=TRUE)     updatePortf(Portfolio=name_all,Dates=paste('::',as.Date(Sys.time()),sep=''))
#analyze performance
chart.Posn(Portfolio=name_all,Symbol=stock.str)   #unfortunately I do not know yet how to size position
#on each entry to be CurrentEquity/price
#so the compare really is not worthwhile
#this would be the preferred way
#retCompare <- merge(PortfReturns(Account=name_all)/100,
#	ROC(get(stock.str)[,4],n=1,type="discrete"))
#charts.PerformanceSummary(retCompare)     #could do buy/hold comparison similar to
#blog post A Quantstrat to Build On Part 4
#but still not really applicable
#so I will hack this way
#but of course this really defeats the purpose of quantstrat
positions <- getPortfolio(name_all)$symbols$WILLREITIND$posPL[,"Pos.Qty"]
#position of 1 will get return and position of 0 will get nothing
ret <- lag(positions,k=1)*ROC(get(stock.str)[,4],n=1,type="discrete")
retCompare <- merge(ret, ROC(get(stock.str)[,4],n=1,type="discrete"))
colnames(retCompare) <- c("Strategy","Wilshire REIT")
charts.PerformanceSummary(retCompare,ylog=TRUE, cex.legend=1.25,
	main="Wilshire REIT with Aleph Blog Momentum",

To leave a comment for the author, please follow the link and comment on their blog: Timely Portfolio. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)