NAME

Finance::Shares::Lesson1 - Fetching quotes

DESCRIPTION

IMPORTANT: If you haven't already done so, please read "Tutorial" in Finance::Shares::Overview and carry out the instructions in the Preparation section which follows it.

This script is '01fetch.pl' in the tutorial directory of the Finance::Shares package.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Finance::Shares::MySQL;

    # Create MySQL object giving access to the data
    my $db = new Finance::Shares::MySQL(
	user     => 'test',
	password => 'test',
	database => 'test',
	debug    => 2,
    );

    # Fetch the data from the internet
    my @data = $db->fetch(
	symbol     => 'MSFT',
	start_date => '2003-01-01',
	end_date   => '2003-01-31',
    );
    die 'No data fetched' unless @data;

    # Print out the data so you can see it's there
    foreach my $row (@data) {
	my ($date, $open, $high, $low, $close, $volume) = @$row;
	printf('%s %6.2f,%6.2f,%6.2f,%6.2f, %d %s',
	$date, $open, $high, $low, $close, $volume, "\n");
    }

Most scripts follow the same format. Create the objects, do the necessary and show the results. Note that you will need to be online before you run it.

Run it, then try these tasks. Those that are a little more challenging are marked with an asterisk (*).

  1. Change stock_code, start_date and end_date in the fetch() call at line 14 before running it again. The stock codes can be from any country e.g. HPQ from New York, BSY.L from London or 12017.PA from Paris. See e.g. http://uk.finance.yahoo.com for more.

  2. Add another hash key to the fetch() call at line 14:

    mode => 'offline',

    Extend the dates and run the script. You should find that no internet access is attempted and only previously fetched data is returned. Now reset the mode:

    mode => 'cache',

    The other mode setting is 'online'.

  3. Have you noticed that the data is not fetched in any particular order? Add the following line before the data is printed (line 22) and run the script again.

    @data = sort { $a->[0] cmp $b->[0] } @data;
  4. Save the quotes to a CSV file. You will need to add a comma and remove some spaces in the printf statement (around line 24), then run the script with the command line:

    $ 01fetch.pl > quotes.csv
  5. 5*.

    Change the 01fetch.pl script so that it fetches any set of quotes and stores them in a suitably named CSV file. You may be able to make use of the Getopt::Long module:

        use Getopt::Long;
    
        my $start_date;
    
        GetOptions(
    	'start_date=s'	=> \$start_date,
        );
  6. 6*.

    Use the mysql client to access the fetched data directly.

    $ mysql -u test -p

    Enter password: test Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 to server version: 3.23.52-Max-log

    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

    mysql> use test;

    mysql> select * from i0;

    Amongst others, there should be an entry for StockQuotes. Use select to insect the table name (beginning with 'i') listed under 'sqlname'.

    It should show you index tables for exchanges you have fetched data from. Look into one of the exchanges and inspect the mysql tables with names like 't23'.

    Try using commands like the following. ('nopager' is the opposite to 'pager').

        mysql> pager;
    
        mysql> select * from t23
            -> where qdate >= '2002-06-01'
    	-> and qdate < '2002-10-01'
    	-> order by qdate;

    An additional script 01list.pl in the tutorials directory lists the mapping of mysql table names to the internal name which may be a little more descriptive. Try it out. It can be used like this.

    $ 01list.pl

    $ 01list.pl StockQuotes

    $ 01list.pl StockQuotes::L

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 80:

Expected '=item 5'

Around line 93:

Expected '=item 6'