0
votes

I have a csv file with 2 columns: "Date" and "Ticker". Each date represents the date of the purchase of a "Ticker" in my personal portfolio. For example, SHOP is a ticker I purchased on 2016-24-03.

I would like to use getSymbols to find the closing price for all the tickers I have purchased. After doing so I would like to add those prices to a data Frame with their respective Date and Ticker. The end goal is to compare the return of the ticker versus the return of the SPY.

My issue is that I have not been able to figure out how to use getSymbols because my start date for each ticker is obviously different.

> Port
     Date      Ticker  
1  2018-05-29   TRXC 
2  2018-04-27   SHOP 
3  2018-01-25   MTCH 
4  2018-12-23    TTD 
5  2018-05-22   CLNE 
1
Possible duplicate of stackoverflow.com/q/30897606/3358272 (found by searching StackOverflow for [r] getSymbols start date). - r2evans
Thanks for the link. I updated my post to show how my data is structured. I'm thinking getSymbol may not be the correct route for me to go. I'm looking to find the closing price of each ticker on its respective date so that I can calculate the return to present day.I'm new to [r] so this will be a good learning experience. - dantoine
The answer in the linked question will directly handle this question, using Port$Ticker instead of symbols, and Port$Date instead of begin.date. If you just replace those two symbols, does it work? - r2evans
@dantoine, The answer in the link should be working. The outcome should be a list object with all returns of the tickers in your Port data.frame. Just make sure that the tickers in Port are not stored as a factor. Otherwise create some expected output to show what your end result should look like. - phiver
Note - you should probably use xts objects instead of data frames for this type of analysis. Since getSymbols() returns each object to the global env you could just do this in a loop and cbind() the results - Jared Marks

1 Answers

0
votes

Why not just grab the data and once you have the last 10 years worth of data use dplyr to filter the rows you want by date and symbol?

You would have to install tidyquant - install.packages("tidyquant").

library(tidyquant)
tickers <- c("PYPL", "GOOG", "SHOP", "NVDA", "MSFT", "SPY") ####### Select the stocks to download
time_from <- "2010-01-01" ####### Starting date
time_to <- "2018-08-03" ####### Ending date

stock_returns_daily <- tickers %>%
  tq_get(get = "stock.prices",
from = time_from,
to = time_to)

compare <- stock_returns_daily %>%
  group_by(symbol) %>%
  select(symbol, date, close) %>%
  filter(symbol == c("PYPL", "SPY")) %>% ####### Select the stocks you want to compare to SPY
  filter(between(date, as.Date("2015-09-05"),as.Date("2015-09-17"))) ####### Select the date ranges you are interested in
compare

I am sure there is a much more elegant solution out there but is this more or less along the lines you were looking for? The problem here is that you will have to edit the lines of code where ####### appears for the stocks you want to analyse.