[R-SIG-Finance] Quantstrat to backtest portfolio strategy. User Defined Weights

Vineet Gupta vineetgupta410 at gmail.com
Wed Dec 14 20:21:07 CET 2016


Dear Brian,

 

Thank you for your answer. 

 

My apologies for not providing enough information. I have a small example at the bottom of this page – it is incomplete, as I could not figure out how to backtest my portfolio strategy with Quantstrat.

 

Your assumptions are correct. I just want to re-balance my portfolio at specified re-balancing dates, according to a set of weights. 

 

With the function ‘Return.portfolio’, aside from the R help documentation, do you know of any other guides for how to use it? If there is no documentation available, then I would be happy to share my notes, if I need to make any.

 

Also, does the ‘Return.portfolio’ function return objects to the Blotter environment, that we can analyse in the same way as Quantstrat strategies? 

 

Example code

 

library(quantstrat)

startDate <- '2010-01-01'   # start of data

endDate <- '2010-07-31'     # end of data

symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI", "XLB", "XLK", "XLU")

Sys.setenv(TZ = "UTC")      # set time zone

getSymbols(symbols, src = 'yahoo', index.class = c("POSIXt", "POSIXct"),

           from = startDate, to = endDate, adjust = TRUE)

 

#Initialise currency and instruments

initDate <- '2009-12-31'

initEq <- 1e6

currency("USD")

stock(symbols, currency = "USD", multiplier = 1)

 

#Create our Markowitz weights

m_wts <- data.frame('Date' = seq(as.Date("2010-01-31"), by="month", length.out=7))

invisible(lapply(symbols, function(x) m_wts[[x]] <<- as.numeric(NaN)))

invisible(lapply(1:nrow(m_wts), function(i) m_wts[i, 2:9] <<- round(runif(8)/8,2)))

invisible(lapply(1:nrow(m_wts), function(i) m_wts[i, 10] <<- 1 - sum(m_wts[i, 2:9])))

unlist(lapply(1:nrow(m_wts), function(i) sum(m_wts[i,2:10 ])))

 

initPortf(name = "markowitz", symbols, initDate = initDate)

initAcct(name="markowitz", portfolios="markowitz", initDate = initDate,

                                                                initEq = initEq)

initOrders(portfolio = "markowitz", initDate = initDate)

strategy('markowitz', store = T)

 

 

Rgds,

Vineet

 

-----Original Message-----
From: Brian G. Peterson [mailto:brian at braverock.com] 
Sent: 14 December 2016 14:40
To: Vineet Gupta <vineetgupta410 at gmail.com>; r-sig-finance at r-project.org
Subject: Re: [R-SIG-Finance] Quantstrat to backtest portfolio strategy. User Defined Weights

 

On Wed, 2016-12-14 at 14:28 +0000, Vineet Gupta wrote:

> I am trying to use Quantstrat to backtest a portfolio strategy with 

> user defined weights at each re-balance date. The user defined 

> weights, are given in a matrix (example shown below)

> 

>          Date     ABC.UN   ABT.UN ACN.UN ADBE.UW

> 

> 20 2015-05-15 0.50000000 0.000000    0.5       0

> 21 2015-05-22 0.50000000 0.000000    0.5       0

> 22 2015-05-29 0.50000000 0.000000    0.5       0

> 23 2015-06-05 0.50000000 0.000000    0.5       0

> 24 2015-06-12 0.08865296 0.411347    0.5       0

> 25 2015-06-19 0.00000000 0.500000    0.5       0

> 

> Simply put, how can I backtest such a strategy, using Quantstrat? I 

> have looked at the rule, 'rulePctEquity', but this appears to only 

> govern maximum trade sizes.

> 

> What information does Quantstrat need to re-balance my portfolio at 

> each user-defined re-balance date?

> 

> Thanks in advance. Very much appreciated

 

You haven't really given us enough information to answer your question, so I'm just guessing based on the information you did provide.

 

If you are simply 'trading' a portfolio strategy where the portfolio is rebalanced form one set of weights to another on a specified rebalancing period, you probably don't want quantstrat at all.

 

In that case, you likely want the function Return.portfolio in PerformanceAnalytics or you want to use PortfolioAnalytics to construct your target portfolio.

 

Working in returns and weights is different than working in trades and cash.

 

rulePctEquity simply governs how much of the total equity is available for each instrument in the strategy.  It doesn't reduce the size of an existing position on the rebalance period, if there is one.  It could certainly be extended to enter orders or transactions to rebalance, if you wanted it to.

 

You'll have to provide a minimal reproducible example and more information about what you'd like to do for someone to be able to help you further.

 

Regards,

 

Brian


	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list