0
votes

Initial Data SetI have over a thousand Stock Tickers, and corresponding IPO Dates. I would like use a loop to go through each stock ticker and find the Open Price for the IPO date. I can currently do this with quantmod. getSymbols(), but it downloads the entire data set, I can limit the data to a time frame, but it wont let me do this ex.

getSymbols('AAPL', from = '2017-02-02', to = '2017-02-02')

For some reason this wont return the first price. It requires a bigger time frame, and I only care about the first Open Price. The end output would be to add a third column to my existing data frame, the third column would be the first price. I am really trying to avoid downloading thousands of Stocks data because then it would run forever. Any help downloading the first Open Price and then putting that price into a data frame for all of the Tickers would be greatly appreciated.

2
I have the initial date, but every stock IPO is different so im not sure how to write a function that could do date plus one day, if the IPO date is on the 31st how would that work? Also i will try to add a output table. @phiver - Student99

2 Answers

0
votes

Since you haven't produced an example of your data, I've made my own. Next time please don't use a screenshot of the object. Use dput() instead.

library(quantmod)
library(lubridate)

getSymbols(c('AAPL', 'GOOG', 'MSFT', 'TSLA', 'FB'))
tickers <- data.frame(ticker = c('AAPL', 'GOOG', 'MSFT', 'TSLA', 'FB'),
                      date = sapply(mget(setdiff(ls(),"B")), function(x) as.character(index(x[1,]))))

tickers$open <- NA
tickers$date <- ymd(tickers$date)

In the above code I download data for a few stocks and get the first date of data and use that as an ipo date. I have made the dates into a date format using lubridate. This makes it easier to add dates, as you've said in the comment, this is impossible with characters. However, if it is formatted as a date, + 7 will take it to the next month if the date is on the last day of the month.

Code below is simple. I extract the ticker, which will be passed to the getSymbols argument in getSymbols(). The ipo_date will be passed to the from argument of getSymbols(). The next_date is 7 days from the ipo date, anything less than 7 days does not work.

I set auto.assign to FALSE so that getSymbols() does not save the object to the environment. This allows me to extract the first open price, which is always in the first row of the first column, hence the [1, 1] immediately after the getSymbols() call.

for (i in 1:nrow(tickers)) {

  ticker <- as.character(tickers[i, 'ticker'])

  ipo_date <- as.character(tickers[i, 'date'])

  next_date <- as.character(tickers[i, 'date']+7)

  tickers[i, 'open'] <- getSymbols(ticker, from = ipo_date, to = next_date, 
                                   auto.assign = FALSE)[1,1]

}
0
votes

Some of your stocks are not American! This is problematic as Yahoo Finance uses a country extension for equity markets outside of America. For instance, there is not stock with a symbol of 1810 in America, so this returns an error. There are several outside of America, I chose the one from Hong Kong. To download this data, you need to be put .HK after the symbol for Hong Kong. For LSE (London), it's .L

Once I fixed this the code works. All tickers have their open price on IPO date. However, if these are in different currencies, I'm not sure how useful this is.


xy <- data.frame(`IPO Date` = c("7/9/2018", "9/21/2018", "6/30/2017", "9/28/2017",
                                "9/12/2018", "8/1/2018", "4/3/2018", "6/19/2018",
                                "6/29/2017", "9/28/2018"), `Stock Symbol` = 
                                c("1810.HK", "FTCH", "DHER.DE", "ROKU", "NIO", 
                                  "CWK", "SPOT", "4385.T", "APRN", "FCH.L"))

colnames(xy) <- gsub('\\.', ' ', colnames(xy))

library(lubridate)
library(quantmod)

xy$`IPO Date` <- mdy(xy$`IPO Date`)
xy$`IPO Open Price` <- NA

for (i in 1:nrow(xy)) {

  ticker <- as.character(xy[i, 'Stock Symbol'])

  ipo_date <- as.character(xy[i, 'IPO Date'])

  next_date <- as.character(xy[i, 'IPO Date']+7)

  xy[i, 'IPO Open Price'] <- getSymbols(ticker, from = ipo_date, to = next_date, 
                                   auto.assign = FALSE)[1,1]

}