#! /usr/bin/perl # # FXData # manages all data regarding currencies # # package FXData; #use LWP::Simple; use strict; my $URL = "http://finance.yahoo.com/d/quotes.csv?s=%s=X&f=sl1d1t1ba&e=.csv"; # FXData(fx rate file name, currency file name) sub new { my $class = shift; my $self = {}; $self->{rate_file} = shift; # fx transactions file $self->{cur_file} = shift; # currencies file # listing of currency symbols (keys) # and descriptions (values) $self->{listing} = {}; # just the currency symbols. $self->{symbols} = []; # table of all fx transactions # available. $self->{spot_table} = {}; $self->{field_sep} = ":"; bless ($self, $class); $self->load(); return $self; } # set_rate_file(rate file name) sub set_rate_file { my $self = shift; $self->{rate_file} = shift; } # set_currency_file(currency file name) sub set_currency_file { my $self = shift; $self->{cur_file} = shift; } # down_load() # download data from Yahoo! finance. sub down_load { my $self = shift; my @symlist = @{$self->{symbols}}; my %fx_table; my $from_sym; foreach $from_sym (@symlist) { my $to_sym; foreach $to_sym (@symlist) { next if $from_sym eq $to_sym; my $url = sprintf $URL, $from_sym . $to_sym; my @data = split ",", `./wget.py "$url"`; $data[0] =~ /"(.*?)\=/; my $pair = $1; # pairs of currencies like USDJPY my $rate = $data[1]; # spot rate $fx_table{$pair} = $rate; } } $self->{rate_table} = {%fx_table}; $self->dump_spotrates(); } # utility method for writing a hash table into a file sub write_table { my $self = shift; my $filename = shift; my %data = @_; open FILE, ">$filename" || die $!; my $key; while ($key = each %data) { print FILE "$key", $self->{field_sep}, $data{$key}, "\n"; } close FILE; } # utility method for reading a table from # a file and loading it into a hash data structure. sub read_table { my $self = shift; my $filename = shift; open FILE, $filename || die "could not open file $filename$!"; my %table = (); while () { chomp; my ($key, $val) = split $self->{field_sep}; $table{$key} = $val; } close FILE; return %table; } # load_currencies() # loads currency file sub load_currencies { my $self = shift; $self->{listing} = {$self->read_table($self->{cur_file})}; $self->{symbols} = [keys %{$self->{listing}}]; } # load_spotrates() # loads fx rates file sub load_spotrates { my $self = shift; $self->{rate_table} = {$self->read_table($self->{rate_file})}; } # dump_currencies() # writes currency file sub dump_currencies { my $self = shift; $self->write_table($self->{cur_file}, %{$self->{listing}} ); } # dump_spotrates() # writes rates file sub dump_spotrates { my $self = shift; $self->write_table($self->{rate_file}, %{$self->{rate_table}} ); } # load() # loads all files and downloads # data if none exists sub load { my $self = shift; $self->down_load() unless -e $self->{rate_file}; $self->load_currencies(); $self->load_spotrates(); } # # Public Functions # # sync_rates() # updates fx rates sub sync_rates { my $self = shift; unlink $self->{rate_file}; $self->down_load(); $self->dump_spotrates(); } # currency_listing() # returns a hashtable of currency # listings of the format { SYMBOL => DESCRIPTION } sub currency_listing { my $self = shift; return $self->{listing}; } # rate_table() # returns a hash table of currency pairs # and their exchange rate in the format # { S1S2 => Rate } # S1 = currency exchanging from # S2 = currency exchanging to sub rate_table { my $self = shift; return %{$self->{rate_table}}; } 1;