Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple account for ib #87

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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