As commented by Alex C, the R package PMwR
, which I maintain, may offer some useful functionality. A small example: I create a journal of three trades. (Note that a journal here is simply a collection of trades.)
library("PMwR")
library("orgutils")
tmp <- readOrg(text="
| instrument | amount | timestamp | price |
|------------+--------+------------+-------|
| AMZN | 10 | 2018-01-03 | 1201 |
| AMZN | -10 | 2018-01-10 | 1250 |
| IBM | 20 | 2018-01-10 | 153 |
")
tmp$timestamp <- as.Date(tmp$timestamp)
J <- as.journal(tmp)
J
## instrument timestamp amount price
## 1 AMZN 2018-01-03 10 1201
## 2 AMZN 2018-01-10 -10 1250
## 3 IBM 2018-01-10 20 153
##
## 3 transactions
You get the current composition of the portfolio with position
.
position(J)
## 2018-01-10
## AMZN 0
## IBM 20
The argument drop.zero
hides non-active positions.
position(J, drop.zero = TRUE)
## 2018-01-10
## IBM 20
position(J, when = as.Date("2018-1-5"), drop.zero = TRUE)
## 2018-01-05
## AMZN 10
You may also compute the position for more than one day:
days <- seq(from = as.Date("2018-1-1"),
to = as.Date("2018-1-12"),
by = "1 day")
position(J, when = days)
## AMZN IBM
## 2018-01-01 0 0
## 2018-01-02 0 0
## 2018-01-03 10 0
## 2018-01-04 10 0
## 2018-01-05 10 0
## 2018-01-06 10 0
## 2018-01-07 10 0
## 2018-01-08 10 0
## 2018-01-09 10 0
## 2018-01-10 0 20
## 2018-01-11 0 20
## 2018-01-12 0 20