git ssb

0+

cel / ledger-scripts



Tree: f1521af2d73e96acaa4e96dfc60dbfc50373845a

Files: f1521af2d73e96acaa4e96dfc60dbfc50373845a / fidelity2ledger

3891 bytesRaw
1#!/usr/bin/env perl
2
3use strict;
4
5$| = 1;
6
7my $brokerage_account = 'Assets:Broker:Fidelity';
8my $commission_account = 'Expenses:Broker:Fidelity';
9my $fee_account = 'Expenses:Broker:Fidelity:Fee';
10my $dividend_account = 'Income:Dividends';
11my $interest_account = 'Income:Interest';
12my $long_term_cap_gains_account = 'Income:Dividend:Long-term Capital Gains';
13my $deposit_account = 'Income';
14my $debit_account = 'Expenses';
15
16my %symbols = (
17 '06740C261' => 'VIX'
18);
19
20sub process_field {
21 my $value = $_;
22 if (substr($value, 0, 1) eq ' ') {
23 substr($value, 1);
24 } else {
25 +$value;
26 }
27}
28
29while (<>) {
30 my @fields = split(',', $_);
31 my ($run_date, $action, $symbol, $security_description, $security_type, $quantity,
32 $price, $commission, $fees, $accrued_interest, $amount, $settlement_date) =
33 map { process_field($_); } @fields;
34
35 next if ($run_date eq 'Run Date');
36
37 my ($month, $day, $year) = $run_date =~ /(.*)\/(.*)\/(.*)/;
38 my $date = $year . '/' . $month . '/' . $day;
39
40 $symbol = $symbols{$symbol} // $symbol;
41
42 my $from_account;
43 my $check_num;
44 my $amount_symbol = '$' . $amount;
45 my $description = $security_description;
46
47 if ($action eq 'YOU BOUGHT' or $action eq 'REINVESTMENT') {
48 # Fix price
49 #my $price_calculated = ($amount + $commission + $fees) / -$quantity;
50 #my $commission_calculated = -($price * $quantity + $amount + $fees) + 0.00000001;
51
52 if ($security_description eq 'CASH') {
53 next;
54 }
55
56 $amount_symbol = $quantity . ' ' . $symbol;
57 $from_account = $brokerage_account;
58 $description = 'Buy: ' . $security_description;
59 }
60
61 elsif ($action =~ /YOU SOLD/) {
62 $amount_symbol = $quantity . ' ' . $symbol;
63 $from_account = $brokerage_account;
64 $description = 'Sell: ' . $security_description;
65 }
66
67 elsif ($action eq 'DIVIDEND RECEIVED') {
68 $from_account = $dividend_account;
69 $amount *= -1;
70 $description = 'Dividend: ' . $security_description;
71 }
72
73 elsif ($action eq 'INTEREST EARNED') {
74 $from_account = $interest_account;
75 $amount *= -1;
76 $description = 'Interest';
77 }
78
79 elsif ($action eq 'LONG-TERM CAP GAIN') {
80 $from_account = $long_term_cap_gains_account;
81 $amount *= -1;
82 $description = 'Long-term capital gains: ' . $security_description;
83 }
84
85 elsif ($action eq 'Electronic Funds Transfer Paid') {
86 $from_account = $debit_account;
87 $amount *= -1;
88 $description = $action;
89 }
90
91 elsif (my ($sender) = $action =~ /DIRECT\s*DEPOSIT (.*)/) {
92 $from_account = $deposit_account;
93 $amount *= -1;
94 $description = $sender;
95 }
96
97 elsif ($action =~ /^CHECK RECEIVED/) {
98 $from_account = $deposit_account;
99 $amount *= -1;
100 $description = 'Check received';
101 }
102
103 elsif (my ($receiver) = $action =~ /^(?:DIRECT\s*DEBIT|DEBIT CARD PURCHASE)\s*(.*)/) {
104 $from_account = $debit_account;
105 $amount *= -1;
106 $description = $receiver;
107 }
108
109 elsif (my ($fee) = $action =~ /FEE CHARGED\s*(.*)/) {
110 $from_account = $fee_account;
111 $amount *= -1;
112 $description = $fee;
113 }
114
115 elsif (($check_num) = $action =~ /Check Paid # ?([0-9]*)/) {
116 $from_account = $debit_account;
117 $amount *= -1;
118 $description = 'Check paid';
119 }
120
121 else {
122 die("Unknown transaction type: $action");
123 }
124
125 my $check = $check_num ? '(#' . $check_num . ') ' : '';
126
127 printf "%s %s%s\n", $date, $check, $description;
128 printf "\t%-36s %s\n", $brokerage_account, $amount_symbol;
129 printf "\t%-36s \$%.02f\n", $from_account, $amount;
130 printf "\t%-36s \$%.02f\n", $commission_account, $commission if ($commission);
131 printf "\t%-36s \$%.02f\n", $fee_account, $fees if ($fees);
132 print "\n";
133
134}
135

Built with git-ssb-web