Download the last year of dividends on Nasdaq with Python and Yahoo_fin.
Yahoo_fin is a Python 3 package designed to extract historical stock price data, as well as provide current information about market caps, dividend yields and the stocks that make up the main exchanges. Among its main features we have:
Collection of income statements, balance sheets and cash flows
Company information
Analyst data
Real-time stock prices
Cryptocurrency data retrieval
Getting the most actively traded stocks during the current trading day
Retrieving option prices and expiration dates
Tracking earnings calendar history
Financial news RSS feeds
In this article we will focus on downloading fiscal information. More specifically, we will explain how to obtain dividend history for a given ticker and store the result in a `.csv` file for later consultation.
Package installation and environment preparation.
To carry out the project, the first step is to run the following command in order to download the project from the GitHub repository.
Once the project has been downloaded, the next step is to configure our Python programming environment by installing all the libraries required by the project.
The project we are going to work with is stored in the directory obtener_historico_dividendos and is composed of two files: historico_dividendos.py and tickers.csv. The file tickers.csv stores the list of tickers, that is, the company abbreviations to be queried. The file format must be as shown below:
The file historico_dividendos.py contains the code explained in the next section.
Explanation of the file historico_dividendos.py
This file contains the following code:
In the code shown above, the first part corresponds to loading the file `tickers.csv` with the Python pandas library. The code is as follows:
The code continues by creating an empty dataframe where all the information provided by Yahoo_fin will be stored. From this point onward, all we do is iterate according to the number of values stored in the `tickers.csv` file.
At this point it is important to analyze what kind of information is provided by the method get_dividends(). For that purpose, here is the following table:
dividend
ticker
2019-08-26
0.95
JNJ
2019-11-25
0.95
JNJ
2020-02-24
0.95
JNJ
2020-05-22
1.01
JNJ
2020-08-24
1.01
JNJ
2020-11-23
1.01
JNJ
2021-02-22
1.01
JNJ
In addition, we do not need the full historical series: the most recent dividends are enough. For that reason we create the variable `fecha_de_inicio`, which contains the result of subtracting 600 days from today's date. Once that operation is completed, we simply perform the query in the following way:
The result is added to the dataframe created at the beginning. It is important to sort it by date.
The result is stored in a file called resultados_dividendos.csv with the following format: