# Creating a portfolio in R : good practices

I am quite new to quantfin, but wanting to learn. I've searched for the answer (google and stackex), but haven't found anything satisfactory (but I might not be asking the correct questions...) The problem :

I have to recreate a portfolio (PF) from a journal, which is published every 3 months, but with daily data. This PF is rebalanced every 3 months. I currently got all the daily data I need (universe). How do I create my PF in the most useful way?

My ideal goal would be to have my PF, time-based, with only the stocks that are in it at that moment (and not the whole universe with weight=0).

Is there a package for this? what are good practices ?

Any help appreciated

• Enrico Schumann has created a package in R that deals with some of this, I think. enricoschumann.net/R/packages/PMwR/manual/PMwR.html He posts here and may be able to give you more details... – Alex C Mar 4 '18 at 17:20
• Thanks Alex, I'll have a look tomorrow and give a feedback. – Daniel Mc Phillips Mar 4 '18 at 21:15
• how does your journal look like? What is its structure? Can you give a snapshot? – AK88 Mar 5 '18 at 3:50
• @AK88 By journal, I meant a newspaper. In this case, it is the Risk-Portfolio held by Finanz-und-Wirtschaft newspaper (Switzerland). I confess it wasn't very clever to mention "journal" in this topic! – Daniel Mc Phillips Mar 5 '18 at 19:42

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")

| 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

• Hi Enrico, thanks for your help. Your package should do the trick! – Daniel Mc Phillips Mar 5 '18 at 19:39