-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkraken2transfers.py
executable file
·79 lines (63 loc) · 2.63 KB
/
kraken2transfers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
#
# Read kraken ledger csv file and convert to 'raw' format read by abledger.py
#
# Filenames are hard-coded, so only directory can be given
#
# File format should be csv with headings on first line:
# "txid","refid","time","type","aclass","asset","amount","fee","balance"
import os
import argparse
import time
import csv
import re
os.environ['TZ'] = 'UTC' # workaround for no inverse of time.gmtime(t)
TOLERANCE = 1e-6
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--directory', help='dir for csv files to read', default='/home/aeron/Documents/Admin/Trading/Logs/Kraken/')
args = parser.parse_args()
def extractCSVs(_s, _n, _i):
# TODO: pass error up instead of passing line number down
line = _s.rstrip().lstrip()
if line == '':
return []
for e in csv.reader([line], quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True):
vs = e
if len(vs) != _n:
sys.exit('ERROR: Incorrect number of entries on line %d (expecting %d, got %d)' % (_i, _n, len(vs)))
sys.exit('parse error')
return vs
currencyTranslation = {
"ZEUR": "EUR",
"ZUSD": "USD",
"ZGBP": "GBP",
"XETH": "ETH",
"XXBT": "BTC",
"XETC": "ETC",
"XXLM": "XLM",
}
output = open(args.directory + 'kraken.transfers.csv', 'w')
output.write("Date, Base Currency, Value, Trade Currency, Amount, Transfer Info\n")
with open(args.directory + 'ledgers.csv') as f:
ln = 0
for line in f:
ln += 1
if ln == 1:
if line.rstrip() != '"txid","refid","time","type","aclass","asset","amount","fee","balance"':
exit('ERROR: first line of "%s" not headings as expected' % inputfile)
else:
entries = extractCSVs(line, 9, ln)
if entries:
(txid, refid, timestr, type_, aclass, currency, amount, fee, balance) = entries
if currency != "KFEE" and (type_ == 'deposit' or type_ == 'withdrawal' or type_ == 'transfer'):
date = time.strftime('%Y-%m-%d-%H-00', time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))
currency = currencyTranslation[currency]
amount = float(amount)
if type_ == 'deposit': output.write("%s, %s, %f, %s, %f, ->kraken\n" % (date, currency, -amount, currency, amount))
elif type_ == 'withdrawal': output.write("%s, %s, %f, %s, %f, ->kraken\n" % (date, currency, -amount, currency, amount))
elif type_ == 'transfer': output.write("%s, GBP, 0, %s, %s,\n" % (date, currency, amount))
else: exit("ERROR: unexpected ledger entry type '%s' slipped through!" % type_)
fee = abs(float(fee))
if fee > 0:
output.write("%s, GBP, 0, %s, %f, \n" % (date, currency, -fee))
output.close()