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
When this dividend script is useful
The script is useful when you want a repeatable dataset for later analysis instead of copying dividend tables by hand. By keeping the ticker list in a CSV file, the process can be repeated with another group of companies and the output remains ready for pandas, spreadsheets or a dashboard.
This tutorial complements other financial-data articles such as what a stock market is and the Bitcoin reputation and sentiment thesis.
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 |
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: | CB | AWK | SCU | JNJ | MO | |
|---|---|---|---|---|---|
| 2021-03-24 | 0.86 | ||||
| 2021-03-18 | 0.78 | ||||
| 2021-02-24 | 2.35 | ||||
| 2021-02-22 | 1.01 | ||||
| 2021-02-05 | 0.55 | ||||
| 2020-12-24 | 0.86 | ||||
| 2020-12-17 | 0.78 | ||||
| 2020-11-23 | 1.01 | ||||
| 2020-11-09 | 0.55 | ||||
| 2020-09-17 | 0.78 | ||||
| 2020-09-14 | 0.86 | ||||
| 2020-08-24 | 1.01 | ||||
| 2020-08-11 | 0.55 | ||||
| 2020-06-18 | 0.78 | ||||
| 2020-06-12 | 0.84 | ||||
| 2020-05-22 | 1.01 | ||||
| 2020-05-11 | 0.55 | ||||
| 2020-03-24 | 0.84 | ||||
| 2020-03-19 | 0.75 | ||||
| 2020-02-24 | 0.53 | ||||
| 2020-02-24 | 0.95 | ||||
| 2020-02-06 | 0.5 | ||||
| 2019-12-24 | 0.84 | ||||
| 2019-12-19 | 0.75 | ||||
| 2019-11-25 | 0.95 | ||||
| 2019-11-15 | 0.03 | ||||
| 2019-11-08 | 0.5 | ||||
| 2019-09-19 | 0.75 | ||||
| 2019-09-13 | 0.84 | ||||
| 2019-08-26 | 0.95 | ||||
| 2019-08-13 | 0.32 |
If you wish, you can also consult the following explanatory video https://www.youtube.com/watch?v=v3LNklgOvvc: