Use information on single trades to compute a position at a specific point in time.
position(amount, ...)# S3 method for default
position(amount, timestamp, instrument, when,
drop.zero = FALSE, account = NULL,
use.names = NULL, ...)
# S3 method for journal
position(amount, when, drop.zero = FALSE,
use.account = FALSE, ...)
# S3 method for position
print(x, ..., sep = ":")
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.
To extract the numeric position matrix, say as.matrix(p)
.
a timestamp or a vector of timestamps; alternatively, several keywords are supported. See Details.
numeric or an object of class journal
numeric or character: timestamps, must be sortable
character: symbols to identify different instruments
character: description of account. Ignored if NULL
.
logical
logical
If logical, drop instruments that have a zero
position; default is FALSE
. If numeric, it
is used as a tolerance; e.g., a value of
1-e12
will drop any position whose absolute
amount is smaller than 1-e12
.
An object of type position.
arguments passed to print
A regular expression. Split instruments accordingly. Not implemented yet.
Enrico Schumann
position
is a generic function; most useful is
the method for journal
s.
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
.
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#computing-balances
journal
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)
Run the code above in your browser using DataLab