=head1 NAME

Finance::Shares::Lesson1 - Fetching quotes

=head1 DESCRIPTION

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

This script is '01fetch.pl' in the F<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 (*).

=over 4

=item 1.

Change B<stock_code>, B<start_date> and B<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.
L<http://uk.finance.yahoo.com> for more.

=item 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 C<mode>:

    mode => 'cache',

The other mode setting is 'online'.

=item 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;

=item 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

=item 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,
    );
    
B<NB:> When running the script, remember to put all dates inside quotes.  Otherwise perl will interpret 2003-05-13
as a subtraction sum.

=item 6*.

Use the mysql client to access the fetched data directly.

S< >   $ B<mysql -u test -p>

S< >   Enter password: B<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.

S< >   mysql> B<use test;>

S< >   mysql> B<select * from i0;>

Amongst others, there should be an entry for StockQuotes.  Use B<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 F<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.

S< >   $ B<01list.pl>

S< >   $ B<01list.pl StockQuotes>

S< >   $ B<01list.pl StockQuotes::L>

=back

=cut

