-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolio-returns-calculator.R
62 lines (46 loc) · 2.25 KB
/
portfolio-returns-calculator.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
############################################
### Portfolio Returns Graphing Utility ###
### ###
### Author: Kaspar Lee (@KasparLee) ###
### Packages: tidyquant, dplyr, ggpplot2 ###
### ###
############################################
# Import libraries
library(tidyquant)
library(dplyr)
library(ggplot2)
# Portfolio items and weightings
# *** These should be updated to include your own portfolio holdings and start date to analyse from ***
# *** Note that the tickers must be named exactly as they appear on Yahoo Finance ***
tickers <- c('VUSA.L', 'VMID.L', 'VFEM.AS', 'INRG.MI', 'STHS.L', 'VGOV.L')
weights <- c(.25, .25, .15, .1, .1, .15)
weightTable <- tibble(symbol = tickers, weights = weights)
startDate = '2013-11-16'
# Load Yahoo Finance stock price data
allStockData <- tq_get(tickers, get='stock.prices', from=startDate)
# Stock data mutated to have daily return column, and weighted daily return column
stockReturns <- allStockData %>%
group_by(symbol) %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = 'daily', col_rename = 'returns') %>%
left_join(weightTable, by = 'symbol') %>%
mutate(weightedReturn = weights * returns)
# Stock data with cumulative returns at each date
stockReturnsCum <- stockReturns %>%
group_by(symbol, date) %>%
summarise(returnsSum = sum(returns)) %>%
mutate(cumulativeReturn = cumprod(1 + returnsSum))
# Combined/weighted cumulative portfolio returns at each date
portfolioReturns <- stockReturns %>%
group_by(date) %>%
summarise(portfolioReturns = sum(weightedReturn)) %>%
mutate(cumulativeReturn = cumprod(1 + portfolioReturns))
# Function to add labels, ticks etc. to plots
addPlotLayers <- function(ggplotFunc) {
ggplotFunc + geom_line() + labs(x = 'Date', y = 'Cumulative Returns') + scale_y_continuous(labels = function(x) paste0(x, '%'))
}
# Plot combined portfolio return line graph
portfolioPlot <- addPlotLayers(ggplot(portfolioReturns, aes(x = date, y = (cumulativeReturn - 1) * 100)))
print(portfolioPlot)
# Plot individual holdings return line graph
individualStocksPlot <- addPlotLayers(ggplot(stockReturnsCum, aes(x = date, y = (cumulativeReturn - 1) * 100, group=symbol, color=symbol)))
print(individualStocksPlot)