Cocoa vs Perl

I have spent about three months off and on (OK: almost exclusively off with the occasional evening once every fortnight on) trying to write a very simple app in Objective-C and Cocoa (with me new MacBook Pro). The idea was to take a selection of stocks, and:

  • Mark them to market — i.e. state the current value and the gain or loss since they were bought
  • Mark them to an equivalent selection of indices according to the 3-factor model. This states that a stock’s size, value vs growth status, and market are the determinant of about 96% of the stock’s price.
  • Maybe extend to back test using historical data
  • Maybe extend to non-equity securities, e.g. bonds, forex or commodities

In that time, I have managed to learn a good deal of Objective-C, X-Code, Cocoa, Foundation and even a little bit of the quixoticly wonderful Core Data. However, very little real working code is coming out the other side.

My brother said: never mind that, choose a different language. We were talking Python, Ruby or Perl. Since I have a pretty long background in Perl hacking (but stopped a few years ago along with the rest of coding), I tried that. Within a day, I have a program that takes an associated array of stocks, prices and share quantities, retrieves todays prices and calculates the gain or loss. CPAN has come on a lot since I last check it out, plus having a Unix shell seems to make everything a lot easier. Half the day was spent with CPAN getting necessary modules working (hint for complete lusers: get your root account working before installing). Two hours were spent learning TextMate (Mac users: don’t hesitate, download it now). And the rest of the time reminding myself how just hashes worked. Result:

#!/usr/bin/perl

use Finance::Quote;
my @funds = (”DFVEX“, ”DFREX“,”DFCEX“, ”DFIEX“);
my %quotes = Finance::Quote->new()->yahoo (@funds);
my %costs = (‘DFVEX’,11.46,’DFREX’,32.25,’DFCEX’,14.08,’DFIEX’,12.23);
my %shares = (‘DFVEX’,100,’DFREX’,100,’DFCEX’,100,’DFIEX’,100);
my $total=0;
my $gain=0;

# print stats
foreach $f (@funds) {
my $name = $quotes {$f, ”name“};
my $price= $quotes {$f, ”price“};
my $cost = $costs{$f};
my $share=$shares{$f};
my $value=$price*$share;
$total+=$value;
my $profit = $price-$cost;
$gain+=$profit*$share;
my $percent= $profit/$cost;

print ”Fund $f $name \tPROFIT = $profit \tCOST = $cost \tPRICE = $price\tGAIN = $percent \tVAL=$value\n“;
}
print ”\n\nTOTAL=$total\nGAIN=$gain\n“;

Not complex, but goodness gracious me, it actually works!

Technorati Tags: , , , , ,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: