oercim
oercim

Reputation: 1848

I can't get the sp500 data from google finance using quantmod package

I cant get the sp500 data fro google finance using quantmod package using R.

The stock's web page is:

[https://finance.google.com/finance?q=INDEXSP%3A.INX&sq=sp500&sp=1&ei=OPzYWaqcDtOBsAGDq674BQ][1]

I am using the below code:

library(quantmod)
GSPC <- getSymbols(".INX",auto.assign = FALSE, from = "2010-01-01",src="google")

However, I am getting the below error:

Error in download.file(paste(google.URL, "q=", Symbols.name, "&startdate=", :
cannot open URL 'http://finance.google.com/finance/historical?q=.INX&startdate=Jan+01,+2010&enddate=Oct+07,+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=.INX&startdate=Jan+01,+2010&enddate=Oct+07,+2017&output=csv':
HTTP status was '404 Not Found'

I guess it is because of the symbol of the stock. I tried a lot different symbols(it is .INX in the above code). However, I couldn't manage to download the data.

Upvotes: 1

Views: 2013

Answers (2)

AG1
AG1

Reputation: 6774

Another option is to use src="yahoo".
Note that the symbol name needs to be switched from .INX to ^GSPC.

GSPC <- getSymbols("^GSPC", auto.assign=FALSE, from="2010-01-01", src="yahoo")
GSPC[1:2]
           GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
2010-01-04   1116.56   1133.87  1116.56    1132.99  3991400000       1132.99
2010-01-05   1132.66   1136.63  1129.66    1136.52  2491020000       1136.52

Upvotes: 0

hvollmeier
hvollmeier

Reputation: 2986

One easy way to get the historical data of all SP500 stocks is by loading the tidyquant package.

tq_index(“SP500") %>% # get the index components
slice(1:3) %>%  # this example will get you the first 3 SP500 stocks, uncomment this line if you want all 500 stocks
tq_get(get = "stock.prices”)

Have a look at one of the very detailed and helpful tidyquant vignettes . If you want/need xts- objects tidyquant offers many ways to switch between tibble and xtsobjects so that you get all the functionality of the quantmod package if needed.

Upvotes: 1

Related Questions