1

I am looking for a free API to retrieve the daily end-of-day S&P 500 index for the past year.

There are plenty of APIs which provide the individual stocks, but the indices are not for free. There are also many historic S&P 500 index datasets, but they do not cover the very recent couple of days.

1

The official website of the index is https://www.spglobal.com/spdji/en/indices/equity/sp-500/#overview.

If you hover with your mouse over Export above the graph, then you can recover a link, with which I can download the last ten years' worth of closing prices up until yesterday in XLS format (I've removed the hostIdentifier part):

https://www.spglobal.com/spdji/en/idsexport/file.xls?redesignExport=true&selectedModule=PerformanceGraphView&selectedSubModule=Graph&yearFlag=tenYearFlag&indexId=340

  • Thanks. But I need a REST API, csv or json. Or can you please explain how you imported the data with Python or R? Thanks. – lars20070 Sep 24 '20 at 17:33
  • 1
    In Python, you can download files using the package urllib.request. Using Pandas, you can easily open XLS files: pd.read_excel(). There you probably need the parameters header, index_col. – Bence Mélykúti Sep 24 '20 at 19:58
0

Sorry that I have to "answer" you question when I should actually "comment" to ask if you're okay with using a python module instead of a REST API (not enough reputation).
Anyway, I love playing with stock data, here is my method for getting it for free in python and exporting it to a csv.

pip install yfinance

import yfinance as yf
import pandas as pd

sp500 = yf.Ticker('^GSPC')
sp500_hist = sp500.history(period='1y')
sp500_hist.to_csv('sp500_1y.csv')

This will provide you with daily data, open high low close and volume. Not sure how familiar you are with pandas, but if you only want closing prices, subset your dataframe like this:

sp500_hist[['Close']].to_csv('sp500_1y.csv')

or if you want volume as well:

sp500_hist[['Close', 'Volume']].to_csv('sp500_1y.csv')
  • Thanks @rangeseeker. I previously looked into yfinance. But since I am working from R, I would prefer a REST API. – lars20070 Sep 25 '20 at 10:50
  • yfinance is certainly the best way to get the S&P 500 index in Python. Yahoo decommissioned their REST API and yfinance somehow fixed that. Wrapping yfinance with reticulate might be the best way to get the S&P 500 into R. Seems there is no clear REST API. – lars20070 Sep 26 '20 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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