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
  • So what you want to calculate is $$ 100\left(\frac{\rm Close}{\rm Open} - 1\right) $$ ? – caverac Oct 18 '17 at 14:00
  • Are your entry and exit prices the close of each period? If not, what are the entry and exit prices for your first trade on 20070829? – amdopt Oct 18 '17 at 20:36
up vote 2 down vote accepted

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

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

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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