Finance::NASDAQ::Quote - Fetch real time stock quotes from nasdaq.com


Finance-NASDAQ-Quote documentation Contained in the Finance-NASDAQ-Quote distribution.

Index


Code Index:

NAME

Top

Finance::NASDAQ::Quote - Fetch real time stock quotes from nasdaq.com

VERSION

Top

Version 0.06

SYNOPSIS

Top

Fetch real time stock quotes from nasdaq.com

    use Finance::NASDAQ::Quote qw/getquote/;

    my %quote = getquote('F');
    my $text  = getquote('V');

EXPORT

Top

None by default.

FUNCTIONS

Top

getquote( SYMBOL )

In list context, returns a hash containing the price, net change, percent change, sign ('+' or '-'), and volume for stock SYMBOL, or an empty list on error.

In scalar context, returns a formatted string suitable for human consumption, or undef on error.

AUTHOR

Top

Ian Kilgore, <iank at cpan.org>

BUGS/TODO

Top

nasdaq.com (and hence getquote) returns curiously formatted strings, rather than numbers. getquote should 'numify' these.

It is likely that nasdaq.com will be changing their site some time in 2009. Watch for updates.

The module lacks many tests. getquote could be made more modular so as to avoid requiring an internet connection to test it.

Please report any bugs or feature requests to bug-finance-nasdaq-quote at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-NASDAQ-Quote. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Finance::NASDAQ::Quote




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-NASDAQ-Quote

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Finance-NASDAQ-Quote

* CPAN Ratings

http://cpanratings.perl.org/d/Finance-NASDAQ-Quote

* Search CPAN

http://search.cpan.org/dist/Finance-NASDAQ-Quote/

COPYRIGHT & LICENSE

Top


Finance-NASDAQ-Quote documentation Contained in the Finance-NASDAQ-Quote distribution.
package Finance::NASDAQ::Quote;

use warnings;
use strict;

use HTML::TreeBuilder;
use LWP::Simple qw($ua get);
$ua->timeout(15);

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw/getquote/;

our $VERSION = '0.06';


sub getquote {
    my ($symbol,$ua) = @_;
    my $url = "http://www.nasdaq.com/aspx/nasdaqlastsale.aspx?symbol=$symbol&selected=$symbol";
    my @ids = qw/_LastSale _NetChange _PctChange _Volume/;
    my $content;
    if (defined $ua) {
        my $resp = $ua->get($url);
        $content = $resp->content() if $resp->is_success();
    } else {
        $content = get $url;
    }
    warn "NASDAQ is down" and return unless defined $content;

    my $tree = HTML::TreeBuilder->new;
    $tree->parse($content);

    my %quote;
    @quote{qw/prc net pct vol/} = map { _findspan($tree, $_) } @ids;

    my $img = $tree->look_down('_tag', 'img', _id('_updownImage'));
    if ($img) {
        my ($color) = ($img->attr('src') =~ /(\w+)ArrowSmall/);
        $quote{sgn} = $color eq 'green' ? '+' : '-';
    } else {
        warn "Failed to locate updownimage";
        $quote{sgn} = undef;
    }

    ($quote{nam}) = ($tree->find('title')->as_text() =~ /^([^(]+) \(\S+\)/);
    if (defined $quote{nam}) {
        $quote{nam} =~ s/ +$//g;
    } else {
        warn "Could not parse title";
    }

    $tree = $tree->delete();

    return if grep {not defined} values %quote;
    return wantarray ? %quote : _as_text($symbol, %quote);
}

# for look_down
sub _id {
    my $id = shift;
    return sub {
        my ($tag) = @_;
        if (defined $tag->attr('id')) {
            return $tag->attr('id') eq $id;
        } else {
            return 0;
        }
    }
}

sub _findspan {
    my ($tree,$id) = @_;
    my $elem = $tree->look_down('_tag', 'span', _id($id));
    if (defined $elem) {
        return $elem->as_text;
    } else {
        warn "Could not find span $id";
        return undef;
    }
}

# format %quote as a string
sub _as_text {
    my ($symbol,%quote) = @_;
    return sprintf ("%s (%s): \$%g, %s%s (%s%s), vol %s", $quote{nam},
                            $symbol, @quote{qw/prc sgn net sgn pct vol/});
}

1;