Get historical market data by API from Yahoo!finance for German and American stocks
Manually retrieving historical stock market data can be quite annoying if you need more than a dozen values or if you need to update them regularly.
The task would look like this:
- go to ARIVA.DE to find the historical data for the chosen stock, e.g. SAP: https://www.ariva.de/sap-aktie/historische_kurse
- find the box „Kurse als CSV-Datei“, enter start date, end date and delimiter and then press ‚Download‘
Pretty easy if you need data for one stock. Not so easy if you want to download 30 DAX values or 60 MDAX values. Impossible if you want the S&P 500.
Retrieving stock data from an API offers great advantages compared to a csv download:
- avoid manual tasks
- repeatable script
- get many time series fully automated
So let‘s create a script to automatically download stock quote time series for DAX and MDAX values.
This is where Yahoo!finance comes into play.
There is a pretty easy way to use the API for the Yahoo!finance data called yfinance: this API is created by Ran Aroussi and available from PyPI: https://pypi.org/project/yfinance/
After installing yfinance using pip we simply need the following lines to retrieve historical data for a given stock:
import yfinance as yf
symbol = 'aapl'
ticks = yf.Ticker(symbol)
h = ticks.history(period='10y')
We might only need the close values so our code would look like this:
history = h['Close']
If you need a CSV file for later use you can create it by the following lines:
path='out/'
history.to_csv(path+symbol+'.csv', sep=';', header=True)
The only problem for German stocks is:
Where do we find the symbols we use to retrieve the data?
If you don‘t know the symbols, www.dax-indices.com will help you:
Here we find the details and data for the DAX. Go there, find the link ‚Daten‘ and from the list ‚Zusammensetzung und Indikatoren‘ download the DAX file. In this Excel-sheet find the column ‚Trading Symbol‘, copy the 30 Values for the DAX Performance Index into an editor, append each symbol with .DE and save this list to a separate file.
To retrieve the values for other indices simply change the isin parameter in the url:
- MDAX: DE0008467416
- TecDAX: DE0007203275
- CDAX: DE0008469602
- DAX 50 ESG: DE000A0S3E87
The S&P500 list can be found at datahub.io: https://datahub.io/core/s-and-p-500-companies#resource-constituents
If you need ticker symbols for individual other companies simply try to enter the name at https://finance.yahoo.com/ and find the symbol there.
The corresponding code for this example in my repository reads the DAX index list, retrieves 1 year of historical stock market data for each entry and saves the values to csv-files named after the ticker symbols.