rebalance: Rebalance Portfolio

View source: R/rebalance.R

rebalanceR Documentation

Rebalance Portfolio

Description

Compute the differences between two portfolios.

Usage

rebalance(current, target, price,
          notional = NULL, multiplier = 1,
          truncate = TRUE, match.names = TRUE,
          fraction = 1, drop.zero = FALSE,
          current.weights = FALSE,
          target.weights = TRUE)

## S3 method for class 'rebalance'
print(x, ..., drop.zero = TRUE)

replace_weight(weights, ..., prefix = TRUE, sep = "::")

Arguments

current

the current holdings: a (typically named) vector of position sizes; can also be a position

target

the target holdings: a (typically named) vector of weights; can also be a position

price

a numeric vector: the current prices; may be named

notional

a single number: the value of the portfolio; if missing, replaced by sum(current*prices)

multiplier

numeric vector, possibly named

truncate

truncate computed positions? Default is TRUE.

match.names

logical

fraction

numeric

x

an object of class rebalance.

...

rebalance: arguments passed to print; replace_weight: numeric vectors

drop.zero

logical: should instruments with no difference between current and target be included?

Note the different defaults for computing and printing.

current.weights

logical. If TRUE (the default), the values in current are interpreted as weights. If FALSE, current is interpreted as a position (i.e. notional/number of contracts).

target.weights

logical. If TRUE (the default), the values in target are interpreted as weights. If FALSE, target is interpreted as a position (i.e. notional/number of contracts).

weights

a numeric vector with named components

sep

character

prefix

logical

Details

The function computes the necessary trades to move from the current portfolio to a target portfolio.

replace_weight is a helper function to split baskets into their components. All arguments passed via ... should be named vectors. If names are not syntactically valid (see make.names), quote them. The passed vectors themselves should be passed as named arguments: see examples.

Value

An object of class rebalance, which is a data.frame:

instrument

character, or NA when match.names is FALSE

price

prices

current

current portfolio

target

new portfolio

difference

the difference between current and target

Attached to the data.frame are several attributes:

notional

notional

match.names

logical

multiplier

multipliers

Author(s)

Enrico Schumann

References

Schumann, E. (2023) Portfolio Management with R. http://enricoschumann.net/R/packages/PMwR/; in particular, see
http://enricoschumann.net/R/packages/PMwR/manual/PMwR.html#rebalance

See Also

journal

Examples

r <- rebalance(current = c(a = 100, b = 20),
               target  = c(a = 0.2, c = 0.3),
               price   = c(a = 1, b = 2, c = 3))
as.journal(r)

## replace_weight: the passed vectors must be named;
##                 'basket_3' is ignored because not
##                 component of weights is named
##                 'basket_3'

replace_weight(c(basket_1 = 0.3,
                 basket_2 = 0.7),
               basket_1 = c(a = 0.1, b = 0.4, c = .5),
               basket_2 = c(x = 0.1, y = 0.4, z = .5),
               basket_3 = c(X = 0.5, Z = 0.5),
               sep = "|")

PMwR documentation built on Oct. 19, 2023, 9:09 a.m.