From c0d250c7f85c4de46220b8c41c21593dbd0ed462 Mon Sep 17 00:00:00 2001 From: Stefano Mihai Canta Date: Mon, 27 Jan 2020 13:04:52 -0600 Subject: [PATCH 1/3] Fix ofx assert for Fidelity, handle ofx with negative unit price, fix internal account transfers --- beancount_import/source/ofx.py | 40 ++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/beancount_import/source/ofx.py b/beancount_import/source/ofx.py index 22ce979b..5809e555 100644 --- a/beancount_import/source/ofx.py +++ b/beancount_import/source/ofx.py @@ -842,6 +842,21 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str: if stripped_checknum: posting_meta[CHECK_KEY] = D(stripped_checknum) + fee_total = ZERO + for fee_key in ['fees', 'commission']: + amount = getattr(raw, fee_key, None) + if amount is not None and amount != ZERO: + fee_total += amount + entry.postings.append( + Posting( + account=get_account_by_key(account, + fee_key + '_account'), + units=Amount(number=amount, currency=self.currency), + cost=None, + price=None, + flag=None, + meta=None)) + cash_transfer_transaction_amount = None if raw.trantype == 'INCOME' or raw.trantype == 'INVBANKTRAN' or raw.trantype == 'STMTTRN': # Cash-only transaction @@ -929,9 +944,12 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str: date=None, label='FIXME', merge=False) + elif raw.trantype == 'TRANSFER' and units == ZERO: + # Internal transfer, i.e. from after-tax to roth + continue else: cost_spec = CostSpec( - number_per=unitprice, + number_per=(abs(total)-abs(fee_total))/units, number_total=None, currency=self.currency, date=None, @@ -1022,26 +1040,6 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str: flag=None, meta=external_meta)) - fee_total = ZERO - for fee_key in ['fees', 'commission']: - amount = getattr(raw, fee_key, None) - if amount is not None and amount != ZERO: - fee_total += amount - entry.postings.append( - Posting( - account=get_account_by_key(account, - fee_key + '_account'), - units=Amount(number=amount, currency=self.currency), - cost=None, - price=None, - flag=None, - meta=None)) - - if raw.trantype in STOCK_BUY_SELL_TYPES: - assert abs(total + fee_total + - (units * unitprice)) < TOLERANCE, abs( - total + fee_total + (units * unitprice)) - if not security_transaction_exists and not cash_transaction_exists: results.add_pending_entry( ImportResult( From 42f62a3c8cd4430f0e85d512c7815d9b95010a4e Mon Sep 17 00:00:00 2001 From: scanta2 Date: Sat, 25 Jul 2020 06:25:39 -0500 Subject: [PATCH 2/3] Implement suggestion for price fix --- beancount_import/source/ofx.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/beancount_import/source/ofx.py b/beancount_import/source/ofx.py index 5809e555..4ec275dc 100644 --- a/beancount_import/source/ofx.py +++ b/beancount_import/source/ofx.py @@ -948,8 +948,11 @@ def get_subaccount_cash(inv401ksource: Optional[str] = None) -> str: # Internal transfer, i.e. from after-tax to roth continue else: + number_per_fix = unitprice + if abs(total + fee_total + (units * unitprice)) >= TOLERANCE: + number_per_fix = normalize_fraction((abs(total)-abs(fee_total))/units) cost_spec = CostSpec( - number_per=(abs(total)-abs(fee_total))/units, + number_per=number_per_fix, number_total=None, currency=self.currency, date=None, From a01b78288c08d89e895a4600c1b700a103dc87fd Mon Sep 17 00:00:00 2001 From: scanta2 Date: Sat, 25 Jul 2020 08:42:23 -0500 Subject: [PATCH 3/3] Rebaseline tests --- .../import_results.beancount | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/testdata/source/ofx/test_investment_buy_sell_income/import_results.beancount b/testdata/source/ofx/test_investment_buy_sell_income/import_results.beancount index ce8401ff..6efda888 100644 --- a/testdata/source/ofx/test_investment_buy_sell_income/import_results.beancount +++ b/testdata/source/ofx/test_investment_buy_sell_income/import_results.beancount @@ -125,14 +125,14 @@ ; features: [] 2018-08-01 * "BUYSTOCK" + Expenses:Investment:MyBank:Fees 63.4869 USD + Expenses:Investment:MyBank:Commission 23.0233 USD Assets:Investment:MyBank:SDVMV 60.01318 SDVMV {67.141053527 USD} date: 2018-08-01 ofx_fitid: "aedf1852aa39a54-623ee.4d104.5" ofx_type: "BUYSTOCK" Assets:Investment:MyBank:Cash -4115.86 USD ofx_fitid: "aedf1852aa39a54-623ee.4d104.5" - Expenses:Investment:MyBank:Fees 63.4869 USD - Expenses:Investment:MyBank:Commission 23.0233 USD ;; date: 2018-08-01 ;; info: {"filename": "/investment_buy_sell_income.ofx", "type": "application/x-ofx"} @@ -150,6 +150,8 @@ ; features: [] 2018-08-01 * "SELLSTOCK" + Expenses:Investment:MyBank:Fees 31.9944 USD + Expenses:Investment:MyBank:Commission 57.7239 USD Assets:Investment:MyBank:EEBHF -41.50416 EEBHF {} @ 83.661853593 USD date: 2018-08-01 ofx_fitid: "4a5141ead2c672e8a559.65-80e.b" @@ -157,8 +159,6 @@ Income:MyBank:Capital-Gains:EEBHF Assets:Investment:MyBank:Cash 3382.60 USD ofx_fitid: "4a5141ead2c672e8a559.65-80e.b" - Expenses:Investment:MyBank:Fees 31.9944 USD - Expenses:Investment:MyBank:Commission 57.7239 USD ;; date: 2018-08-04 ;; info: {"filename": "/investment_buy_sell_income.ofx", "type": "application/x-ofx"}