Description Usage Arguments Details Value Author(s) Examples
ror
calculates a vector of cumulative returns.
pnl
calculates a vector of period profit & loss.
1 2 3 |
prices |
numeric matrix or coerciable via |
states |
numeric ector of states. |
roll.at |
logical. See below. |
size.at |
logical. See below. |
delta |
numeric. See below. |
prices
should be coercable by as.matrix
. Valuation
prices are in the first column, roll-out prices are in the
second column, and roll-in prices are in the third column. If a
two-column matrix is passed, the first column represents both
valuation and roll-out prices, and roll-in prices are represented in
the second. If only a vector is passed, no roll adjustments are
made. Any columns beyond column three are ignored.
states
must be a vector consisting of 1, 0, and -1 only. Its
length must be a multiple of nrow(data)
but is generally
identical to it.
pnl
calculates the PnL as:
PnL(t) = [P(t) - P(t-1) + [I(t-1) - O(t-1)] * R(t-1)] * s(t-1)
P is the valuation price, O is the roll-out price,
I is the roll-in price, and R = (0,1) is the
roll flag passed to roll.at
.
roll.at
is a logical or numeric vector consisting of only 1 and
0. Its length must be a multiple of nrow(data)
but is generally
identical to it. It allows the modelling of spliced price series
(e.g., 'continuous' price series of futures contracts). Wherever
roll.at
is TRUE, it is assumed that subsequent observations in
the valuation price vector continues the series hitherto contained in
the roll-in vector. The above definition of PnL incorporates this by
assuming that an open position is exited at the roll-out price and
re-entered at the roll-in price each time roll.at
is TRUE.
ror
calculates the cumulative return as follows:
CRoR(t) = [1 + (PnL(j+1) + PnL(j+2) + ... + PnL(t)) * delta(j)] * CRoR(j)
where j is the last TRUE size.at
prior to t.
delta
defines the percentage change in cumulative return given
change in price. size.at
is a logical vector that specifies the
times where returns are compounded. By default size.at=TRUE
, so
returns are compounded every period and cumulative return is this
intuitive special case of the above equation:
CRoR(t) = [1 + PnL(t) * delta(t-1)] * CRoR(t-1)
This defines a leveraging scheme whereby open positions are re-sized
at every period to maintain a constant Δ sensitivity of
price-to-equity, which may or may not be what is intended. A useful
specification is size.at=phasemap(states)!= 0
, which defines
the assumption that positions are sized to Δ at initiation
only. Another useful specification is delta=n/prices
, which
means that an x\% change in price (since the last TRUE size.at
)
causes an (x * n)% change in cumulative return.
Together delta
and size.at
define the system's leverage
scheme.
A numeric vector representing PnL in price points (pnl
) or
cumulative return (ror
).
Robert Sams robert@sanctumfi.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | prc <- c(1.0,0.5,1.0)
## Equity down 50%, unchanged
ror(prc, c(1,1,1), delta=1.0, size.at=FALSE)
## Equity down 50%, down 25%
ror(prc, c(1,1,1), delta=1.0, size.at=TRUE)
## Equity down 25%, down 6.25%
ror(prc, c(1,1,1), delta=0.5, size.at=TRUE)
## Equity down 100% (bankrupt)
ror(prc, c(1,1,1), delta=2.0, size.at=TRUE)
## NOTE: Cum RoR <100% allowed!
ror(prc, c(1,1,1), delta=3.0, size.at=TRUE)
## Equity up 25%, up 50%
ror(prc, c(-1,1,1), delta=0.5, size.at=FALSE)
## Equity up 25%, up 56.25%
ror(prc, c(-1,1,1), delta=0.5, size.at=TRUE)
## These are the same.
ror(prc, c(1,1,1), delta=c(1,2,1), size.at=TRUE)
ror(prc, c(1,1,1), delta=1, size.at=FALSE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.