#! /usr/bin/perl # # FXEngine # a bridge between the fxengine OCaml application # and the rest of the FX Arbitrage application # # package FXEngine; use FXData; use strict; use constant USERDIR => "/tmp/"; # tempfile() -> temporary file name sub tempfile { my $tmpfile; do { my $mid = $$ . srand(9999) . getppid(); $tmpfile = USERDIR . "tmp$mid"; } until (! -e($tmpfile) ); return $tmpfile; } # FXEngine(FXData(), array) # FXData - FXData object # array - array of currencies to be used to # create arbitrage transactions. sub new { my $class = shift; my $self = {}; $self->{fxdata} = shift; $self->{currency} = shift; $self->{limit} = 10; $self->{positive_only} = 0; bless ($self, $class); return $self; } # set_limit(int) # the number of transactions returned by get_transactions sub set_limit { my $self = shift; $self->{limit} = shift; } # positive_only() # if called get_transactions # only returns positive fx transactions. sub positive_only { my $self = shift; $self->{positive_only} = 1; } # get_transactions() -> string # returns fx transactions sub get_transactions { my $self = shift; my $list = join " ", @{$self->{currency}}; my %table = $self->{fxdata}->rate_table(); # create tempfile for fxengine data file my $tmpfile = tempfile() . ".fxe"; open TMPFILE, ">$tmpfile" || die $!; print TMPFILE "$list\n"; my ($key, $value); while (($key, $value) = each %table) { print TMPFILE "$key" . " " . $table{$key} . "\n"; } close TMPFILE; # arguments to fxengine my @args = (); push(@args, "-p") if $self->{positive_only}; my $sargs = join " ", @args; open ENGINE, "./fxengine $tmpfile $sargs |" || die $!; my @records = ; close ENGINE; unlink $tmpfile; return @records[0..($self->{limit}-1)];; } 1;