$ cat moveaverage2.pl #!/usr/bin/perl -w #use strict; push(@Inc,'pwd'); #require Money::Finance; use Money::Finance; @values = ( 12,22,23,24,21,23,24,23,23,21,29,27,26,28 ); @mv = (0,10000); $size = scalar(@values); print "\n Values to work with = { @values } \n"; print " Number of values = $size \n"; #use strict; # ---------------------------------------------------------------- # Calculate the average of the above function # ---------------------------------------------------------------- my $cup = Money::Finance::new(); my $cup1 = new Money::Finance; $ave =Money::Finance::getLastAverage(5,$size,@values); print "\n Average of last 5 days = $ave \n"; Money::Finance::getMovingAve(5,$size,@values,@mv); print "\n Moving Average with 5 days window = \n { @mv } \n"; $ $ $ cd Money $ $ $ cat Finance.pm package Money::Finance; require Exporter; @ISA = (Exporter); @EXPORT = qw( new FutureValue PresentValue FVofAnnuity AnnuityOfFV getLastAverage getMovingAve SetInterest); # # Globals, if any # local $defaultInterest = 5.0; sub SetInterest($) { my $rate = shift(@_); $defaultInterest = $rate; printf "\n \$defaultInterest = $rate"; } # ------------------------------------------------------------------ # Notes: # 1. The interest rate $r is given in a value of [0-100]. # 2. The $n given in the terms is the rate at which the interest # is applied. # # ------------------------------------------------------------------ # ------------------------------------------------------------------ # Present value of an investment given # fv - a future value # r - rate per period # n - number of period # ------------------------------------------------------------------ sub FutureValue($$$) { my ($pv,$r,$n) = @_; my $fv = $pv * ((1 + ($r/100)) ** $n); return $fv; } # ------------------------------------------------------------------ # Present value of an investment given # fv - a future value # r - rate per period # n - number of period # ------------------------------------------------------------------ sub PresentValue($$$) { my $pv; my ($fv,$r,$n) = @_; $pv = $fv / ((1 + ($r/100)) ** $n); return $pv; } # ------------------------------------------------------------------ # Get the future value of an annuity given # mp - Monthly Payment of Annuity # r - rate per period # n - number of period # ------------------------------------------------------------------ sub FVofAnnuity($$$) { my $fv; my $oneR; my ($mp,$r,$n) = @_; $oneR = ( 1 + $r) ** $n; $fv = $mp * ( ($oneR - 1)/ $r); return $fv; } # ------------------------------------------------------------------ # Get the annuity from the following bits of information # r - rate per period # n - number of period # fv - Future Value # ------------------------------------------------------------------ sub AnnuityOfFV($$$) { my $mp; # mp - Monthly Payment of Annuity my $oneR; my ($fv,$r,$n) = @_; $oneR = ( 1 + $r) ** $n; $mp = $fv * ( $r/ ($oneR - 1)); return $mp; } # ------------------------------------------------------------------ # Get the average of the last "n" values in an array. # ------------------------------------------------------------------ # The last $count number of elements from the array in @values # The total number of elements in @values is in $number # sub getLastAverage($$@) { my ($count, $number, @values) = @_; my $i; my $a = 0; return 0 if ($count == 0); for ($i = 0; $i< $count; $i++) { $a += $values[$number - $i - 1]; } return $a / $count; } # ------------------------------------------------------------------ # Get a moving average of the values. # ------------------------------------------------------------------ # The window size is the first parameter, the number of items in the # passed array is next. (This can easily be calculated within the # function using the scalar() function, but the subroutine shown here # is also being used to illustrate how to pass pointers.) # The reference to the array of values is passed next, followed by a # reference to the place the return values are to be stored. # sub getMovingAve($$\@\@) { my ($count, $number, $values, $movingAve) = @_; my $i; my $a = 0; my $v = 0; return 0 if ($count == 0); return -1 if ($count > $number); return -2 if ($count < 2); $$movingAve[0] = 0; $$movingAve[$number - 1] = 0; for ($i=0; $i<$count;$i++) { $v = $$values[$i]; $a += $v / $count; $$movingAve[$i] = 0; } for ($i=$count; $i<$number;$i++) { $v = $$values[$i]; $a += $v / $count; $v = $$values[$i - $count - 1]; $a -= $v / $count; $$movingAve[$i] = $a; } return 0; } sub new { my $this = {}; # Create anonymous hash, and #self points to it. print "\n i am in new \n"; bless $this; # Connect the hash to the package Cocoa. return $this; # Return the reference to the hash. } 1; $