Skip to content
All library documents

Representing Rebalanced Portfolios as Trade Journals in R

Article Quant Q&A · Author: Daniel Mc Phillips

Summary

The document addresses how to represent a stock portfolio that is rebalanced periodically while daily positions need to be recovered over time. It presents a trade-journal approach: record each instrument, transaction amount, date, and price, then derive holdings from those transactions. In the example, buying and later selling one stock leaves it with a zero position while a purchase of another stock creates an active holding.

The demonstrated R package can report portfolio composition on a chosen date or across a sequence of dates, and an option omits zero holdings from the displayed positions. This structure supports a time-based view containing only active investments while retaining the underlying transaction history. The example illustrates position tracking rather than a complete portfolio backtest: it does not explain how to translate published quarterly target weights into trades, model transaction costs, handle corporate actions, or measure performance. The package-specific functions may also require adaptation to a researcher’s data and workflow.

Key ideas

  • A trade journal records transactions and can serve as the basis for reconstructing portfolio holdings.
  • Portfolio positions can be queried for a specific date or a range of dates.
  • Zero-position instruments can be excluded from a displayed portfolio composition.
  • The example demonstrates position tracking but does not cover trading costs or performance measurement.
  • Quarterly target weights still need to be translated into appropriate transactions.

Tags

Full text
# Creating a portfolio in R : good practices


# 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

## Answer by Enrico Schumann (score 3, accepted)

https://quant.stackexchange.com/a/38619

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
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.