Skip to content

Instantly share code, notes, and snippets.

@blippy
Created May 5, 2019 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blippy/b76b80aa6477ccf73b53eae91ca160bd to your computer and use it in GitHub Desktop.
Save blippy/b76b80aa6477ccf73b53eae91ca160bd to your computer and use it in GitHub Desktop.
Accounts package in perl
#!/usr/bin/env perl
#use Date::Manip;
use Time::Piece;
use POSIX;
my %gaaps;
my %qtys;
my $start_date = "2019-04-06";
my $pracc; # the default account we want to print
my $tacc = 0; # the total for that account
mkdir "out";
sub fopen {
my $fname = shift(@_);
$fname = "out/$fname";
open(my $fp, ">", $fname) or die "Cannot open file:$fname.";
return $fp;
}
#open(my $fcgt, ">", "out/cgt.txt") or die "Cannot open cgt file";
$fcgt = fopen("cgt.txt");
$fgaap = fopen("gaap.txt");
$fstocko = fopen("stocko.csv");
printf $fstocko "TICKER,DATE,TIME,TYPE,SHARES,FX,PRICE,CURRENCY,COMMISSION,TAX,TOTAL\n";
sub split1 {
my ($str, $n) = @_;
return split(/\s+/, $str, $n);
}
sub print_acc {
my ($dstamp, $amount, $alt, $desc) = @_;
$tacc += $amount;
printf "%s %s %10.2f %10.2f %-6s %s\n", $pracc, $dstamp, $amount, $tacc, $alt, $desc;
}
sub ntran {
my ($dstamp, $dr, $cr, $amount, $desc) = @_ ;
return if ($dstamp lt $start_date);
#print "ntran amount: $dr $cr $amount\n";
$gaaps{$dr} += $amount;
if($dr eq $pracc) { print_acc $dstamp, $amount, $cr, $desc }
$gaaps{$cr} -= $amount;
if($cr eq $pracc) { print_acc $dstamp, -$amount, $dr, $desc }
}
sub etran {
my ($dstamp, $acc, $ticker, $qty, $amount, $desc) = split1(@_, 6);
my $samount = ($qty <=> 0) * $amount;
ntran $dstamp, "flow", $acc, $samount, $desc;
$qtys{"$acc:$ticker"} += $qty;
my $dstamp1 = Time::Piece->strptime($dstamp, "%Y-%m-%d")->strftime("%d/%m/%Y");
my $aqty = abs($qty);
if($acc ne "isa") {
printf $fcgt "%s\t%s\t%s\t%d\t%f\t0.00\t0.00\n", ($qty>0 ? "B" : "S"), $dstamp1, $ticker, $aqty, $amount/$aqty;
}
# stockopedia
my $ticker1 = $ticker;
$ticker1 =~ s/^(.+)../LON:$1/;
my $way = $qty >0 ? "Buy" : "Sell";
my $uprice = $amount/$aqty * 100.0;
#my $commision = $amount - ($uprice*$aqty/100.0);
printf $fstocko "%s,%s,10:10:10,%s,%d,1,%f,GBX,0,,%.2f\n", $ticker1, $dstamp1, $way, $aqty, $uprice, $samount;
my $dep = $qty >0 ? "Deposit" : "Withdrawal";
printf $fstocko ",%s,10:10:10,%s,,,,,,,%.2f\n", $dstamp1, $dep, $samount;
}
sub gaap {
my ($meta, $base, $comp, $desc) = split1(@_, 4);
$gaaps{$meta} += $gaaps{$base};
my $acc = $base eq "=" ? $meta : $base;
printf $fgaap "%-7s %10.2f %10.2f %s\n", $acc, $gaaps{$acc}, $comp, $desc;
if($base eq "=") { print $fgaap "\n" }
}
sub scan {
my $filename = shift(@_);
open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
while (my $row = <$fh>) {
chomp $row;
my ($cmd, $args) = split(/\s+/ , $row, 2);
if ($cmd eq "etran-3") { etran $args }
elsif ($cmd eq "gaap") { gaap $args }
elsif ($cmd eq "ntran") { ntran split1($args, 5) }
elsif ($cmd eq "start") { $start_date = $args }
}
}
sub print_qtys {
$fqty = fopen("qty.txt");
foreach my $key (sort keys %qtys) {
printf $fqty "%-10s %5d\n", $key, $qtys{$key};
}
close $fqty;
}
sub main1 {
$pracc = $ARGV[0]; # the account I want to print
scan("ltbhv1.txt");
scan("accts2019v1.txt");
scan("gaap.txt");
print_qtys;
print_gaaps;
close $fcgt;
}
main1;
#!/usr/bin/env perl6
#use experimental :macros;
#use Time::Piece;
sub splitn($str, $n) { split(/\s+/, $str, $n) }
my @inputs = q:x/cat ltbhv1.txt accts2019v1.txt/ .split("\n") ;
my @etrans;
my @posts = gather {
for @inputs {
my ($cmd, $rest) = splitn($_, 2);
given $cmd {
when "etran-3" {
my @args = splitn($rest, 6);
my ($dstamp, $acc, $ticker, $eqty, $eamount, $desc) = @args;
@etrans.push(@args);
my $amount = sign($eqty) * $eamount;
take $dstamp, "flow" , $amount, $desc;
take $dstamp, $acc, -$amount, $desc;
}
when "ntran" {
my @args = splitn($rest, 5);
take @args[0], @args[1], @args[3], @args[4];
take @args[0], @args[2], -@args[3], @args[4];
}
}
}
}.grep({ $_ ge "2019-04-06"}).sort;
if my $pracc = @*ARGS[0] {
my $cum = 0.0;
for @posts.grep({ $_[1] eq $pracc}) {
$cum += $_[2];
sprintf("%s %4s %10.2f %10.2f %s", $_[0], $_[1], $_[2], $cum, $_[3]).put };
}
# produce the gaap file
my %gaaps is default(0.00);
for @posts {%gaaps{$_[1]} += $_[2]; }
my $fh-gaap = open "out/gaap1.txt", :w;
for 'gaap.txt'.IO.lines {
my ($cmd, $meta, $base, $comp, $desc) = splitn($_, 5);
next if $cmd ne "gaap";
%gaaps{$meta} += %gaaps{$base};
my $acc = $base eq "=" ?? $meta !! $base;
$fh-gaap.printf("%-7s %10.2f %10.2f %s\n", $acc, %gaaps{$acc}, $comp, $desc);
$fh-gaap.printf("\n") if $base eq "=";
};
$fh-gaap.close;
#produce the qty file
my %qtys;
for @etrans {
my ($dstamp, $acc, $ticker, $eqty, $eamount, $desc) = $_;
%qtys{"$acc:$ticker"} += $eqty;
}
my $qty-text = %qtys.map({sprintf("%-10s %5d", .key, .value) }).sort.join("\n");
spurt('out/qty1.txt', $qty-text);
# produce cgt and stocko
my $fcgt = open "out/cgt1.txt", :w;
my $fstocko= open "out/stocko1.csv", :w;
$fstocko.printf( "TICKER,DATE,TIME,TYPE,SHARES,FX,PRICE,CURRENCY,COMMISSION,TAX,TOTAL\n");
for @etrans {
my ($dstamp, $acc, $ticker, $eqty, $eamount, $desc) = $_;
#my $y, $m, $d = split($dstamp, '-');
my $dstamp1 = split('-', $dstamp).reverse.join('/');
my $aqty = abs($eqty);
#cgt
my $cfmt = "%s\t%s\t%s\t%d\t%f\t0.00\t0.00\n";
if $acc ne "isa" {
$fcgt.printf($cfmt, ($eqty>0 ?? "B" !! "S"), $dstamp1, $ticker, $aqty, $eamount/$aqty);
}
# stocko
my $ticker1 = "LON:" ~ $ticker.chop.chop;
my $way = $eqty >0 ?? "Buy" !! "Sell";
my $uprice = $eamount/$aqty * 100.0;
my $samount = sign($eqty) * $eamount;
$fstocko. printf("%s,%s,10:10:10,%s,%d,1,%f,GBX,0,,%.2f\n", $ticker1, $dstamp1, $way, $aqty, $uprice, $samount);
my $dep = $eqty >0 ?? "Deposit" !! "Withdrawal";
$fstocko. printf( ",%s,10:10:10,%s,,,,,,,%.2f\n", $dstamp1, $dep, $samount);
}
$fcgt.close;
$fstocko.close;
@blippy
Copy link
Author

blippy commented May 5, 2019

Refer to my blog for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment