*! version 1.0.0 23jun2016

Title

backtest -- performs and graphs a cumulative backtest of a financial portfolio period by period.

Syntax

cbacktest varlist [if] [in]
, weights(matrix_name) generate(newvarname) timevar(varname) [nograph]

weights(matrix_name) matrix_name is the name of a Stata matrix (Nx1) where the weights of the portfolio are stored.
This is a vertical matrix with dimensions (N x 1), where N= number of price variables.

generate(newvarname) newvarname is the name of a new variable where the cumulative holding return will be stored. If the variable exists, the variable is replaced with the calculated values.

timevar(varname) varname is the name of the time-series variable.

nograph if nograph is specified, the graph will not be shown. The default is to show the graph of cumulative holding return of the portfolio

Description

cbacktest calculates the cumulative holding period return of a financial portfolio for each observation/period. The varlist must be price variables, one for each asset of the portfolio.
The portfolio weights must be located in a (Nx1) Stata matrix, which must be specified in the weights option.
The time series variable is usually the period variable. The dataset is sorted according to this variable.
The cumulative holding return of the portfolio will be stored in the new variable specified in the generate option. If this variable exists, then
it will be replaced.

Remarks

Check the "Also See" Menu for related commands.

Examples

Collects online daily stock data from Yahoo Finance for 2014 and 2015.

. returnsyh AAPL MSFT GE, fm(1) fd(1) fy(2014) lm(12) ld(31) ly(2015) frequency(d) price(adjclose)

Defines two weight matrices for two portfolios:

Portfolio 1: 30% for Apple, Inc, 20% for Microsoft Corp, and 50% for General Electric, Co.:

Portfolio 2: 50% for Apple, Inc, 25% for Microsoft Corp, and 25% for General Electric, Co.:

. matrix WPORT1=(0.3\0.2\0.5)

. matrix WPORT2=(0.5\0.25\0.25)

Generates the cumulative holding returns of the portfolios for the whole period. For each period, the holding period return of the portfolio is calculated:

. cbacktest p_adjclose_AAPL p_adjclose_MSFT p_adjclose_GE, gen(portret1) timevar(period) weights(WPORT1) nograph

. cbacktest p_adjclose_AAPL p_adjclose_MSFT p_adjclose_GE, gen(portret2) timevar(period) weights(WPORT2) nograph

Makes a graph to compare the performance of both portfolios over time:

. tsline portret1 portret2

Generates and graphs the cumulative holding return of the portfolio 1 for a specified period. The holding period return of the portfolio for all days in 2015 (starting from Jan 01 2015) is calculated:

. cbacktest p_adjclose_AAPL p_adjclose_MSFT p_adjclose_GE if period>=td(01jan2015) & period<=td(31dec2015), gen(portret1_1) timevar(period) weights(WPORT1)

Stored results

cbacktest does not save results in r()

Author

Carlos Alberto Dorantes, Tecnológico de Monterrey, Querétaro Campus, Querétaro, México.
Email: cdorante@itesm.mx