A little Nasdaq retriever

RS 2018-10-09 - Back after a long while. Retirement comes closer, and my old-age savings at the local savings bank yielded 0.00%, so (better late than never) I have taken up investing them, stocks and bonds and stuff.

Many resources are available for info about that, one of my favorites to find out about dividend history (and a little future) is nasdaq.com (for US stocks). I visit it often, but for automating my rudimental financial software (in Tcl), I wanted to retrieve the current data from it, like: when will the next dividend be paid? how much? When was the record date?

Simple beginner stuff. Here's what I did to pick the valuable data from the considerable line noise:

 #!/usr/bin/env tclsh
 set usage {
    usage: nasdaq.tcl symbol..
    
    Retrieves current dividend data from nasdaq.com for the given ticker symbols, to stdout.
 }
 if {[llength $argv] < 1} {puts stderr $usage; exit 1}

 proc main argv {
    package require http
    package require tls
    http::register https 443 [list ::tls::socket -tls1 1]

    foreach symbol $argv {puts [nasdaq $symbol]}
 }
 proc nasdaq symbol {
    set sym [string tolower $symbol] ;# nasdaq.com wants lowercase
    set url https://www.nasdaq.com/de/symbol/$sym/dividend-history
    set token [http::geturl $url]
    set map {Date "" CashAmount div}
    set buf [list symbol $symbol]
    foreach line [split [set $token\(body)] \n] {
        if [regexp {_lastsale.+>[$](.+?)<} $line -> price] {
            lappend buf price $price
        }
        if [regexp {span.+Grid_([A-Za-z]+)_0.>(.+?)<} $line -> type val] {
            lappend buf [string map $map $type] $val
        }
    }
    return $buf
 }
 main $argv

The "nasdaq" function is at the core, and it may be helpful for other requirements, like computing the TTM or forward yield (to be done later). So far, you get only the raw data, like if you looked at their web page.

Usage example:

 ~/Documents/onvista $ /c/Tcl/bin/tclsh ../../bin/nasdaq.tcl MO PM
 symbol MO price 63.2 exdate 13.09.2018 div 0.8 Decl 23.08.2018 Rec 14.09.2018 Pay 10.10.2018
 symbol PM price 84.98 exdate 25.09.2018 div 1.14 Decl 12.09.2018 Rec 26.09.2018 Pay 12.10.2018

Jorge - 2018-10-09 19:48:07

I am glad seeing a new post from you, I have learned a lot from your commented code. Thanks.


RS 2018-10-09: Thank you! Let's work together to build a useful toolbox for financial Tcl... :^)


JM 2018-10-09: yes! please let me know if there is anything I can do to help