Skip to content

Commit

Permalink
Merge pull request #87 from tarioch/feature/ib_multiaccount
Browse files Browse the repository at this point in the history
Support multiple account for ib
  • Loading branch information
tarioch authored Mar 14, 2023
2 parents 9df8299 + 8c86a39 commit dff4621
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 94 deletions.
176 changes: 91 additions & 85 deletions src/tariochbctools/importers/ibkr/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,104 +46,109 @@ def extract(self, file, existing_entries):
assert isinstance(statement, Types.FlexQueryResponse)

transactions = []
for trx in statement.FlexStatements[0].CashTransactions:
existingEntry = None
if CashAction.DIVIDEND == trx.type or CashAction.WHTAX == trx.type:
existingEntry = next(
(t for t in transactions if self.matches(trx, t)), None
)
for stmt in statement.FlexStatements:
for trx in stmt.CashTransactions:
existingEntry = None
if CashAction.DIVIDEND == trx.type or CashAction.WHTAX == trx.type:
existingEntry = next(
(t for t in transactions if self.matches(trx, t)), None
)

if existingEntry:
if CashAction.WHTAX == trx.type:
existingEntry["whAmount"] += trx.amount
else:
existingEntry["amount"] += trx.amount
existingEntry["description"] = trx.description
existingEntry["type"] = trx.type
else:
if CashAction.WHTAX == trx.type:
amount = 0
whAmount = trx.amount
if existingEntry:
if CashAction.WHTAX == trx.type:
existingEntry["whAmount"] += trx.amount
else:
existingEntry["amount"] += trx.amount
existingEntry["description"] = trx.description
existingEntry["type"] = trx.type
else:
amount = trx.amount
whAmount = 0

transactions.append(
{
"date": trx.dateTime,
"symbol": trx.symbol,
"currency": trx.currency,
"amount": amount,
"whAmount": whAmount,
"description": trx.description,
"type": trx.type,
}
)
if CashAction.WHTAX == trx.type:
amount = 0
whAmount = trx.amount
else:
amount = trx.amount
whAmount = 0

transactions.append(
{
"date": trx.dateTime,
"symbol": trx.symbol,
"currency": trx.currency,
"amount": amount,
"whAmount": whAmount,
"description": trx.description,
"type": trx.type,
"account": stmt.accountId,
}
)

result = []
for trx in transactions:
if trx["type"] == CashAction.DIVIDEND:
asset = trx["symbol"].rstrip("z")
payDate = trx["date"].date()
totalDividend = trx["amount"]
totalWithholding = -trx["whAmount"]
totalPayout = totalDividend - totalWithholding
currency = trx["currency"]

_, rows = query.run_query(
existing_entries,
options.OPTIONS_DEFAULTS,
'select sum(number) as quantity, account where currency="'
+ asset
+ '" and date<#"'
+ str(payDate)
+ '" group by account;',
)
totalQuantity = D(0)
for row in rows:
totalQuantity += row.quantity

remainingPayout = totalPayout
remainingWithholding = totalWithholding
for row in rows[:-1]:
myAccount = row.account
myQuantity = row.quantity

myPayout = round(totalPayout * myQuantity / totalQuantity, 2)
remainingPayout -= myPayout
myWithholding = round(
totalWithholding * myQuantity / totalQuantity, 2
result = []
for trx in transactions:
if trx["type"] == CashAction.DIVIDEND:
asset = trx["symbol"].rstrip("z")
payDate = trx["date"].date()
totalDividend = trx["amount"]
totalWithholding = -trx["whAmount"]
totalPayout = totalDividend - totalWithholding
currency = trx["currency"]
account = trx["account"]

_, rows = query.run_query(
existing_entries,
options.OPTIONS_DEFAULTS,
'select sum(number) as quantity, account where currency="'
+ asset
+ '" and date<#"'
+ str(payDate)
+ '" group by account;',
)
remainingWithholding -= myWithholding
totalQuantity = D(0)
for row in rows:
totalQuantity += row.quantity

remainingPayout = totalPayout
remainingWithholding = totalWithholding
for row in rows[:-1]:
myAccount = row.account
myQuantity = row.quantity

myPayout = round(totalPayout * myQuantity / totalQuantity, 2)
remainingPayout -= myPayout
myWithholding = round(
totalWithholding * myQuantity / totalQuantity, 2
)
remainingWithholding -= myWithholding
result.append(
self.createSingle(
myPayout,
myWithholding,
myQuantity,
myAccount,
asset,
currency,
payDate,
priceLookup,
trx["description"],
account,
)
)

lastRow = rows[-1]
result.append(
self.createSingle(
myPayout,
myWithholding,
myQuantity,
myAccount,
remainingPayout,
remainingWithholding,
lastRow.quantity,
lastRow.account,
asset,
currency,
payDate,
priceLookup,
trx["description"],
account,
)
)

lastRow = rows[-1]
result.append(
self.createSingle(
remainingPayout,
remainingWithholding,
lastRow.quantity,
lastRow.account,
asset,
currency,
payDate,
priceLookup,
trx["description"],
)
)

return result

def createSingle(
Expand All @@ -157,6 +162,7 @@ def createSingle(
date,
priceLookup,
description,
account,
):
narration = "Dividend for " + str(quantity) + " : " + description
liquidityAccount = self.getLiquidityAccount(assetAccount, asset, currency)
Expand Down Expand Up @@ -191,7 +197,7 @@ def createSingle(
)
postings.append(data.Posting(incomeAccount, None, None, None, None, None))

meta = data.new_metadata("dividend", 0)
meta = data.new_metadata("dividend", 0, {"account": account})
return data.Transaction(
meta, date, "*", "", narration, data.EMPTY_SET, data.EMPTY_SET, postings
)
Expand Down
19 changes: 10 additions & 9 deletions src/tariochbctools/plugins/prices/ibkr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ def get_latest_price(self, ticker: str):
raise e

statement = parser.parse(response)
for position in statement.FlexStatements[0].OpenPositions:
if position.symbol.rstrip("z") == ticker:
price = D(position.markPrice)
timezone = tz.gettz("Europe/Zurich")
time = datetime.combine(
position.reportDate, datetime.min.time()
).astimezone(timezone)

return source.SourcePrice(price, time, position.currency)
for custStatement in statement.FlexStatements:
for position in custStatement.OpenPositions:
if position.symbol.rstrip("z") == ticker:
price = D(position.markPrice)
timezone = tz.gettz("Europe/Zurich")
time = datetime.combine(
position.reportDate, datetime.min.time()
).astimezone(timezone)

return source.SourcePrice(price, time, position.currency)

return None

Expand Down

0 comments on commit dff4621

Please sign in to comment.