I'm helping a friend with some R homework for a apparently badly taught R class (because all the stuff covered in the class and the supplementary material doesn't help).
We have two datasets. One contains daily discrete returns of a company share in percent and the other contains daily exchange rates from two currencies, let's say USD to Swiss Franc. It looks like this:
Date Mon Day Exchangerate
2000 01 01 1.03405
2000 01 02 1.02987
2000 01 03 1.03021
2000 01 04 1.03456
2000 01 05 1.03200
And the daily discrete returns:
Date Share1
20000104 -0.03778
20000105 0.02154
20000106 0.01345
20000107 -0.01234
20000108 -0.01789
The task is to write a function that uses both matrices and calculates the daily returns from the perspective of a Swiss investor. We assume an initial investment of 1000 US Dollar.
I tried using tidyverse and calculate the changes in total return and percent changes from one day to another using the lag function from dplyr as in the code provided below.
library(tidyverse)
myCHFreturn <- function(matrix1, matrix2) {
total = dplyr::right_join(matrix1, matrix2, by = "date") %>%
dplyr::filter(!is.na(Share1)) %>%
dplyr::select(-c(Date, Mon, Day)) %>%
dplyr::mutate(rentShare1_usd = (1+Share1)*1000,
rentShare1_usd = dplyr::lag(rentShare1_usd) * (1+Share1),
rentShare1_chf = rentShare1_usd*Exchangerate,
rentShare1_chfperc =(rentShare1_chf - dplyr::lag(rentShare1_chf))/dplyr::lag(rentShare1_chf),
rentShare1_chfperc = rentShare1_chfperc*100)
}
The problem is that the rentShare1_usd = dplyr::lag(rentShare1_usd) * (1+Share1)
part of the function relies on the values calculated for the initial 1000 US Dollar investment. Thus, my perception is that we need some type of rolling calculation of the changes, based on the initial investment. However, I don't know how to implement this in the function, since I've only worked with rolling means. We want to calculate the daily returns based on the change given in Variable Share1 and the value of the investment of the previous day. Any help is very much appreciated.
At least to point you to part of a solution, the value of a unit share on any one day is the cumulative product from the start date to that date of (1 + daily_discrete_return) over the time period concerned. To take an example using an extended version of your daily discrete returns table:
df = read.table(text = "Date Share1
20000104 -0.03778
20000105 0.02154
20000106 0.01345
20000107 -0.01234
20000108 -0.01789
20000109 0.02154
20000110 0.01345
20000111 0.02154
20000112 0.02154
20000113 0.01345", header = TRUE, stringsAsFactors = FALSE)
library(dplyr)
Shares = 1000
df1 = mutate(df, ShareValue = cumprod(1+Share1) * Shares)
Date Share1 ShareValue
1 20000104 -0.03778 962.2200
2 20000105 0.02154 982.9462
3 20000106 0.01345 996.1668
4 20000107 -0.01234 983.8741
5 20000108 -0.01789 966.2726
6 20000109 0.02154 987.0862
7 20000110 0.01345 1000.3625
8 20000111 0.02154 1021.9103
9 20000112 0.02154 1043.9222
10 20000113 0.01345 1057.9630
Once you've got a table with the share value as at that date in it you can join it back to your exchange rate table to calculate the swiss currency equivalent for that date, and extend it to do percentage changes and so on.