2
$\begingroup$

I’ve been looking around for a R-package that will allow me to track my stock portfolio - basically I would like to enter stocks that I own, track the trades I make, calculate my open position & average cost, unrealized & realized P/L, etc… but I have not seen a package that does this. Has anyone read about/use one with these sorts of functions? Thanks.

$\endgroup$
1
$\begingroup$

You can do that with the blotter package. We use it to reconcile our trades.

It's only available on R-Forge, so see this stackoverflow question for how to install it. Run the "amzn_test" demo for an example of how to use it:

library(blotter)
demo(amzn_test)
$\endgroup$
1
$\begingroup$

The PMwR package, which I maintain, provides such computations. The package is on CRAN and GitHub/GitLab.

Some example code:

library("PMwR")

trades <- read.table(text="
 timestamp , instrument , amount , price
2019-06-25 ,     Amazon ,     20 ,  1878
2019-06-26 ,     Amazon ,    -10 ,  1902
2019-07-01 ,     Amazon ,    -10 ,  1921
2019-04-15 ,    Netflix ,     20 ,   362",
header = TRUE, sep = ",",
strip.white = TRUE, stringsAsFactors = FALSE)

trades$timestamp <- as.Date(trades$timestamp)
trades <- as.journal(trades)
trades
##    instrument   timestamp  amount  price
## 1      Amazon  2019-06-25      20   1878
## 2      Amazon  2019-06-26     -10   1902
## 3      Amazon  2019-07-01     -10   1921
## 4     Netflix  2019-04-15      20    362
## 
## 4 transactions  

trades is a journal, a list of your trades.

summary(trades)
## journal: 4 transactions in 2 instruments
## 
##   instrument  n  avg buy  avg sell       first        last
##      Amazon   3     1878    1911.5  2019-06-25  2019-07-01
##      Netflix  1      362        NA  2019-04-15  2019-04-15

From this journal, you can now compute positions.

position(trades)
##         2019-07-01
## Amazon           0
## Netflix         20

position(trades, drop.zero = TRUE)
##         2019-07-01
## Netflix         20

position(trades, when = as.Date("2019-06-27"))
##         2019-06-27
## Amazon          10
## Netflix         20

And you can compute profit/loss.

pl(trades)
## Amazon 
##   P/L total        670
##   average buy     1878
##   average sell  1911.5
##   cum. volume       40
## 
## Netflix 
##   P/L total      NA
##   average buy   362
##   average sell   NA
##   cum. volume    20
## 
## ‘P/L total’ is in units of instrument;
## ‘volume’ is sum of /absolute/ amounts.
## ‘sum(amount)’ is not zero for Netflix: specify ‘vprice’ to compute p/l.

Since there is an open position in Netflix, we need to provide a price that is used for valuation (vprice).

pl(trades, vprice = c(Netflix = 380))
## Amazon 
##   P/L total        670
##   average buy     1878
##   average sell  1911.5
##   cum. volume       40
## 
## Netflix 
##   P/L total     360
##   average buy   362
##   average sell  380
##   cum. volume    20
## 
## ‘P/L total’ is in units of instrument;
## ‘volume’ is sum of /absolute/ amounts.

The function pl can also compute profit/loss over time.

pl(trades["Amazon"], along.timestamp = TRUE)
## Amazon 
##   timestamp     2019-06-25 2019-06-26 2019-07-01
##   P/L total              0        480        670
##   __ realised            0        240        670
##   __ unrealised          0        240          0
##   average buy         1878
##   average sell      1911.5
##   cum. volume           20         30         40
## 
## ‘P/L total’ is in units of instrument;
## ‘volume’ is sum of /absolute/ amounts.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.