Skip to content

Commit

Permalink
Interactive Brokers statement import: processing of extra tax for MLP.
Browse files Browse the repository at this point in the history
  • Loading branch information
titov-vv committed Apr 7, 2024
1 parent fcf39f8 commit 7c67986
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
24 changes: 22 additions & 2 deletions jal/data_import/broker_statements/ibkr.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class IBKR_AssetType:
'FUT': FOF.ASSET_FUTURES,
'WAR': FOF.ASSET_WARRANT,
'RIGHT': FOF.ASSET_RIGHTS,
'CFD': FOF.ASSET_CFD
'CFD': FOF.ASSET_CFD,
'MLP': FOF.ASSET_MLP
}

def __init__(self, asset_type, subtype):
Expand Down Expand Up @@ -982,7 +983,26 @@ def aggregate_taxes(self, taxes: list) -> list:
part['amount'] = sum(tax['amount'] for tax in group_list) # and update quantity in it
lieu_aggregated.append(part)
taxes_aggregated = sorted(other_taxes + lieu_aggregated, key=key_func)
return taxes_aggregated

# There might be additional record to withhold 10% of extra tax on partnerships (currently faced for MLP) reported in different dates
key_func = lambda x: (x['account'], x['asset'], x['currency'], x['description'], x['timestamp'])
mlp_taxes = [x for x in taxes_aggregated if self._find_in_list(self._data[FOF.ASSETS], 'id', x['asset'])['type'] == FOF.ASSET_MLP]
non_mlp_taxes = [x for x in taxes_aggregated if x not in mlp_taxes]
mlp_processed = []
for k, group in groupby(mlp_taxes, key=key_func):
group_list = sorted(list(group), key=lambda x: (x['amount'])) # sort to have higher tax first
if len(group_list) > 2:
raise Statement_ImportError(self.tr("Too many records for MLP tax: ") + f"{group_list}")
if len(group_list) == 2: # 1st line is expected to be a base tax and 2nd line seems to be additional 10%
tax = group_list[1] # Store 2nd line as separate asset payment record
tax['id'] = max([0] + [x['id'] for x in self._data[FOF.ASSET_PAYMENTS]]) + 1
tax['type'] = FOF.PAYMENT_FEE
tax['description'] += " - Extra 10% tax due to IRS section 1446"
self.drop_extra_fields(tax, ["source", "currency", "reported"])
self._data[FOF.ASSET_PAYMENTS].append(tax)
mlp_processed.append(group_list[0]) # Keep only base tax
taxes_processed = sorted(non_mlp_taxes + mlp_processed, key=key_func)
return taxes_processed

def load_taxes(self, taxes):
cnt = 0
Expand Down
1 change: 1 addition & 0 deletions jal/data_import/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FOF:
ASSET_RIGHTS = "right"
ASSET_CRYPTO = "crypto"
ASSET_CFD = "cfd"
ASSET_MLP = "mlp"

