-2

I need to create holding returns on 100USD from 3 different stocks.

I made a data frame containing stocks GE, IBM and index NYA taken from yahoo:

stocks <- as.xts(data.frame(GE = GE[,6], IBM = IBM[,6], NYA = NYA[,6]))

Then, I found their returns:

stocks_return <- diff(log(stocks))

Now what I need is to create a plot which displays the holding returns for all three time series if I invest 100USD.

PavoDive
  • 6,322
  • 2
  • 29
  • 55
  • 1
    Welcome to stack overflow. Please improve your question by adding: 1. some sample data. 2. The code of your attempts to solve the problem or achieve what you want. 3. The errors or unexpected behavior you observe in your code and 4. The expected result. **Help us help you** – PavoDive Nov 09 '19 at 16:42
  • Hello and welcome on stack overflow. This is a forum about programming and not about quantitative finance. To increase your chances of a good answer you should clearly explain what you mean by holding returns. Otherwise try asking your question here: https://quant.stackexchange.com/ – Cettt Nov 09 '19 at 16:43

1 Answers1

1

I solved a similar problem before. Here is an adapted version of that:

library(PerformanceAnalytics)
library(quantmod)
library(purrr)
library(magrittr)
library(ggplot2)

date <- "2019-01-01"


stocks <- c("GE",
            "IBM",
            "^NYA")

#get the data
prices  <- stocks %>%
 map(~ getSymbols(., auto.assign = FALSE, from = date))

#get adjusted data
precios_ajustados <- prices %>%
 map(., Ad)

#calculate returns for each adjusted prices
returns <- precios_ajustados %>%
 map(~
      Return.calculate(.x)
 )

#insert 100 in the first observation (made NA by Return.calculate()) to simulate the investment
for (i in 1:length(stocks)) {

 names(returns[[i]]) <- stocks[i]
 returns[[i]][1, 1]  <- 100
 returns[[i]][-1, 1] <- returns[[i]][-1, 1] + 1 #adding a one so that the cumulative return reflects ups and downs.

}

returns <- returns %>% map(~ cumprod(.)) #for every stock return, calculate the cumulative product.
returns <- set_names(returns, stocks) #settings names for easy calling

plot(returns$IBM)
lines(returns$GE, col = "red")
lines(returns$`^NYA`, col = "blue")

You can source the code directly previous downloading the libraries required.

jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • Thanks. Now the OP can copy your solution and submit it as his/her own solution to a homework assignment. – Jim G. Nov 09 '19 at 18:28
  • @JimG. How do you know that? I only tried to post reproducible code to help him. Sorry – jpdugo17 Nov 09 '19 at 18:42
  • Welcome to SO! Your answer seems to be ok. The question is not ok. @Jim G.. IMO this is a programming forum, the problem could be the question, not the answer. – David García Bodego Nov 19 '19 at 03:31