Skip to content
All library documents

Marking Open-Position Profit and Loss Through Time

Article Quant Q&A · Author: user2300940

Summary

This example shows how to calculate realized and unrealized profit and loss for a position over time from a trade journal and a sequence of valuation prices. The sample trade records an entry and exit, while hourly index closes provide prices for marking the position. A journal represents the signed trade amounts, execution prices, and timestamps; a profit-and-loss function then computes trade P&L and can evaluate the position along the supplied timestamps using the close prices.

The output demonstrates how the mark changes while the trade is open and remains at realized P&L after the position closes. The author notes that entry and exit prices can be adjusted to include slippage, and clarifies that the reported P&L is in instrument units. The example illustrates one R package workflow rather than a general script for every trade list. It uses closing prices, so it does not resolve intrabar valuation, fees, contract multipliers, or how to reconcile the sample trade profits with the computed instrument-unit result. A follow-up asks about timestamp conversion but gives no answer.

Key ideas

  • A trade journal records signed amounts, execution prices, and timestamps for P&L calculations.
  • Valuation prices at successive timestamps can produce a time series of realized and unrealized P&L.
  • After a position closes, the example's cumulative P&L remains at the realized amount.
  • Execution prices can be adjusted to account for slippage, and the output is stated in instrument units.
  • The example uses closes and does not specify treatment of fees, contract multipliers, or intrabar marks.

Tags

Full text
# Calculate day-to-day change in value of open position


# Calculate day-to-day change in value of open position












From a list of trades I would like to calculate the change in profit during the time the position is open. Does anyone have a script to do this, either python or R? The profits are based on a contract size of 100 from the index, where slippage is taken into consideration.

Trade list

```
Enter           Exit            Profit
20070829 14:00  20070829 17:24  1,465,00
20070914 17:24  20070918 14:00  1,588,98
20070924 14:00  20070924 17:24  796,98
```

Index data:

```
 Date     Time      Open     High     Low     Close  Volume
  20070829  900     7430.24 7430.24 7358.12 7372.1  1
  20070829  1000    7378.56 7378.56 7354.41 7372.16 1
  20070829  1100    7366.6  7433.46 7366.6  7428.72 1
  20070829  1200    7423.62 7429.08 7408.82 7418.13 1
  20070829  1300    7417.8  7427.13 7412.43 7422.03 1
  20070829  1400    7423.91 7434.21 7406.92 7418.04 1
  20070829  1500    7416.77 7431.55 7413.9  7426.8  1
  20070829  1600    7428.96 7442.38 7406.2  7414.65 1
  20070829  1724    7415.36 7433.29 7407.98 7433.29 1
  20070830  900     7439.18 7484.69 7439.18 7478.72 1
  20070830  1000    7478.22 7491.75 7462.95 7464.2  1
  20070830  1100    7463.82 7480.76 7463.82 7475.08 1
  20070830  1200    7474.77 7474.77 7439.76 7456.95 1
  20070830  1300    7456.19 7456.19 7423.3  7429.93 1
  20070830  1400    7430.05 7444.99 7425.68 7444.99 1
  20070830  1500    7441.21 7454.32 7419.61 7420.8  1
  20070830  1600    7421.59 7480.41 7418.81 7479.4  1
  20070830  1724    7479.01 7494.39 7469.71 7487.42 1
  20070831  900     7519.94 7574.6  7519.94 7554.82 1
  20070831  1000    7554.85 7564.01 7546.77 7552.3  1
```

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

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

Perhaps the PMwR package does some of the things you want. Disclosure: I am the package author. PMwR is not on CRAN (yet), but is on GitHub (https://github.com/enricoschumann/PMwR) and can also be installed from http://enricoschumann.net/R/packages/PMwR/index.htm .

It allows you to compute the profit/loss for trades, both in total and over time.

You start by creating a `journal`. I take your first trade and use the closing prices from the table. If you want to include your slippage, adjust the entry/exit prices.

```
library("PMwR")
j <- journal(amount = c(100, -100),
             price = c(7418.04, 7433.29),
             timestamp = as.POSIXct(c("20070829  1400",
                                      "20070829  1724"),
                                    format = "%Y%m%d  %H%M"))

j

##              timestamp  amount    price
## 1  2007-08-29 14:00:00     100  7418.04
## 2  2007-08-29 17:24:00    -100  7433.29
## 
## 2 transactions
```

To compute the profit/loss, use the function `pl`.

```
pl(j)

## P/L total        1525
## average buy   7418.04
## average sell  7433.29
## cum. volume       200
## 
## 'P/L total' is in units of instrument;
## 'volume' is sum of /absolute/ amounts.
```

You may also evaluate your position over time. As an example, I use the close prices you provided.

```
timestamp <- as.POSIXct(
    c("20070829  0900", "20070829  1000", "20070829  1100",
      "20070829  1200", "20070829  1300", "20070829  1400",
      "20070829  1500", "20070829  1600", "20070829  1724",
      "20070830  0900", "20070830  1000", "20070830  1100",
      "20070830  1200", "20070830  1300", "20070830  1400",
      "20070830  1500", "20070830  1600", "20070830  1724",
      "20070831  0900", "20070831  1000"),
    format = "%Y%m%d  %H%M")

close <- c(7372.1 , 7372.16, 7428.72, 7418.13, 7422.03,
           7418.04, 7426.8 , 7414.65, 7433.29, 7478.72,
           7464.2 , 7475.08, 7456.95, 7429.93, 7444.99,
           7420.8 , 7479.4 , 7487.42, 7554.82, 7552.3)
```

Now, you call `pl`with two more arguments: `along.timestamp` and `vprice`; the latter stands for valuation price. This will give you a time-series of total profit/loss (realised + unrealised) of your trade.

```
pl(j, along.timestamp = timestamp, vprice = close)[[1]]$pl

## 2007-08-29 09:00:00 2007-08-29 10:00:00 2007-08-29 11:00:00 
##                   0                   0                   0 
## 2007-08-29 12:00:00 2007-08-29 13:00:00 2007-08-29 14:00:00 
##                   0                   0                   0 
## 2007-08-29 15:00:00 2007-08-29 16:00:00 2007-08-29 17:24:00 
##                 876                -339                1525 
## 2007-08-30 09:00:00 2007-08-30 10:00:00 2007-08-30 11:00:00 
##                1525                1525                1525 
## 2007-08-30 12:00:00 2007-08-30 13:00:00 2007-08-30 14:00:00 
##                1525                1525                1525 
## 2007-08-30 15:00:00 2007-08-30 16:00:00 2007-08-30 17:24:00 
##                1525                1525                1525 
## 2007-08-31 09:00:00 2007-08-31 10:00:00 
##                1525                1525
```

## Answer by user2300940 (score 0)

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

I am getting an error with the format on my data: How do I change the timestamp to numeric?

```
> head(DaxH1)
            timestamp amount   price
1 2008-01-14 11:00:59     19 7735.37
2 2008-01-14 13:00:59    -19 7741.90
3 2008-01-16 17:00:59     20 7485.11
4 2008-01-17 11:00:59    -20 7528.99
5 2008-01-22 16:00:59     22 6651.13
6 2008-01-23 09:07:59    -22 6769.17
```

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.