Skip to content

Commit d188be5

Browse files
committed
IBFlexStatementExtractor: Cash xacts: Properly deal with diff. currency pairs
There can be different currency pairs, e.g. EUR.USD vs USD.CAD, and it can be exchanged in one or another direction.
1 parent 4925515 commit d188be5

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

name.abuchen.portfolio/src/name/abuchen/portfolio/datatransfer/ibflex/IBFlexStatementExtractor.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import name.abuchen.portfolio.model.Transaction;
4747
import name.abuchen.portfolio.model.Transaction.Unit;
4848
import name.abuchen.portfolio.money.CurrencyUnit;
49+
import name.abuchen.portfolio.money.ExchangeRate;
4950
import name.abuchen.portfolio.money.Money;
5051
import name.abuchen.portfolio.money.Values;
5152
import name.abuchen.portfolio.online.QuoteFeed;
@@ -417,8 +418,9 @@ else if (element.hasAttribute("dateTime"))
417418
return null;
418419
}
419420

420-
// Split the symbol into source and target currency (e.g. EUR.USD)
421-
// where USD is the currency paid and EUR is the currency bought
421+
// Split the symbol into "source" and "target" currency (e.g. EUR.USD)
422+
// where USD is the currency paid and EUR is the currency bought. We
423+
// may need to swap what's source and what't target below.
422424
String symbol = element.getAttribute("symbol");
423425
String[] parts = symbol.split("\\.");
424426
if (parts.length != 2)
@@ -428,6 +430,20 @@ else if (element.hasAttribute("dateTime"))
428430
String targetCurrency = parts[0];
429431
String sourceAmount = element.getAttribute("tradeMoney");
430432
String targetAmount = element.getAttribute("quantity");
433+
BigDecimal exachangeRate = asExchangeRate(element.getAttribute("tradePrice"));
434+
// If "quantity" is negative, then we're dealing with pair like
435+
// USD.CAD, exchanging USD to CAD, or maybe EUR.USD, exchanging
436+
// EUR to USD.
437+
if (targetAmount.startsWith("-"))
438+
{
439+
var t1 = sourceAmount;
440+
sourceAmount = targetAmount;
441+
targetAmount = t1;
442+
var t2 = sourceCurrency;
443+
sourceCurrency = targetCurrency;
444+
targetCurrency = t2;
445+
exachangeRate = ExchangeRate.inverse(exachangeRate);
446+
}
431447

432448
// Set the amounts
433449
Money source = Money.of(sourceCurrency, asAmount(sourceAmount));
@@ -438,7 +454,6 @@ else if (element.hasAttribute("dateTime"))
438454
cashTransaction.getSourceTransaction().setMonetaryAmount(source);
439455
cashTransaction.getTargetTransaction().setMonetaryAmount(target);
440456

441-
BigDecimal exachangeRate = asExchangeRate(element.getAttribute("tradePrice"));
442457
cashTransaction.getSourceTransaction().addUnit(new Unit(Unit.Type.GROSS_VALUE, source, target, exachangeRate));
443458

444459
// Set fees

0 commit comments

Comments
 (0)