| position | R Documentation |
Use information on single trades to compute a position at a specific point in time.
position(amount, ...)
## Default S3 method:
position(amount, timestamp, instrument, when,
drop.zero = FALSE, account = NULL,
use.names = NULL, ...)
## S3 method for class 'journal'
position(amount, when, drop.zero = FALSE,
use.account = FALSE, ...)
## S3 method for class 'position'
print(x, ..., sep = ":")
when |
a timestamp or a vector of timestamps; alternatively, several keywords are supported. See Details. |
amount |
numeric or an object of class |
timestamp |
numeric or character: timestamps, must be sortable |
instrument |
character: symbols to identify different instruments |
account |
character: description of account. Ignored if |
use.account |
logical. If TRUE, positions are computed by account and instrument; otherwise by instrument only. |
use.names |
logical or |
drop.zero |
If logical, drop instruments that have a zero position;
default is |
x |
An object of type position. |
... |
arguments passed to |
sep |
A regular expression. Split instruments accordingly. Not implemented yet. |
position computes positions for lists of trades.
position is a generic function; most useful is the
method for journals.
The function checks if timestamp is sorted (see
is.unsorted) and sorts the journal by
timestamp, if required. If there are (some) NA
values in timestamp, but timestamp is sorted
otherwise, the function will proceed (with a warning,
though).
The argument when can also be specified as one of
several keywords: last (or newest or
latest) provides the position at the latest
timestamp; first (or oldest) provides the
position at the earliest timestamp; all provides the
positions at all timestamps in the journal. endofday,
endofmonth and endofyear provide positions at
the end of all calendar days, months and years within the
timestamp range of the journal. The latter keywords can only
work if timestamp can be coerced to
Date.
Several mathematical operators work specially for
positions. Unary +/- work directly on
positions, whereas unary ! returns a logical
vector of the same dimensions as the initial position.
(So !!<position> shows whether a position is
non-zero.)
Binary operations, such as +, - or >,
work if both positions have the same timestamps (which
includes the case in which the timestamp of both is
NA). Multiplication produces an error.
Instruments can differ, but must be non-NA, except
for the special case of length-one NA.
An object of class position, which is a numeric matrix
with instrument and timestamp attributes. Note
that position will never drop the result's dim
attribute: it will always be a matrix of size
length(when) times length(unique(instrument)),
which may not be obvious from the printed output. (An operation
such as < or ! results in a logical matrix of the
same dimension.)
The rows of the matrix correspond to timestamps; the columns
correspond to instruments.
To extract the numeric position matrix, use as.matrix(p).
Enrico Schumann
Schumann, E. (2025) Portfolio Management with R.
https://enricoschumann.net/R/packages/PMwR/;
in particular, see
https://enricoschumann.net/R/packages/PMwR/manual/PMwR.html#computing-balances
journal; internal computations are handled by
cumsum and findInterval
position(amount = c(1, 1, -1, 3, -4),
timestamp = 1:5, when = 4.9)
## using a journal
J <- journal(timestamp = 1:5, amount = c(1, 1, -1, 3, -4))
position(J, when = 4.9)
## 'declaring' a position, using named amounts
amount <- c(1, 1, 1)
instrument <- c("A", "A", "B")
position(amount = amount, instrument = instrument)
## .... or equivalently
amount <- c(A = 2, B = 1)
position(amount)
## ignore names of amount
position(amount, use.names = FALSE)
## adding/subtracting positions
p1 <- position(c(A = 0.2, B = 0.8))
p2 <- position(c( B = 0.7, C = 0.3))
p1 - p2
## A 0.2
## B 0.1
## C -0.3
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.