Get data from Yahoo! Finance to R - r

I am trying to get data from Yahoo! Finance to R
I've installed quantmod like that:
install.packages("quantmod", repos="http://R-Forge.R-project.org")
But when I try this:
getQuote("QQQQ;SPY", what=yahooQF("Last Trade (Price Only)"))
I see:
Error in download.file(paste("http://finance.yahoo.com/d/quotes.csv?
s=", :
cannot open URL 'http://finance.yahoo.com/d/quotes.csv?
s=QQQQ+SPY&f=d1t1l1'
In addition: Warning message:
In download.file(paste("http://finance.yahoo.com/d/quotes.csv?s=", :
URL 'http://download.finance.yahoo.com/d/quotes.csv?
s=QQQQ+SPY&f=d1t1l1': status was 'Couldn't resolve host name'

To answer the specific question asked:
getSymbols("QQQ;SPY", from="1997-12-31", src='yahoo')
works just fine - you get two xts series, QQQ and SPY (note that QQQQ is "dead" and replaced with QQQ), with 6 columns Open, High, Low, Close, Volume, Adjusted.
It looks like you only want the close? If so, you can get just that series with
Cl(QQQ)
If you don't like auto-assignment, you can use
qqq.data <- getSymbols("QQQ", auto.assign=FALSE, from="1997-12-31", src='yahoo')
to assign the downloaded data to a variable of your choice. Note that most people would probably be interested in the adjusted price instead, if so, you can select it with
Ad(QQQ)
Finally, indeed, as pointed out, google finance no longer provides data through its API.

getSymbols("AAPL",src="google") has been deprecated.
Error: ‘getSymbols.google’ is defunct.
Google Finance stopped providing data in March, 2018.
You could try setting src = "yahoo" instead.
See help("Defunct") and help("quantmod-defunct")

Related

YahooQF returns incorrect number of dimensions

I'm trying to get markt capitalization from brazil stocks, but I'm getting an error in mycode
This is my code
what_metrics <- yahooQF(c("Market Capitalization"))
getQuote("PINE3.SA", what=what_metrics)
This is the error I get
> Error in sq[, "exchangeTimezoneName"] : incorrect number of dimensions
How can I fix this?
The error likely comes from the fact that the yahoo data in quantmod does not look at stocks traded in Brazil - try e.g. "SPY" instead of "PINE3.SA" and see if that also gives you the error. You could perhaps download the Bovespa data and analyze it with quantmod - see: https://github.com/palencar/BovespaID.

How to decrease the amount of URL requests per second in RISmed R package?

I am currently using the R packages RISmed and bibliometrix to analyze publication data.
Here is my code:
library(bibliometrix)
library(RISmed)
search_topic <- "(((Dental Health Services[MESH]) OR (Dentistry[MESH] OR Dentists[MESH)) AND Research[MESH]) AND Research[MESH] AND (Computers[MESH] OR Medical Informatics[MESH] OR Information Technology[MESH]) "
search_query <- EUtilsSummary(search_topic, retmax=200, mindate=2014, maxdate=2020)
summary(search_query)
D <- EUtilsGet(search_query)
M <- pubmed2df(D)
Everything works great, but when I run the last line, I get the error:
In readLines(the_url) :
cannot open URL 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?retmode=xml&dbfrom=pubmed&id=31173232&cmd=neighbor': HTTP status was '429 Unknown Error'
It says in the documentation for the EUtilsGet function that users can post no more than 3 times/second:
In order not to overload the E-utility servers, NCBI recommends that
users post no more than three URL requests per second and limit large
jobs to either weekends or between 9:00 PM and 5:00 AM Eastern time
during weekdays. Failure to comply with this policy may result in an
IP address being blocked from accessing NCBI.
After consulting this post too, it became clear that I might be able to fix the solution by either restarting R and updating the package (both of which had no effect), or by modifying the code to increase the time between requests.
How might one increase the time between requests, as this seems like the only option for me in this case? If there are any other alternative solutions, those would also be appreciated as well.

quantmod - getQuote() - '403 Forbidden'

I found an answer to my own question (see below). Still need help.
In the same package, quantmod, there is an option called getSymbol.google.
Nevertheless,
If I use it to get Microsoft value, for example, it works all right
getSymbols.google('MSFT', environment() , src="google", from = (Sys.Date() - 1))
[1] "MSFT"
But, I can´t make it work on a currency pair;
getSymbols.google("GBPUSD", environment() , src="google", from = (Sys.Date() - 1))
Error in download.file(paste(google.URL, "q=", Symbols.name, "&startdate=", :
cannot open URL 'http://finance.google.com/finance/historical?q=GBPUSD&startdate=Nov+02,+2017&enddate=Nov+03,+2017&output=csv'
In addition: Warning message:
In download.file(paste(google.URL, "q=", Symbols.name, "&startdate=", :
cannot open URL 'http://finance.google.com/finance/historical?q=GBPUSD&startdate=Nov+02,+2017&enddate=Nov+03,+2017&output=csv': HTTP status was '400 Bad Request'
Any ideas?
Good morning,
Since the 1ts of November i´m having trouble with the function getQuote from Yahoo. Is a function inside the package "quantmod", which uses yahoo API to request the information.
The description of the function is as follows; Fetch current stock quote(s) from specified source. At present this only handles sourcing quotes from Yahoo Finance, but it will be extended to additional sources over time.
In r, i´m getting the following error; "HTTP status was '403 Forbidden'"
I´ve look on my browser and the error comes from the following error in Yahoo web page "Fetch current stock quote(s) from specified source. At present this only handles sourcing quotes from Yahoo Finance, but it will be extended to additional sources over time."
Does anybody know how to solve ir, or, any alternatives to the function getQuote()
Here is an example from RStudio
getQuote("AAPL")
Error in download.file(paste("https://finance.yahoo.com/d/quotes.csv?s=", :
cannot open URL 'https://finance.yahoo.com/d/quotes.csv?s=AAPL&f=d1t1l1c1p2ohgv'
In addition: Warning message:
In download.file(paste("https://finance.yahoo.com/d/quotes.csv?s=", :
cannot open URL 'https://finance.yahoo.com/d/quotes.csv?s=AAPL&f=d1t1l1c1p2ohgv': HTTP status was '403 Forbidden'
Thanks
seems that yahoo has discontinued this service. Anyone aware of a alternative for yahoo (I'd rather not have to webscrape yahoo for this)
rob
I ran into the same problem... it's kludgey but as a workaround to get the end-of-day value, I have found this to work for now:
Instead of getQuote() to get the Last price (which doesn't seem to work from Yahoo anymore):
underlying<-"AAPL"
quote.last <-getQuote(underlying)$Last
I use "getSymbols" which still works-- throws it into a new data frame, and I pull out the value I want from that:
Hx<-getSymbols(underlying,from=Sys.Date()-1) # allows me to not have to retain the ticker name if I do this across many tickers
quote.last<-as.double(tail(Cl(get(Hx)),1)) # Closing price value from last row of data
rm(list=Hx) # throw away the temporary data frame with quote history
I'm sure the's a more elegant way to do it, but this is what fell out of my brain as a quick workaround that got it done... sadly that doesn't get things like the Bid and Ask that getQuote does.

How to use getReturns with the Yahoo finance API

I have problems with R package getReturns. I have encountered this error since 17th May:
Warning in file(file, "rt") :
cannot open URL 'http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=4&b=28&c=2014&d=4&e=27&f=2017&g=w&ignore=.csv': HTTP status was '404 Not Found'
It looks like that ichart API did not run anymore. Can anyone help me with this issue? Does someone know how to fix it? I have encountered the same issue with the quantmod R package.
I have encountered this problem as well. Yahoo! has taken down ichart and the open-source libraries that rely on it are now broken. Yahoo! also has no plans to introduce a replacement. For more information, see this post on Yahoo!'s Forums.
I switched to eodhistoricaldata.com after Yahoo failed, several weeks ago I found good alternative with an API very similar to Yahoo Finance.
Basically, for almost all R scripts I use I just changed this:
URL <- paste0("ichart.finance.yahoo.com/table.csv?s=", symbols[i])
to:
URL <- paste0("eodhistoricaldata.com/api/table.csv?s=", symbols[i])
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.
You can follow my an earlier post, which might help you.
I tried :
library(quantmod)
# Create an object containing the Pfizer ticker symbol
symbol <- "PFE"
# Use getSymbols to import the data
getSymbols(symbol, src="yahoo", auto.assign=T)
# because src='google' throws error, yahoo was used, and even that is down
When I tried other source, it worked:
# "quantmod::oanda.currencies" contains a list of currencies provided by Oanda.com
currency_pair <- "GBP/CAD"
# Load British Pound to Canadian Dollar exchange rate data
getSymbols(currency_pair, src="oanda")
str(GBPCAD)
It seems there are issues with google and yahoo while we use quantmod pkg.
I will suggest you to use 'Quandl' instead. Plz goto Quandl website, register for free and create API key, and then copy it in below:
# Install Quandl
install.packages("Quandl")
# or from github
install.packages("devtools")
library(devtools)
install_github("quandl/quandl-r")
# Load the Quandl package
library(Quandl)
# use API for full access
Quandl.api_key("xxxxxx")
# Download APPLE stock data
mydata = Quandl::Quandl.datatable("ZACKS/FC", ticker="AAPL")
For HDFC at BSE, you can use:
hdfc = Quandl("BSE/BOM500180")
for more details:
https://www.quandl.com/data/BSE-Bombay-Stock-Exchange?keyword=HDFC

Can getSymbols still work with oanda?

I want to get the data of currencies and metals. As I tried some packages, many person suggest quantmod. So I used getSymbols as the following:
getSymbols("USD/EUR",src="oanda")
Error in download.file(paste(oanda.URL, from.date, to.date, "exch=", currency.pair[1], :
cannot open URL 'http://www.oanda.com/convert/fxhistory?lang=en&date1=09%2F28%2F13&date=02%2F09%2F15&date_fmt=us&exch=USD&expr2=EUR&margin_fixed=0&SUBMIT=Get+Table&format=CSV&redirected=1'
In addition: Warning message:
In download.file(paste(oanda.URL, from.date, to.date, "exch=", currency.pair[1], :
cannot open: HTTP status was '404 Not Found'
as I used:
getSymbols("USD/EUR",src="oanda",from="2015-01-01")
I get the same message.
So getSymbols can still work with Oanda?
And another question is where I can find the list of the symbols that the web-service such as Yahoo, Oanda, Google supported? In fact I don't need the stock symbols, I just need the symbols for the future such as corn, gold and the currency.
Oanda changed their URL structure and file format. I fixed this over the weekend. You would need to look on the website of each respective provider in order to find what symbols they support.

Resources