Description Usage Arguments Details Value References Examples
Implements a twofund strategy where allocations to each fund are adjusted to maintain some userspecified portfolio beta. For example, you could backtest a zerobeta (i.e. market neutral) UPRO/VBLTX strategy using this function.
1 2 3 4 5  targetbeta_twofunds(tickers = NULL, intercepts = NULL, slopes = NULL, ...,
benchmark.ticker = NULL, reference.tickers = NULL, tickers.gains = NULL,
benchmark.gains = NULL, reference.gains = NULL, target.beta = 0,
tol = 0.15, window.units = 50, failure.method = "closer",
maxall.tol = tol  0.05, initial = 10000)

tickers 
Character vector specifying 2 ticker symbols that Yahoo! Finance recognizes, if you want to download data on the fly. 
intercepts 
Numeric vector of values to add to daily gains for each ticker. 
slopes 
Numeric vector of values to multiply daily gains for each ticker by. Slopes are multiplied prior to adding intercepts. 
... 
Arguments to pass along with 
benchmark.ticker 
Character string specifying ticker symbol for
benchmark index for calculating beta. If unspecified, the first fund in

reference.tickers 
Character vector of ticker symbols to include on graph as data points for comparative purposes. 
tickers.gains 
Numeric matrix of gains, where each column has gains for a particular fund. 
benchmark.gains 
Numeric vector of gains for the benchmark index for
calculating beta. If unspecified, the first fund in 
reference.gains 
Numeric vector or matrix of gains for funds to include on graph as data points for comparative purposes. 
target.beta 
Numeric value. 
tol 
Numeric value specifying how far the effective portfolio beta has
to deviate from 
window.units 
Numeric value specifying the width of the trailing moving window used to estimate each fund's beta. 
failure.method 
Character string or vector specifying method(s) to use
when fund betas are such that the target portfolio beta cannot be achieved.
Choices are 
maxall.tol 
Numeric value specifying tolerance to use when implementing
the 
initial 
Numeric value specifying what value to scale initial prices to. 
The general implementation is as follows. Beta for each of the two funds is
estimated based on the first window.units
gains. Initial allocations
are selected to achieve portfolio beta of target.beta
. If that is not
possible  for example, if target.beta = 0
and both funds have
positive beta  then the action taken depends on what method is selected
through the failure.method
input (details below).
Assuming the target beta is attainable, the function moves over 1 day, and
applies each fund's gains for that day. It then recalculates each fund's
beta based on the window.units
width interval, and determines the
effective portfolio beta based on fund allocations and betas. If the
effective beta is outside of [target.beta  tol, target.beta + tol]
, a
rebalancing trade is triggered. As before, if the target beta cannot be
achieved, certain actions are taken depending on the selected method.
When outside of a trade because the target beta could not be achieved, the function attempts to rebalance each time it shifts over to a new day, regardless of the effective portfolio beta.
When failure.method = "cash"
, the entire portfolio balance is
allocated to cash when the target beta cannot be achieved.
When failure.method = "fund1"
(or "fund2"
), the entire
portfolio balance is allocated to the first (or second) fund when the target
beta cannot be achieved.
When failure.method = "fund1.maxall"
(or "fund2.maxall"
), when
the target beta cannot be achieved, fund 1 (or fund 2) is combined with cash,
with the fund 1 (fund 2) allocation as high as possible while staying within
maxall.tol
of target.beta
.
When failure.method = "inverse1"
(or "inverse2"
), an inverse
version of the first (or second) fund is used when the target beta cannot be
achieved. In many cases where the target beta cannot be achieved with the two
funds, it can be achieved with an inverse version of one and the other. If
the target beta still cannot be achieved, the entire portfolio balance is
allocated to cash.
When failure.method = "closer"
, the entire portfolio balance is
allocated to whichever fund has a beta closer to target.beta
.
For each method, a 4element list containing:
Numeric matrix named fund.balances
giving fund balances over
time.
Numeric matrix named fund.betas
giving fund betas over time.
Numeric vector named effective.betas
giving effective portfolio
beta over time.
Numeric value named trades
giving the total number of trades
executed.
Ryan, J.A. and Ulrich, J.M. (2017) quantmod: Quantitative Financial Modelling Framework. R package version 0.412, https://CRAN.Rproject.org/package=quantmod.
1 2 3 4 5 6  ## Not run:
# Backtest zerobeta UPRO/VBLTX strategy
beta0 < targetbeta_twofunds(tickers = c("UPRO", "VBLTX"), target.beta = 0)
plot(beta0$fund.balances[, "Portfolio"])
## End(Not run)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.