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.
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 HallYour best bet is to use quantmod and store the results as a time series (in this case, it will be xts
):
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.
ContangoI'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.
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
.
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 HallIf 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!
Slightly modified from the above solutions... (thanks Shane and Stotastic)
Peter HallUnfortunately, 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.