/
finpension.py
322 lines (277 loc) · 13.1 KB
/
finpension.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
This is a beancount importer for Finpension.
Setup:
1) have a running beancount system
2) run 'bean-extract config.py path/to/finpension/transaction_report.csv -f mainLedgerFile.bean
"""
import pandas as pd
from datetime import datetime, timedelta
import xml.etree.ElementTree as ET
import warnings
import pickle
import re
import numpy as np
import sys
from loguru import logger
import yaml
from os import path
from beancount.query import query
from beancount.parser import options
from beancount.ingest import importer
from beancount.core import data, amount
from beancount.core.number import D
from beancount.core.number import Decimal
from beancount.core import position
from beancount.core.number import MISSING
# some constants set by FinPension in the csv export header
FP_currency = 'Asset Currency'
FP_proceeds = 'Cash Flow'
FP_asseet_price = 'Asset Price in CHF'
class FinPensionImporter(importer.ImporterProtocol):
"""
Beancount Importer for Finpension
"""
def __init__(self,
deposit_account = None,
root_account = None,
isin_lookup = None, # for example Assets:Invest:IB
div_suffix='Div', # suffix for dividend Account , like Assets:Invest:IB:VT:Div
interest_suffix='Interest',
fees_suffix='Fees',
pnl_suffix='PnL',
file_encoding="utf-8-sig",
sep=";",
# a regex pattern that allows to distinguish between pillar 2&3 and individual portfolios
regex=r"finpension_(S[2,3][a]?)_(Portfolio\d)",
):
self.root_account = root_account # root account from which others can be derived
self.deposit_account = deposit_account
self.div_suffix = div_suffix
self.interest_suffix = interest_suffix
self.fees_suffix = fees_suffix
self.pnl_suffix = pnl_suffix
self.isin_lookup = isin_lookup
self.file_encoding = file_encoding
self.flag = '*'
self.regex = regex
self.sep = sep
def identify(self, file):
# intended file format is *finpension_s2_p1* for säule(pillar) 2 portfolio 1
result = bool(re.search(self.regex, file.name, re.IGNORECASE))
logger.info(
f"identify assertion for finpension importer and file '{file.name}': {result}")
return result
def getLiquidityAccount(self, currency):
return ':'.join([self.main_account, currency])
def getDivIncomeAcconut(self, currency, symbol):
return ':'.join([self.main_account.replace('Assets', 'Income'), symbol, self.div_suffix])
def getInterestIncomeAcconut(self, currency):
return ':'.join([self.main_account.replace('Assets', 'Income'), self.interest_suffix, currency])
def getAssetAccount(self, symbol):
return ':'.join([self.main_account, symbol])
def getFeesAccount(self, currency):
return ':'.join([self.main_account.replace('Assets', 'Expenses'), self.fees_suffix, currency])
def file_account(self, file):
self.fix_accounts(file)
return self.main_account or self.root_account
def fix_accounts(self, file):
try:
pillar, portfolio = re.search(
self.regex, file.name, re.IGNORECASE).groups()
except AttributeError as e:
logger.error(
f"could not extract pillar and/or portfolio from filename {file.name} with regex pattern {self.regex}.")
raise AttributeError(e)
new_account = re.sub(r"S[2,3]a?", pillar, self.root_account)
self.main_account = re.sub(r"Portfolio\d", portfolio, new_account)
def extract(self, file_, existing_entries=None):
# the actual processing of the csv export
# fix Account names with regard to pillar 2/3 and different portfolios.
self.fix_accounts(file_)
df = pd.read_csv(file_.name,
sep=self.sep,
)
# convert specific columns to Decimal with specific precisions
to_decimal_dict = {"Number of Shares": 3,
"Asset Price in CHF": 2,
"Cash Flow": 2,
"Balance": 2}
for col, digits in to_decimal_dict.items():
df[col] = df[col].apply(lambda x: Decimal(x).__round__(digits))
df['Date'] = pd.to_datetime(df['Date']).apply(datetime.date)
# disect the complete report in similar transactions
# abit messy since Finpension uses different tags in pillar 2/3a
trades = df[df.Category.isin(["Portfolio Transaction",'Buy','Sell'])]
deposits = df[df.Category.isin(["Transfer vested benefits",'Deposit'])]
fees = df[df.Category.isin(["Implementation fees",'Flat-rate administrative fee','Flat-rate administration fee'])]
interests = df[df.Category == "Interests"]
dividends = df[df.Category.isin(["Dividend and Interest Distributions",'Dividend'])]
return_txn = (self.Trades(trades)
+ self.Deposits(deposits) # omitted for only occuring very rarely
+ self.Fees(fees)
+ self.Interest(interests)
+ self.Dividends(dividends)
+ self.Balances(df)
)
return return_txn
def Trades(self, trades):
bean_transactions = []
for idx, row in trades.iterrows():
currency = row[FP_currency]
isin = row['ISIN']
symbol = self.isin_lookup.get(isin)
asset = row['Asset Name']
if symbol is None:
logger.error(
f"Could not fetch isin {row['symbol']} from supplied ISINs {list(self.isin_lookup.keys())}")
continue
proceeds = amount.Amount(row[FP_proceeds], currency)
quantity = amount.Amount(row['Number of Shares'], symbol)
price = amount.Amount(row[FP_asseet_price], "CHF")
postings = [
data.Posting(self.getAssetAccount(symbol),
quantity, None, price, None, None),
data.Posting(self.getLiquidityAccount(currency),
proceeds, None, None, None, None),
]
if quantity.number > 0:
buy_sell = "BUY"
else:
buy_sell = "SELL"
bean_transactions.append(
data.Transaction(data.new_metadata('Buy', 0),
row['Date'],
self.flag,
isin, # payee
' '.join(
[buy_sell, quantity.to_string(), '@', price.to_string()+";", asset]),
data.EMPTY_SET,
data.EMPTY_SET,
postings
))
return bean_transactions
def Fees(self, fees):
bean_transactions = []
for idx, row in fees.iterrows():
currency = row[FP_currency]
amount_ = amount.Amount(row[FP_proceeds], currency)
# make the postings, two for fees
postings = [data.Posting(self.getFeesAccount(currency),
-amount_, None, None, None, None),
data.Posting(self.getLiquidityAccount(currency),
amount_, None, None, None, None)]
meta = data.new_metadata(__file__, 0, {}) # actually no metadata
bean_transactions.append(
data.Transaction(meta,
row['Date'],
self.flag,
'FinPension', # payee
"Fees",
data.EMPTY_SET,
data.EMPTY_SET,
postings))
return bean_transactions
def Dividends(self, dividends):
# this function crates Dividend transactions from IBKR data
# make dividend & WHT transactions
bean_transactions = []
for idx, row in dividends.iterrows():
currency = row[FP_currency]
isin = row['ISIN']
symbol = self.isin_lookup.get(isin)
if symbol is None:
logger.error(
f"Could not fetch isin {row['symbol']} from supplied ISINs {list(self.isin_lookup.keys())}")
continue
amount_div = amount.Amount(row[FP_proceeds], currency)
postings = [data.Posting(self.getDivIncomeAcconut(currency, symbol),
-amount_div, None, None, None, None),
data.Posting(self.getLiquidityAccount(currency),
amount_div, None, None, None, None)
]
metadict = {'isin': isin}
per_share_number = row[FP_asseet_price]
if not per_share_number.is_nan():
pershare = amount.Amount(row[FP_asseet_price], currency)
metadict.update({'per_share': pershare})
meta = data.new_metadata(
'dividend', 0, metadict)
bean_transactions.append(
data.Transaction(meta, # could add div per share, ISIN,....
row['Date'],
self.flag,
isin, # payee
f"Dividend {symbol}; {row['Asset Name']}",
data.EMPTY_SET,
data.EMPTY_SET,
postings
))
return bean_transactions
def Interest(self, int_):
# calculates interest payments from IBKR data
bean_transactions = []
for idx, row in int_.iterrows():
currency = row[FP_currency]
amount_ = amount.Amount(row[FP_proceeds], currency)
# make the postings, two for interest payments
# received and paid interests are booked on the same account
postings = [data.Posting(self.getInterestIncomeAcconut(currency),
-amount_, None, None, None, None),
data.Posting(self.getLiquidityAccount(currency),
amount_, None, None, None, None)
]
meta = data.new_metadata('Interest', 0)
bean_transactions.append(
data.Transaction(meta, # could add div per share, ISIN,....
row['Date'],
self.flag,
'FinPension', # payee
"Interest",
data.EMPTY_SET,
data.EMPTY_SET,
postings
))
return bean_transactions
def Balances(self, df):
# generate Balance statements for every latest transaction
# (there may be multiple, no idea how to pick the right one)
# simply make a balance for all values, the correct one should be one of them
bean_transactions = []
df = df[df['Date'] == df['Date'].max()]
for idx, row in df.iterrows():
currency = row[FP_currency]
amount_ = amount.Amount(row['Balance'], currency)
meta = data.new_metadata('balance', 0)
bean_transactions.append(data.Balance(
meta,
row['Date'] + timedelta(days=1), # see tariochtools EC imp.
self.getLiquidityAccount(currency),
amount_,
None,
None))
return bean_transactions
def Deposits(self, dep):
bean_transactions = []
if len(self.deposit_account) == 0: # control this from the config file
return []
for idx, row in dep.iterrows():
currency = row[FP_currency]
amount_ = amount.Amount(row[FP_proceeds], currency)
# make the postings. two for deposits
postings = [data.Posting(self.deposit_account,
-amount_, None, None, None, None),
data.Posting(self.getLiquidityAccount(currency),
amount_, None, None, None, None)
]
meta = data.new_metadata('deposit/withdrawel', 0)
bean_transactions.append(
data.Transaction(meta, # could add div per share, ISIN,....
row['Date'],
self.flag,
'self', # payee
"deposit / withdrawal",
data.EMPTY_SET,
data.EMPTY_SET,
postings
))
return bean_transactions