4646import name .abuchen .portfolio .model .Transaction ;
4747import name .abuchen .portfolio .model .Transaction .Unit ;
4848import name .abuchen .portfolio .money .CurrencyUnit ;
49+ import name .abuchen .portfolio .money .ExchangeRate ;
4950import name .abuchen .portfolio .money .Money ;
5051import name .abuchen .portfolio .money .Values ;
5152import 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