-
Notifications
You must be signed in to change notification settings - Fork 1
/
lendy.py
executable file
·301 lines (227 loc) · 10.1 KB
/
lendy.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
P2P platform Lendy.co.uk parser for Lendy_Statement_YYYYMMDD-YYYYMMDD.csv file and set of statistics functions
"""
import pandas as pd
import csv
from datetime import datetime
import argparse
import os
import utils
DATA_FOLDER = 'data/'
LENDY_CSV_FILE = 'lendy.csv'
def convertToStandardCSV(sourceFile):
try:
destinationFile = os.path.dirname(os.path.realpath(__file__)) + '/' + DATA_FOLDER + LENDY_CSV_FILE
print('Trying to load', sourceFile)
df = pd.read_csv(sourceFile)
print('File', sourceFile, 'loaded.')
# quick content check
if list(df) == [
'Txn Date',
'Transaction type',
'Loan part ID',
'Loan part value',
'Loan part detail',
'Loan ID',
'Start date',
'End date',
'Txn Amount',
'Balance']:
print('File content format looks good.')
else:
raise Exception('File content format looks bad. Sorry')
# reverse row order
df = df[::-1]
df.to_csv(destinationFile, header=[str(x) for x in range(len(df.columns))], encoding='utf-8', index=False)
print('File', sourceFile, 'converted to', destinationFile)
print('First 2 lines of the content:')
print(df.head(2).to_string(index=False, header=False))
print('Last 2 lines of the content:')
print(df.tail(2).to_string(index=False, header=False))
except Exception as e:
print('Conversion failed.', e)
def getDataFromCSVfile(filepath):
with open(filepath) as csvFile:
reader = csv.DictReader(csvFile, delimiter=',')
for row in reader:
if not row['0']:
continue
if not row['5']:
row['5'] = 0.0
yield row
def getRowData(row):
fee = None
principalRepaid = None
interestReceived = None
chargesReceived = None
cashFlowChange = None
try:
if row['1'] == 'Deposit':
cashFlowChange = float(row['8'])
# TODO withdrawal?
# if row['1'] == 'Deposit':
# cashFlowChange = float(row['8'])
if row['1'] == 'Interest':
interestReceived = float(row['8'])
if row['1'] == 'Bonus':
interestReceived = float(row['8'])
if row['1'] == 'Capital Repayment' or row['1'] == 'Capital repayment':
principalRepaid = float(row['8'])
return {'rawDate': row['0'], 'fee': fee, 'principalRepaid': principalRepaid,
'interestReceived': interestReceived, 'chargesReceived': chargesReceived, 'cashFlowChange': cashFlowChange}
except Exception as e:
print(e, row)
def getCashInGame(cashInGame, rowData):
if rowData['cashFlowChange']:
cashInGame = cashInGame + rowData['cashFlowChange']
if rowData['interestReceived']:
cashInGame = cashInGame + rowData['interestReceived']
if rowData['chargesReceived']:
cashInGame = cashInGame + rowData['chargesReceived']
if rowData['fee']:
cashInGame = cashInGame + rowData['fee']
return cashInGame
def getFees():
fees = 0.0
for row in getDataFromCSVfile(DATA_FOLDER + LENDY_CSV_FILE):
rowData = getRowData(row)
if rowData['fee']:
yield (rowData['rawDate'], format(rowData['fee'], '.2f'))
fees = fees + rowData['fee']
yield ('Total fee', format(fees, '.2f'))
def getTotals():
principalRepaid = 0.0
interestsReceived = 0.0
charges = 0.0
fees = 0.0
cashInGame = 0.0
for row in getDataFromCSVfile(DATA_FOLDER + LENDY_CSV_FILE):
rowData = getRowData(row)
cashInGame = getCashInGame(cashInGame, rowData)
if rowData['principalRepaid']:
principalRepaid = principalRepaid + rowData['principalRepaid']
if rowData['interestReceived']:
interestsReceived = interestsReceived + rowData['interestReceived']
if rowData['chargesReceived']:
charges = charges + rowData['chargesReceived']
if rowData['fee']:
fees = fees + rowData['fee']
yield(format(cashInGame, '.2f'), 'Cash in game')
yield(format(interestsReceived, '.2f'), 'Total interests received')
yield(format(charges, '.2f'), 'Total charges received')
yield(format(fees, '.2f'), 'Total fees paid')
yield(format(principalRepaid, '.2f'), 'Total principal repaid')
def getPreviousMonth():
cashInGame = 0.0
feePaid = 0.0
principalRepaid = 0.0
interestsReceived = 0.0
cashInGameForThisMonth = None
if datetime.today().month - 1 > 0:
previousMonth = datetime.today().month - 1, datetime.today().year
else:
previousMonth = 12, datetime.today().year - 1
yield (str(previousMonth[0]) + "." + str(previousMonth[1]), 'Considered month')
for row in getDataFromCSVfile(DATA_FOLDER + LENDY_CSV_FILE):
rowData = getRowData(row)
date = datetime.strptime(rowData['rawDate'], '%d/%m/%Y')
if previousMonth == (date.month, date.year):
if rowData['principalRepaid']:
principalRepaid = principalRepaid + rowData['principalRepaid']
if rowData['interestReceived']:
interestsReceived = interestsReceived + rowData['interestReceived']
if rowData['chargesReceived']:
interestsReceived = interestsReceived + rowData['chargesReceived']
if not cashInGameForThisMonth:
cashInGameForThisMonth = cashInGame
cashInGame = getCashInGame(cashInGame, rowData)
if rowData['fee']:
feePaid = rowData['fee']
yield (format(cashInGameForThisMonth, '.2f'), 'Cash in game for this month')
yield (format(principalRepaid, '.2f'), 'Total principal repaid')
yield (format(interestsReceived, '.2f'), 'Total interests received')
yield (format(feePaid, '.2f'), 'Fee paid ')
def getTotalByMonth():
principalRepaid = 0.0
interestsReceived = 0.0
cashInGame = 0.0
previousMonthPrincipalRepaid = 0
previousMonthInterestsReceived = 0
previousMonthCashInGame = 0
currentMonthDate = datetime.strptime('1.1.2000', '%d.%m.%Y')
currentMonthCashInGame = 0
newMonth = False
roi = 0.0
yield('Month', 'CashInGame', 'Inter.', 'Fee', 'ROI', 'Princip.')
for row in getDataFromCSVfile(DATA_FOLDER + LENDY_CSV_FILE):
rowData = getRowData(row)
rowDate = datetime.strptime(rowData['rawDate'], '%d/%m/%Y')
if rowDate.month != currentMonthDate.month:
previousMonthPrincipalRepaid = principalRepaid
principalRepaid = 0
previousMonthInterestsReceived = interestsReceived
interestsReceived = 0
previousMonthCashInGame = currentMonthCashInGame
currentMonthCashInGame = cashInGame
currentMonthDate = rowDate
newMonth = True
cashInGame = getCashInGame(cashInGame, rowData)
if rowData['principalRepaid']:
principalRepaid = principalRepaid + rowData['principalRepaid']
if rowData['interestReceived']:
interestsReceived = interestsReceived + rowData['interestReceived']
if rowData['chargesReceived']:
interestsReceived = interestsReceived + rowData['chargesReceived']
if newMonth:
previousMonthFee = rowData['fee'] or 0.0
newMonth = False
# fee is negative number
if previousMonthCashInGame > 0:
roi = (previousMonthInterestsReceived + previousMonthFee) / previousMonthCashInGame
# fee is the last transaction of the previous month
if currentMonthDate.month - 1 > 0:
previousMonthYear = currentMonthDate.month - 1, currentMonthDate.year
else:
previousMonthYear = 12, currentMonthDate.year - 1
yield(str(previousMonthYear[0]) + "." + str(previousMonthYear[1]), format(previousMonthCashInGame, '.2f'),
format(previousMonthInterestsReceived, '.2f'), format(previousMonthFee, '.2f'), format(roi, '.4f'),
format(previousMonthPrincipalRepaid, '.2f'))
# ongoing month
# yield(currentMonthDate.strftime('%-m.%Y'), round(cashInGame, 2), round(interestsReceived, 2), '', '', round(principalRepaid, 2))
def getCashFlow():
for row in getDataFromCSVfile(DATA_FOLDER + LENDY_CSV_FILE):
rowData = getRowData(row)
if rowData['cashFlowChange']:
yield (rowData['rawDate'], format(rowData['cashFlowChange'], '.2f'))
def main():
parser = argparse.ArgumentParser(description='This script produces statistics based on Lendy\'s transactions file from My Account - Transactions tab.')
parser.add_argument('-c', '--convert', dest='convert', action='store', default=False, metavar=('FILEPATH'),
help='Converting FILEPATH to ' + DATA_FOLDER + LENDY_CSV_FILE)
parser.add_argument('-f', '--fees', dest='getFees', action='store_true', default=False, help='Paid fees to Lendy')
parser.add_argument('-t', '--total', dest='getTotals', action='store_true', default=False, help='Account statement')
parser.add_argument('-tbm', '--totalbymonth', dest='getTotalByMonth', action='store_true', default=False, help='Account statement per month')
parser.add_argument('-p', '--previousmonth', dest='getPreviousMonth', action='store_true', default=False, help='Account statement for last month')
parser.add_argument('-cf', '--cashflow', dest='getCashFlow', action='store_true', default=False, help='Cashflow actions within the account')
args = parser.parse_args()
resultValues = []
if args.convert:
convertToStandardCSV(args.convert)
elif args.getFees:
resultValues = getFees()
elif args.getTotals:
resultValues = getTotals()
elif args.getTotalByMonth:
resultValues = getTotalByMonth()
elif args.getPreviousMonth:
resultValues = getPreviousMonth()
elif args.getCashFlow:
resultValues = getCashFlow()
else:
parser.print_help()
for list in resultValues:
print(utils.getTabbedStringFromValueList(list))
return
if __name__ == '__main__':
main()