public void test_of_CurrencyCurrency_same() { CurrencyPair test = CurrencyPair.of(USD, USD); assertEquals(test.getBase(), USD); assertEquals(test.getCounter(), USD); assertEquals(test.isIdentity(), true); assertEquals(test.toString(), "USD/USD"); }
if (isIdentity() || other.isIdentity() || this.equals(other) || this.equals(other.inverse())) { return Optional.empty(); return Optional.of(of(base, other.counter).toConventional()); return Optional.of(of(base, other.base).toConventional()); return Optional.of(of(counter, other.counter).toConventional()); return Optional.of(of(counter, other.base).toConventional());
/** * Returns the market convention currency pair for the currencies in the pair. * <p> * If {@link #isConventional()} is {@code true} this method returns {@code this}, otherwise * it returns the {@link #inverse} pair. * * @return the market convention currency pair for the currencies in the pair */ public CurrencyPair toConventional() { return isConventional() ? this : inverse(); }
/** * Gets the currency counter to the reference currency. * <p> * The currency pair contains two currencies. One is the reference currency. * This method returns the other. * * @return the counter currency */ public Currency getReferenceCounterCurrency() { boolean inverse = referenceCurrency.equals(currencyPair.getBase()); return inverse ? currencyPair.getCounter() : currencyPair.getBase(); }
/** * Returns an FX rate object representing the market convention rate between the two currencies. * <p> * If the currency pair is the market convention pair, this method returns {@code this}, otherwise * it returns an {@code FxRate} with the inverse currency pair and reciprocal rate. * * @return an FX rate object representing the market convention rate between the two currencies */ public FxRate toConventional() { return pair.isConventional() ? this : FxRate.of(pair.toConventional(), 1 / rate); }
/** * Obtains an instance representing the FX rate for a currency pair. * * @param base the base currency of the pair * @param counter the counter currency of the pair * @return an ID for the FX rate for the currency pair */ public static FxRateId of(Currency base, Currency counter) { return new FxRateId(CurrencyPair.of(base, counter), ObservableSource.NONE); }
private static FxRate computeCross(FxRate fx1, FxRate fx2, CurrencyPair crossPairAC) { // aim is to convert AAA/BBB and BBB/CCC to AAA/CCC Currency currA = crossPairAC.getBase(); Currency currC = crossPairAC.getCounter(); // given the conventional cross rate pair, order the two rates to match boolean crossBaseCurrencyInFx1 = fx1.pair.contains(currA); FxRate fxABorBA = crossBaseCurrencyInFx1 ? fx1 : fx2; FxRate fxBCorCB = crossBaseCurrencyInFx1 ? fx2 : fx1; // extract the rates, taking the inverse if the pair is in the inverse order double rateAB = fxABorBA.getPair().getBase().equals(currA) ? fxABorBA.rate : 1d / fxABorBA.rate; double rateBC = fxBCorCB.getPair().getCounter().equals(currC) ? fxBCorCB.rate : 1d / fxBCorCB.rate; return FxRate.of(crossPairAC, rateAB * rateBC); }
if (!nearRate.getPair().contains(currency1)) { throw new IllegalArgumentException(Messages.format( "FxRate '{}' and CurrencyAmount '{}' must have a currency in common", nearRate, amount)); if (!nearRate.getPair().toConventional().equals(farRate.getPair().toConventional())) { throw new IllegalArgumentException(Messages.format( "FxRate '{}' and FxRate '{}' must contain the same currencies", nearRate, farRate));
public void test_equals_hashCode() { CurrencyPair a1 = CurrencyPair.of(AUD, GBP); CurrencyPair a2 = CurrencyPair.of(AUD, GBP); CurrencyPair b = CurrencyPair.of(USD, GBP); CurrencyPair c = CurrencyPair.of(USD, EUR); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(c), false); assertEquals(b.equals(a1), false); assertEquals(b.equals(a2), false); assertEquals(b.equals(b), true); assertEquals(b.equals(c), false); assertEquals(c.equals(a1), false); assertEquals(c.equals(a2), false); assertEquals(c.equals(b), false); assertEquals(c.equals(c), true); assertEquals(a1.hashCode(), a2.hashCode()); }
@ImmutableValidator private void validate() { CurrencyPair pair = index.getCurrencyPair(); if (!pair.contains(settlementCurrencyNotional.getCurrency())) { throw new IllegalArgumentException("FxIndex and settlement notional currency are incompatible"); } if (!(pair.equals(agreedFxRate.getPair()) || pair.isInverse(agreedFxRate.getPair()))) { throw new IllegalArgumentException("FxIndex and agreed FX rate are incompatible"); } }
public void test_toConventional() { assertEquals(CurrencyPair.of(GBP, USD).toConventional(), CurrencyPair.of(GBP, USD)); assertEquals(CurrencyPair.of(USD, GBP).toConventional(), CurrencyPair.of(GBP, USD)); assertEquals(CurrencyPair.of(GBP, BRL).toConventional(), CurrencyPair.of(GBP, BRL)); assertEquals(CurrencyPair.of(BRL, GBP).toConventional(), CurrencyPair.of(GBP, BRL)); assertEquals(CurrencyPair.of(BHD, BRL).toConventional(), CurrencyPair.of(BHD, BRL)); assertEquals(CurrencyPair.of(BRL, BHD).toConventional(), CurrencyPair.of(BHD, BRL)); }
@Override public double rateFxSpotSensitivity(Currency baseCurrency, LocalDate referenceDate) { ArgChecker.isTrue( currencyPair.contains(baseCurrency), "Currency {} invalid for CurrencyPair {}", baseCurrency, currencyPair); boolean inverse = baseCurrency.equals(currencyPair.getCounter()); double dfCcyBaseAtMaturity = baseCurrencyDiscountFactors.discountFactor(referenceDate); double dfCcyCounterAtMaturity = counterCurrencyDiscountFactors.discountFactor(referenceDate); double forwardRateDelta = dfCcyBaseAtMaturity / dfCcyCounterAtMaturity; return inverse ? 1d / forwardRateDelta : forwardRateDelta; }
@ImmutablePreBuild private static void preBuild(Builder builder) { // swap order to be base/counter if reverse is conventional // this handles deserialization where the base/counter rules differ from those applicable at serialization Payment base = builder.baseCurrencyPayment; Payment counter = builder.counterCurrencyPayment; CurrencyPair pair = CurrencyPair.of(counter.getCurrency(), base.getCurrency()); if (pair.isConventional()) { builder.baseCurrencyPayment = counter; builder.counterCurrencyPayment = base; } }
@Override public String getName() { return name != null ? name : currencyPair.toString(); }
@ImmutableValidator private void validate() { if (!currencyPair.contains(referenceCurrency)) { throw new IllegalArgumentException(Messages.format( "Reference currency {} must be one of those in the currency pair {}", referenceCurrency, currencyPair)); } }