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:

dividendticker
2019-08-260.95JNJ
2019-11-250.95JNJ
2020-02-240.95JNJ
2020-05-221.01JNJ
2020-08-241.01JNJ
2020-11-231.01JNJ
2021-02-221.01JNJ

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:

CBAWKSCUJNJMO
2021-03-240.86
2021-03-180.78
2021-02-242.35
2021-02-221.01
2021-02-050.55
2020-12-240.86
2020-12-170.78
2020-11-231.01
2020-11-090.55
2020-09-170.78
2020-09-140.86
2020-08-241.01
2020-08-110.55
2020-06-180.78
2020-06-120.84
2020-05-221.01
2020-05-110.55
2020-03-240.84
2020-03-190.75
2020-02-240.53
2020-02-240.95
2020-02-060.5
2019-12-240.84
2019-12-190.75
2019-11-250.95
2019-11-150.03
2019-11-080.5
2019-09-190.75
2019-09-130.84
2019-08-260.95
2019-08-130.32

The code used is:

If you wish, you can also consult the following explanatory video https://www.youtube.com/watch?v=v3LNklgOvvc: