Files: f1521af2d73e96acaa4e96dfc60dbfc50373845a / havelock2ledger
2762 bytesRaw
1 | #!/usr/bin/awk -f |
2 | # |
3 | # havelock2ledger |
4 | # Render transactions from Havelock CSV into ledger format |
5 | # |
6 | # Usage: |
7 | # ./havelock2ledger transactions.csv |
8 | # or, to get transactions in the date ascending order: |
9 | # tac transactions.csv | ./havelock2ledger |
10 | |
11 | BEGIN { |
12 | FS=","; |
13 | assets_account="Assets:Broker:Havelock"; |
14 | fee_account="Expenses:Broker:Havelock"; |
15 | } |
16 | |
17 | # Skip header |
18 | /^Date/ { next; } |
19 | |
20 | # Read transaction |
21 | { |
22 | time = $1; |
23 | type = $2; |
24 | quantity = $3; |
25 | price = $4; |
26 | symbol = $5; |
27 | amount = $6; |
28 | balance = $7; |
29 | |
30 | date = substr(time, 1, 10); |
31 | gsub("-", "/", date); |
32 | |
33 | # Some types don't include quantity and price |
34 | if (! balance) { |
35 | symbol = $3; |
36 | amount = $4; |
37 | balance = $5; |
38 | } |
39 | |
40 | asset = ""; |
41 | account = assets_account; |
42 | if (type == "dividend") { |
43 | description = symbol " Dividend"; |
44 | account = "Income:Dividends"; |
45 | |
46 | } else if (type == "buyipo") { |
47 | description = "Buy " symbol " (IPO)"; |
48 | asset = quantity " " symbol " @ " price " BTC"; |
49 | amount = "-" amount; |
50 | account = ""; |
51 | |
52 | } else if (type == "escrow") { |
53 | description = "Escrow"; |
54 | account = assets_account; |
55 | |
56 | } else if (type == "deposit") { |
57 | description = "Deposit"; |
58 | account = "Income"; |
59 | |
60 | } else if (type == "withdraw") { |
61 | description = "Withdraw"; |
62 | account = "Expenses"; |
63 | |
64 | } else if (type == "buy") { |
65 | description = "Buy " symbol; |
66 | asset = quantity " " symbol " @ " price " BTC"; |
67 | amount = "-" amount; |
68 | account = ""; |
69 | |
70 | } else if (type == "sell") { |
71 | description = "Sell " symbol; |
72 | asset = "-" quantity " " symbol " @ " price " BTC"; |
73 | account = ""; |
74 | |
75 | # get fee, if the csv is reversed and the fee comes after the sell |
76 | if (getline > 0) { |
77 | fee_amount = $4; |
78 | } |
79 | |
80 | } else if (type == "buyback") { |
81 | description = "Buyback " symbol; |
82 | asset = "-" quantity " " symbol " @ " price " BTC"; |
83 | |
84 | } else if (type == "transferin") { |
85 | description = "Transfer " symbol; |
86 | asset = quantity " " symbol; |
87 | account = "Assets"; |
88 | amount = ""; |
89 | |
90 | } else if (type == "fee") { |
91 | fee_amount = amount; |
92 | next; |
93 | |
94 | } else { |
95 | description = "Unknown"; |
96 | } |
97 | |
98 | if (fee_amount) { |
99 | amount = amount - fee_amount; |
100 | } |
101 | |
102 | printf "%s %s\n", date, description; |
103 | if (asset) { |
104 | printf " %-32s %20s\n", assets_account, asset; |
105 | } |
106 | if (amount) { |
107 | printf " %-32s %16s BTC\n", assets_account, amount; |
108 | } |
109 | if (fee_amount) { |
110 | printf " %-32s %16s BTC\n", fee_account, fee_amount; |
111 | fee_amount = ""; |
112 | } |
113 | if (account) { |
114 | printf " %s\n", account; |
115 | } |
116 | print ""; |
117 | } |
118 |
Built with git-ssb-web