jamsix / ib-edavki

Skripta, ki prevede XML poročilo trgovalnih poslov v platformi InteractiveBrokers v XML format primeren za uvoz v obrazce Doh-KDVP, D-IFI, Doh-Div in Doh-Obr v eDavkih Finančne uprave.
MIT License
173 stars 57 forks source link

Dividend withholding tax not taken into account in some cases #136

Open ddbk opened 1 month ago

ddbk commented 1 month ago

Withholding tax is not accounted for in some cases. In my case, I had a dividend that was paid like this:

<CashTransaction accountId="UXXXXXXX" acctAlias="" model="" currency="CAD" fxRateToBase="0.69347" assetCategory="STK" symbol="TAL" description="TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 (Ordinary Dividend)" conid="320362585" securityID="CA71677J1012" securityIDType="ISIN" cusip="" isin="CA71677J1012" listingExchange="TSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230915;202000" settleDate="20230915" amount="456.61" type="Dividends" tradeID="" code="" transactionID="579312265" reportDate="20230918" clientReference="" levelOfDetail="DETAIL" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" actionID="125883467" subCategory="COMMON" figi="BBG000BCP413" issuerCountryCode="" />
<CashTransaction accountId="UXXXXXXX" acctAlias="" model="" currency="CAD" fxRateToBase="0.69347" assetCategory="STK" symbol="TAL" description="TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 - REVERSAL (Ordinary Dividend)" conid="320362585" securityID="CA71677J1012" securityIDType="ISIN" cusip="" isin="CA71677J1012" listingExchange="TSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230915;202000" settleDate="20230915" amount="-456.61" type="Dividends" tradeID="" code="" transactionID="579616301" reportDate="20230918" clientReference="" levelOfDetail="DETAIL" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" actionID="125883467" subCategory="COMMON" figi="BBG000BCP413" issuerCountryCode="" />
<CashTransaction accountId="UXXXXXXX" acctAlias="" model="" currency="CAD" fxRateToBase="0.69347" assetCategory="STK" symbol="TAL" description="TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 (Ordinary Dividend)" conid="320362585" securityID="CA71677J1012" securityIDType="ISIN" cusip="" isin="CA71677J1012" listingExchange="TSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230915;202000" settleDate="20230915" amount="456.6" type="Dividends" tradeID="" code="" transactionID="579616302" reportDate="20230918" clientReference="" levelOfDetail="DETAIL" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" actionID="125883467" subCategory="COMMON" figi="BBG000BCP413" issuerCountryCode="" />

The withholding tax that was related to this dividend was:

<CashTransaction accountId="UXXXXXXX" acctAlias="" model="" currency="CAD" fxRateToBase="0.69347" assetCategory="STK" symbol="TAL" description="TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 PAYMENT IN LIEU OF DIVIDEND - US TAX" conid="320362585" securityID="CA71677J1012" securityIDType="ISIN" cusip="" isin="CA71677J1012" listingExchange="TSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230915;202000" settleDate="20230915" amount="-0.01" type="Withholding Tax" tradeID="" code="" transactionID="579312264" reportDate="20230918" clientReference="" levelOfDetail="DETAIL" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" actionID="125883467" subCategory="COMMON" figi="BBG000BCP413" issuerCountryCode="" />
<CashTransaction accountId="UXXXXXXX" acctAlias="" model="" currency="CAD" fxRateToBase="0.69347" assetCategory="STK" symbol="TAL" description="TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 - US TAX" conid="320362585" securityID="CA71677J1012" securityIDType="ISIN" cusip="" isin="CA71677J1012" listingExchange="TSE" underlyingConid="" underlyingSymbol="" underlyingSecurityID="" underlyingListingExchange="" issuer="" multiplier="1" strike="" expiry="" putCall="" principalAdjustFactor="" dateTime="20230915;202000" settleDate="20230915" amount="-68.49" type="Withholding Tax" tradeID="" code="" transactionID="579312266" reportDate="20230918" clientReference="" levelOfDetail="DETAIL" serialNumber="" deliveryType="" commodityType="" fineness="0.0" weight="0.0" actionID="125883467" subCategory="COMMON" figi="BBG000BCP413" issuerCountryCode="" />

We need to look into this before the next tax season to not pay more taxes than required.

RokLenarcic commented 1 month ago

The problem with this particular set of data is that it's wrong.

Here's the sequence of events as far as I can understand them:

TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 (Ordinary Dividend)
TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 - REVERSAL (Ordinary Dividend)
TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 (Ordinary Dividend)

But the quantities are:

456.6
-456.61
456.61

So the reversal quantity does not match the dividend it's reversing.

The current code will match taxes with dividends one to one, but there seems to be 2 tax entries here for one actual dividend. It's not clear if tax entries can relate to reversed dividends.

I guess one solution would be to just roll together all the dividends and taxes related to same stock and same day into one entry. But that wouldn't really work if reversal is done on another day.

The code links a tax entry to the first entry, which gets reversed and then a new one is made.

RokLenarcic commented 1 month ago

Also the current code will match tax to a dividend with lower transaction ID.

https://github.com/jamsix/ib-edavki/blob/master/ib_edavki.py#L1158

Now if we look at the transactions, sorted by ID, your case, the order of events is nonsensical:

TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 PAYMENT IN LIEU OF DIVIDEND - US TAX
TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 (Ordinary Dividend)
TAL (CA71677J1012) CASH DIVIDEND CAD 0.033828 - US TAX
TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 - REVERSAL (Ordinary Dividend)
TAL (CA71677J1012) CASH DIVIDEND CAD 0.0338275 (Ordinary Dividend)

So it seems we cannot rely on IBKR to have these in a proper order.