C# Historical Dividend retrieval

Today in SmileOfThales we will provide you some brief but useful C# code to retrieve historical cash dividend data in Excel (the whole code is available at the end of the article ).

The topic covers Excel-Dna, data caching, Html parsing with HtmlAgilityPack … that’s it and it’s already pretty 🙂

At the very beginning I needed to retrieve dividend history to experiment implicit dividend solving: implicit the future dividend ex-div dates from the previous ex-div dates (assuming a regular schedule), and use the latest cash dividend as a starting point for my solver.

Now since I work a lot in Excel, I could copy paste it manually from the web page into Excel but it revealed to be painful, and I wanted to do it for several tickers, so …  I finally decided to retrieve directly this data via Excel-Dna, which offers some nice .Net integration into Excel, with easy asynchrony.

Some websites, but only a few, offer free dividend time series: http://www.dividend.com/, http://www.nasdaq.com/ … I opted for Nasdaq.com whose layout focuses on the essential information.

Ex for Caterpillar, the dividend history can be obtained at http://www.nasdaq.com/fr/symbol/cat/dividend-history

C# Historical dividend

Source: www.nasdaq.com

WebClient response and data caching

Below is the synchronous static method that returns our historical dividends.


We use the following method in order to call Nasdaq website (the url format is http://www.nasdaq.com/en/symbol/{0}/dividend-history) and store it in a local textfile.

We make the choice to have a single storage file per ticker, and user has to force retrieval in order to make a call to the server (otherwise dividends are taken from the cache).

Html parsing with HtmlAgilityPack

The Nasdaq website returns a long Html response, but at the very end we can catch the table markup of our consolidated coupon history:

C# dividend history

HTML Markup

In order to parse the elements containing each dividend detail, I used a .Net library called HtmlAgilityPack that makes it easier to parse such Html document, which reveals trickier than deserializing a JSON response.

This is the section of code related to Html parsing:

Excel asynchronous call

As it’s always unpleasant to freeze Excel for a long calculation (here data retrieval), we expose an asynchronous in Excel, instead of the previous one:

C# Historical Dividend

Excel function

Here is the shape of our cash dividend time series for CAT:

C# Historical Dividend

CAT Historical Dividends


In a future article we will show you how you can retrieve option quotes from Google Finance and export it to Excel.