@Override public double fxRate(Currency baseCurrency, Currency counterCurrency) { return fxRateProvider.fxRate(baseCurrency, counterCurrency); }
@Override public JumpToDefault convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { Map<StandardId, Double> mutable = new HashMap<>(); for (Entry<StandardId, Double> entry : amounts.entrySet()) { double converted = rateProvider.convert(entry.getValue(), currency, resultCurrency); mutable.put(entry.getKey(), converted); } return JumpToDefault.of(resultCurrency, mutable); }
/** * Converts an amount in a currency to an amount in a different currency using this rate. * <p> * The currencies must both be included in the currency pair of this rate. * * @param amount an amount in {@code fromCurrency} * @param fromCurrency the currency of the amount * @param toCurrency the currency into which the amount should be converted * @return the amount converted into {@code toCurrency} * @throws IllegalArgumentException if either of the currencies aren't included in the currency pair of this rate */ public default double convert(double amount, Currency fromCurrency, Currency toCurrency) { return amount * fxRate(fromCurrency, toCurrency); }
/** * Converts this amount to an equivalent amount in the specified currency. * <p> * The result will be expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public Money convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.equals(resultCurrency)) { return this; } double converted = rateProvider.convert(amount.doubleValue(), currency, resultCurrency); return Money.of(resultCurrency, converted); }
/** * Gets the FX rate for the specified currency pair and scenario index. * <p> * The rate returned is the rate from the base currency to the counter currency * as defined by this formula: {@code (1 * baseCurrency = fxRate * counterCurrency)}. * This will return 1 if the two input currencies are the same. * * @param baseCurrency the base currency, to convert from * @param counterCurrency the counter currency, to convert to * @param scenarioIndex the scenario index * @return the FX rate for the currency pair * @throws RuntimeException if no FX rate could be found */ public default double fxRate(Currency baseCurrency, Currency counterCurrency, int scenarioIndex) { return fxRateProvider(scenarioIndex).fxRate(baseCurrency, counterCurrency); }
/** * Converts this amount to an equivalent amount in the specified currency. * <p> * The result will be expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public CurrencyAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.equals(resultCurrency)) { return this; } double converted = rateProvider.convert(amount, currency, resultCurrency); return CurrencyAmount.of(resultCurrency, converted); }
/** * Gets the FX rate for the specified currency pair. * <p> * The rate returned is the rate from the base currency to the counter currency * as defined by this formula: {@code (1 * baseCurrency = fxRate * counterCurrency)}. * * @param currencyPair the ordered currency pair defining the rate required * @return the FX rate for the currency pair * @throws RuntimeException if no FX rate could be found */ public default double fxRate(CurrencyPair currencyPair) { return fxRate(currencyPair.getBase(), currencyPair.getCounter()); }
/** * Converts this amount to an equivalent amount the specified currency. * <p> * The result will be expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, which should be expressed in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public CurrencyAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (amounts.size() == 1) { return amounts.first().convertedTo(resultCurrency, rateProvider); } double total = 0d; for (CurrencyAmount amount : amounts) { total += rateProvider.convert(amount.getAmount(), amount.getCurrency(), resultCurrency); } return CurrencyAmount.of(resultCurrency, total); }
@Override public CurrencyAmountArray convertedTo(Currency resultCurrency, FxRateProvider fxRateProvider) { double[] singleCurrencyValues = new double[size]; for (Map.Entry<Currency, DoubleArray> entry : values.entrySet()) { Currency currency = entry.getKey(); DoubleArray currencyValues = entry.getValue(); for (int i = 0; i < size; i++) { singleCurrencyValues[i] += currencyValues.get(i) * fxRateProvider.fxRate(currency, resultCurrency); } } return CurrencyAmountArray.of(resultCurrency, DoubleArray.ofUnsafe(singleCurrencyValues)); }
/** * Converts this sensitivity to an equivalent in the specified currency. * <p> * Any FX conversion that is required will use rates from the provider. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the sensitivity object expressed in terms of the result currency * @throws RuntimeException if no FX rate could be found */ @Override public CurrencyParameterSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.equals(resultCurrency)) { return this; } double fxRate = rateProvider.fxRate(currency, resultCurrency); return mapSensitivity(s -> s * fxRate, resultCurrency); }
/** * Converts this sensitivity to an equivalent in the specified currency. * <p> * Any FX conversion that is required will use rates from the provider. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the sensitivity object expressed in terms of the result currency * @throws RuntimeException if no FX rate could be found */ @Override public CrossGammaParameterSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.equals(resultCurrency)) { return this; } double fxRate = rateProvider.fxRate(currency, resultCurrency); return mapSensitivity(s -> s * fxRate, resultCurrency); }
@Override public CurrencyAmountArray convertedTo(Currency resultCurrency, FxRateProvider fxRateProvider) { if (currency.equals(resultCurrency)) { return this; } double fxRate = fxRateProvider.fxRate(currency, resultCurrency); DoubleArray convertedValues = values.multipliedBy(fxRate); return new CurrencyAmountArray(resultCurrency, convertedValues); }
public void emptyMatrixCanHandleTrivialRate() { FxRateProvider test = (ccy1, ccy2) -> { return 2.5d; }; assertThat(test.fxRate(CurrencyPair.of(GBP, USD))).isEqualTo(2.5d); }
public void cross_specified() { Map<FxRateId, FxRate> marketDataMap = ImmutableMap.of(FxRateId.of(EUR, CHF), FxRate.of(EUR, CHF, EUR_CHF), FxRateId.of(GBP, CHF), FxRate.of(GBP, CHF, GBP_CHF)); MarketData marketData = ImmutableMarketData.of(VAL_DATE, marketDataMap); FxRateProvider fx = MarketDataFxRateProvider.of(marketData, ObservableSource.NONE, CHF); assertEquals(fx.fxRate(GBP, EUR), GBP_CHF / EUR_CHF, 1.0E-10); assertEquals(fx.fxRate(EUR, GBP), EUR_CHF / GBP_CHF, 1.0E-10); assertThrows(() -> fx.fxRate(EUR, USD), MarketDataNotFoundException.class); }
public void cross_base() { Map<FxRateId, FxRate> marketDataMap = ImmutableMap.of(FxRateId.of(EUR, USD), FxRate.of(EUR, USD, EUR_USD), FxRateId.of(GBP, USD), FxRate.of(GBP, USD, GBP_USD)); MarketData marketData = ImmutableMarketData.of(VAL_DATE, marketDataMap); FxRateProvider fx = MarketDataFxRateProvider.of(marketData); assertEquals(fx.fxRate(GBP, EUR), GBP_USD / EUR_USD, 1.0E-10); assertEquals(fx.fxRate(EUR, GBP), EUR_USD / GBP_USD, 1.0E-10); }
public void cross_counter() { Map<FxRateId, FxRate> marketDataMap = ImmutableMap.of(FxRateId.of(EUR, USD), FxRate.of(EUR, USD, EUR_USD), FxRateId.of(EUR, BEF), FxRate.of(EUR, BEF, EUR_BEF)); MarketData marketData = ImmutableMarketData.of(VAL_DATE, marketDataMap); FxRateProvider fx = MarketDataFxRateProvider.of(marketData); assertEquals(fx.fxRate(USD, BEF), EUR_BEF / EUR_USD, 1.0E-10); assertEquals(fx.fxRate(BEF, USD), EUR_USD / EUR_BEF, 1.0E-10); }
public void cross_double_triangle() { Map<FxRateId, FxRate> marketDataMap = ImmutableMap.of(FxRateId.of(EUR, USD), FxRate.of(EUR, USD, EUR_USD), FxRateId.of(EUR, BEF), FxRate.of(EUR, BEF, EUR_BEF), FxRateId.of(GBP, USD), FxRate.of(GBP, USD, GBP_USD)); MarketData marketData = ImmutableMarketData.of(VAL_DATE, marketDataMap); FxRateProvider fx = MarketDataFxRateProvider.of(marketData); assertEquals(fx.fxRate(GBP, BEF), GBP_USD * EUR_BEF / EUR_USD, 1.0E-10); assertEquals(fx.fxRate(BEF, GBP), EUR_USD / EUR_BEF / GBP_USD, 1.0E-10); }
@Override public double rate(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 forwardRate = fxRateProvider.fxRate(currencyPair) * (dfCcyBaseAtMaturity / dfCcyCounterAtMaturity); return inverse ? 1d / forwardRate : forwardRate; }
/** * Converts this instance to an equivalent amount in the specified currency. * <p> * The result will be expressed in terms of the given currency. * Any FX conversion that is required will use rates from the provider. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, which should be expressed in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public default PointSensitivity convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (getCurrency().equals(resultCurrency)) { return this; } double fxRate = rateProvider.fxRate(getCurrency(), resultCurrency); return withCurrency(resultCurrency).withSensitivity(fxRate * getSensitivity()); }
@Override public CurrencyParameterSensitivities parameterSensitivity(FxForwardSensitivity pointSensitivity) { // use the specified base currency to determine the desired currency pair // then derive sensitivity from discount factors based off desired currency pair, not that of the index CurrencyPair currencyPair = pointSensitivity.getCurrencyPair(); Currency refBaseCurrency = pointSensitivity.getReferenceCurrency(); Currency refCounterCurrency = pointSensitivity.getReferenceCounterCurrency(); Currency sensitivityCurrency = pointSensitivity.getCurrency(); LocalDate referenceDate = pointSensitivity.getReferenceDate(); boolean inverse = refBaseCurrency.equals(currencyPair.getCounter()); DiscountFactors discountFactorsRefBase = (inverse ? counterCurrencyDiscountFactors : baseCurrencyDiscountFactors); DiscountFactors discountFactorsRefCounter = (inverse ? baseCurrencyDiscountFactors : counterCurrencyDiscountFactors); double dfCcyBaseAtMaturity = discountFactorsRefBase.discountFactor(referenceDate); double dfCcyCounterAtMaturityInv = 1d / discountFactorsRefCounter.discountFactor(referenceDate); double fxRate = fxRateProvider.fxRate(refBaseCurrency, refCounterCurrency); ZeroRateSensitivity dfCcyBaseAtMaturitySensitivity = discountFactorsRefBase.zeroRatePointSensitivity(referenceDate, sensitivityCurrency) .multipliedBy(fxRate * dfCcyCounterAtMaturityInv * pointSensitivity.getSensitivity()); ZeroRateSensitivity dfCcyCounterAtMaturitySensitivity = discountFactorsRefCounter.zeroRatePointSensitivity(referenceDate, sensitivityCurrency) .multipliedBy(-fxRate * dfCcyBaseAtMaturity * dfCcyCounterAtMaturityInv * dfCcyCounterAtMaturityInv * pointSensitivity.getSensitivity()); return discountFactorsRefBase.parameterSensitivity(dfCcyBaseAtMaturitySensitivity) .combinedWith(discountFactorsRefCounter.parameterSensitivity(dfCcyCounterAtMaturitySensitivity)); }