This is a newbie question in R. I am downloading yahoo finance monthly stock price data using R where the ticker names are read from a text file. I am using a loop to read the ticker names to download the data and putting them in a list. My problem is some ticker names may not be correct thus my code stops when it encounters this case. I want the following.

  • The majority of people use Yahoo Finance to obtain updated quotes for traded securities all around the world. However, it can be a useful source of financial data also for research purposes. The trick is to be able to easily obtain historical series of prices.
  • Discover historical prices for CSV stock on Yahoo Finance. View daily, weekly or monthly format back to when Carriage Services, Inc. Stock was issued.
  1. skip the ticker name if it is not correct.
  2. Each element in the list is a dataframe. I want the ticker names to be appended to variable names in element dataframes.
  3. I need an efficient way to create a dataframe that has the closing prices as variables.

Here is the sample code for the simplified version of my problem.

Bulk Stock Data Series Download at Jason Strimpel Finance. It is no longer necessary to download historical price information from Yahoo! Finance for multiple.

The code stops at the third entry but I want to skip this ticker and move on to 'MMM'. I have heard about Trycatch() function but do not know how to use it.

As per question 2, I want the variable names for the first element of the list to be 'MSFTopen', 'MSFThigh', 'MSFTlow', and 'MSFTclose'. Is there a better to way to do it apart from using a combination of loop and paste() function.

Finally, for question 3, I need a dataframe with three columns corresponding to closing prices. Again, I am trying to avoid a loop here.

Thank you.

Peter Hall

Download Historical Stock Data Yahoo Finance

19.8k8 gold badges46 silver badges101 bronze badges
user227290user227290
7212 gold badges14 silver badges25 bronze badges

7 Answers

Yahoo Stock Market Data Download

Your best bet is to use quantmod and store the results as a time series (in this case, it will be xts):

Contango
42.7k49 gold badges192 silver badges241 bronze badges
ShaneShane
80.4k29 gold badges201 silver badges211 bronze badges

This also a little late...If you want to grab data with just R's base functions without dealing with any add-on packages, just use the function read.csv(URL), where the URL is a string pointing to the right place at Yahoo. The data will be pulled in as a dataframe, and you will need to convert the 'Date' from a string to a Date type in order for any plots to look nice. Simple code snippet is below.

Using R's base functions may give you more control over the data manipulation.

Contango
42.7k49 gold badges192 silver badges241 bronze badges
stotasticstotastic
5431 gold badge3 silver badges15 bronze badges

I'm a little late to the party, but I think this will be very helpful to other late comers.

The stockSymbols function in TTR fetches instrument symbols from nasdaq.com, and adjusts the symbols to be compatible with Yahoo! Finance. It currently returns ~6,500 symbols for AMEX, NYSE, and NASDAQ. You could also take a look at the code in stockSymbols that adjusts tickers to be compatible with Yahoo! Finance to possibly adjust some of the tickers in your file.

Yahoo finance download historical data csv

NOTE: stockSymbols in the version of TTR on CRAN is broken due to a change on nasdaq.com, but it is fixed in the R-forge version of TTR.

Joshua UlrichJoshua Ulrich
143k24 gold badges283 silver badges367 bronze badges

I do it like this, because I need to have the historic pricelist and a daily update file in order to run other packages:

Peter Hall
19.8k8 gold badges46 silver badges101 bronze badges
René BauchRené Bauch

If your ultimate goal is to get the data.frame of three columns of closing prices, then the new package tidyquant may be better suited for this.

This will scale to any number of stocks, so the file of 1000 tickers should work just fine!

Davis VaughanDavis Vaughan

Slightly modified from the above solutions... (thanks Shane and Stotastic)

Peter Hall
19.8k8 gold badges46 silver badges101 bronze badges
Manoj KumarManoj Kumar
2,5081 gold badge11 silver badges22 bronze badges

Unfortunately, URL 'ichart.finance.yahoo.com' is dead and not working now. As I know, Yahoo closed it and it seems it will not be opened.

Several days ago I found nice alternative (https://eodhistoricaldata.com/) with an API very similar to Yahoo Finance.

Basically, for R-script described above you just need to change this part:

to this:

Then add an API key and it will work in the same way as before. I saved a lot of time for my R-scripts on it.

Yahoo Finance Download Stock Database

Peter Hall
19.8k8 gold badges46 silver badges101 bronze badges
Denis AlaevDenis Alaev

Not the answer you're looking for? Browse other questions tagged rfinance or ask your own question.