ACTION_MERGER = "merger"
ACTION_SPLIT = "split"
Expand Down
15 changes: 11 additions & 4 deletions tests/test_data/ibkr_dividends.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
{"id": 1, "name": "", "type": "money"},
{"id": 2, "type": "stock", "name": "BROOKFIELD RENEWABLE PARTNER", "isin": "BMG162581083", "country": "ca"},
{"id": 3, "type": "adr", "name": "TELEFONICA SA-SPON ADR", "isin": "US8793822086", "country": "es"},
{"id": 4, "type": "stock", "name": "OWL ROCK CAPITAL CORP", "isin": "US69121K1043", "country": "us"}
{"id": 4, "type": "stock", "name": "OWL ROCK CAPITAL CORP", "isin": "US69121K1043", "country": "us"},
{"id": 5, "type": "mlp", "name": "USA COMPRESSION PARTNERS LP", "isin": "US90290N1090", "country": "us"}
],
"symbols": [
{"id": 1, "asset": 1, "symbol": "USD"},
{"id": 2, "asset": 2, "symbol": "BEP", "currency": 1, "note": "NYSE"},
{"id": 3, "asset": 3, "symbol": "TEF", "currency": 1, "note": "NYSE"},
{"id": 4, "asset": 4, "symbol": "ORCC", "currency": 1, "note": "NYSE"}
{"id": 4, "asset": 4, "symbol": "ORCC", "currency": 1, "note": "NYSE"},
{"id": 5, "asset": 5, "symbol": "USAC", "currency": 1, "note": "NYSE"}
],
"assets_data": [
{"id": 1, "asset": 3, "reg_number": "879382208"},
{"id": 2, "asset": 4, "reg_number": "69121K104"}
{"id": 2, "asset": 4, "reg_number": "69121K104"},
{"id": 3, "asset": 5, "reg_number": "90290N109"}
],
"asset_payments": [
{"id": 1, "type": "stock_dividend", "account": 1, "timestamp": 1595017200, "number": "13259965038", "asset": 3, "amount": 3.0, "price": "4.73", "tax": 0, "description": "TEF (US8793822086) STOCK DIVIDEND US8793822086 416666667 FOR 10000000000"},
Expand All @@ -35,7 +38,11 @@
{"id": 13, "type": "dividend", "account": 1, "timestamp": 1621023600, "number": "", "asset": 4, "amount": 20.46, "tax": 2.05, "description": "ORCC(US69121K1043) CASH DIVIDEND USD 0.31 PER SHARE (Ordinary Dividend)"},
{"id": 14, "type": "dividend", "account": 1, "timestamp": 1628886000, "number": "", "asset": 4, "amount": 31.0, "tax": 3.1, "description": "ORCC(US69121K1043) CASH DIVIDEND USD 0.31 PER SHARE (Ordinary Dividend)"},
{"id": 15, "type": "dividend", "account": 1, "timestamp": 1611087600, "number": "", "asset": 4, "amount": 38.61, "tax": 3.58, "description": "ORCC(US69121K1043) PAYMENT IN LIEU OF DIVIDEND (Ordinary Dividend)"},
{"id": 16, "type": "dividend", "account": 1, "timestamp": 1621023600, "number": "", "asset": 4, "amount": 10.54, "tax": 1.05,"description": "ORCC(US69121K1043) PAYMENT IN LIEU OF DIVIDEND (Ordinary Dividend)"}
{"id": 16, "type": "dividend", "account": 1, "timestamp": 1621023600, "number": "", "asset": 4, "amount": 10.54, "tax": 1.05,"description": "ORCC(US69121K1043) PAYMENT IN LIEU OF DIVIDEND (Ordinary Dividend)"},
{"id": 17, "type": "dividend", "account": 1, "timestamp": 1675455600, "number": "", "asset": 5, "amount": 5.25, "tax": 1.94, "description": "USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE (Ordinary Dividend)"},
{"id": 18, "type": "dividend", "account": 1, "timestamp": 1683318000, "number": "", "asset": 5, "amount": 5.25, "tax": 1.94, "description": "USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE (Ordinary Dividend)"},
{"id": 19, "type": "fee", "account": 1, "timestamp": 1675455600, "number": "", "asset": 5, "amount": -0.53, "description": "USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX - Extra 10% tax due to IRS section 1446"},
{"id": 20, "type": "fee", "account": 1, "timestamp": 1683318000, "number": "", "asset": 5, "amount": -0.53, "description": "USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX - Extra 10% tax due to IRS section 1446"}
],
"corporate_actions": [],
"income_spending": [
Expand Down
8 changes: 8 additions & 0 deletions tests/test_data/ibkr_dividends.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<SecurityInfo currency="USD" assetCategory="STK" symbol="BEP" description="BROOKFIELD RENEWABLE PARTNER" conid="114646358" securityID="BMG162581083" securityIDType="ISIN" cusip="" isin="BMG162581083" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" maturity="" issueDate="" underlyingCategory="" subCategory="LTD PART" settlementPolicyMethod="" code=""/>
<SecurityInfo currency="USD" assetCategory="STK" symbol="TEF" description="TELEFONICA SA-SPON ADR" conid="12702" securityID="US8793822086" securityIDType="ISIN" cusip="879382208" isin="US8793822086" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" maturity="" issueDate="" underlyingCategory="" subCategory="ADR" settlementPolicyMethod="" code="" />
<SecurityInfo currency="USD" assetCategory="STK" symbol="ORCC" description="OWL ROCK CAPITAL CORP" conid="374690034" securityID="US69121K1043" securityIDType="ISIN" cusip="69121K104" isin="US69121K1043" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" maturity="" issueDate="" underlyingCategory="" subCategory="COMMON" settlementPolicyMethod="" code="" />
<SecurityInfo currency="USD" assetCategory="STK" symbol="USAC" description="USA COMPRESSION PARTNERS LP" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" maturity="" issueDate="" underlyingCategory="" subCategory="MLP" settlementPolicyMethod="" code="" />
</SecuritiesInfo>
<CashTransactions>
<CashTransaction type="Withholding Tax" accountId="U7654321" currency="USD" levelOfDetail="DETAIL" transactionID="20026653483" dateTime="20220331;202000" principalAdjustFactor="" reportDate="20220401" putCall="" tradeID="" expiry="" strike="" multiplier="1" issuer="" underlyingListingExchange="" underlyingSecurityID="" underlyingSymbol="" underlyingConid="" listingExchange="NYSE" isin="BMG162581083" cusip="" securityIDType="ISIN" securityID="BMG162581083" conid="114646358" description="BEP(BMG162581083) CASH DIVIDEND USD 0.3198 PER SHARE - CA TAX" symbol="BEP" assetCategory="STK" fxRateToBase="1" model="" acctAlias="" code="" amount="-1.92" clientReference="" settleDate="20220331"/>
Expand Down Expand Up @@ -73,6 +74,13 @@
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="ORCC" description="ORCC(US69121K1043) CASH DIVIDEND USD 0.31 PER SHARE (Ordinary Dividend)" conid="374690034" securityID="US69121K1043" securityIDType="ISIN" cusip="69121K104" isin="US69121K1043" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20210813;202000" settleDate="20210813" amount="31" type="Dividends" tradeID="" code="" transactionID="17383606551" reportDate="20210813" clientReference="" levelOfDetail="DETAIL" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="ORCC" description="ORCC(US69121K1043) PAYMENT IN LIEU OF DIVIDEND (Ordinary Dividend)" conid="374690034" securityID="US69121K1043" securityIDType="ISIN" cusip="69121K104" isin="US69121K1043" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20210119;202000" settleDate="20210119" amount="38.61" type="Payment In Lieu Of Dividends" tradeID="" code="" transactionID="14967549431" reportDate="20210119" clientReference="" levelOfDetail="DETAIL" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="ORCC" description="ORCC(US69121K1043) PAYMENT IN LIEU OF DIVIDEND (Ordinary Dividend)" conid="374690034" securityID="US69121K1043" securityIDType="ISIN" cusip="69121K104" isin="US69121K1043" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20210514;202000" settleDate="20210514" amount="10.54" type="Payment In Lieu Of Dividends" tradeID="" code="" transactionID="16408230020" reportDate="20210514" clientReference="" levelOfDetail="DETAIL" />
<!-- extra 10% tax for publicly traded partnerships according to IRS Section 1446(f) -->
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230203;202000" settleDate="20230203" amount="-0.53" type="Withholding Tax" tradeID="" code="" transactionID="22939793448" reportDate="20230203" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="114214309" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230203;202000" settleDate="20230203" amount="-1.94" type="Withholding Tax" tradeID="" code="" transactionID="22939793470" reportDate="20230203" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="114214309" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230505;202000" settleDate="20230505" amount="-1.94" type="Withholding Tax" tradeID="" code="" transactionID="23798936159" reportDate="20230505" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="116682532" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE - US TAX" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230505;202000" settleDate="20230505" amount="-0.53" type="Withholding Tax" tradeID="" code="" transactionID="25184537166" reportDate="20231002" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="116682532" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE (Ordinary Dividend)" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230203;202000" settleDate="20230203" amount="5.25" type="Dividends" tradeID="" code="" transactionID="22939793407" reportDate="20230203" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="114214309" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
<CashTransaction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="USAC" description="USAC(US90290N1090) CASH DIVIDEND USD 0.525 PER SHARE (Ordinary Dividend)" conid="120420959" securityID="US90290N1090" securityIDType="ISIN" cusip="90290N109" isin="US90290N1090" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230505;202000" settleDate="20230505" amount="5.25" type="Dividends" tradeID="" code="" transactionID="23798936154" reportDate="20230505" clientReference="" levelOfDetail="DETAIL" subCategory="MLP" actionID="116682532" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" />
</CashTransactions>
<CorporateActions>
<CorporateAction accountId="U7654321" acctAlias="" model="" currency="USD" fxRateToBase="1" assetCategory="STK" symbol="TEF" description="TEF (US8793822086) STOCK DIVIDEND US8793822086 5 FOR 100 (TEF, TELEFONICA SA-SPON ADR, US8793822086)" conid="12702" securityID="US8793822086" securityIDType="ISIN" cusip="879382208" isin="US8793822086" listingExchange="NYSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" reportDate="20210630" dateTime="20210630;202000" actionDescription="TEF (US8793822086) STOCK DIVIDEND US8793822086 5 FOR 100 (TEF, TELEFONICA SA-SPON ADR, US8793822086)" amount="0" proceeds="0" value="70.5" quantity="15" fifoPnlRealized="0" mtmPnl="0" code="" type="HI" transactionID="16907808998" levelOfDetail="DETAIL" />
Expand Down

0 comments on commit 7c67986

Please sign in to comment.