JavaMoney / jsr354-ri-bp

Backport of the JSR 354 RI compatible with Java prior to SE8.
Other
9 stars 14 forks source link

Historical Exchange Rates #33

Closed RyanHoldren closed 6 years ago

RyanHoldren commented 7 years ago

I can't seem to get historical rates to work. I've tried a bunch of variations, but I either get the current exchange rate or this exception:

CurrencyConversionException [base=USD, term=CAD, conversionContext=null]: Cannot convert USD into CAD: All delegate prov iders failed to deliver rate, providers=[org.javamoney.moneta.internal.convert.IdentityRateProvider@3b088d51, org.javamoney.moneta.internal.convert.ECBCurrentRateProvider{ context: ProviderContext (
{rateTypes=[DEFERRED], providerDescription=European Central Bank, days=1, provider=ECB})}, org.javamoney.moneta.internal.convert.IMFRateProvider{ context: ProviderContext (
{rateTypes=[DEFERRED], providerDescription=International Monetary Fond, days=1, provider=IMF})}, org.javamoney.moneta.internal.convert.IMFHistoricRateProvider{ context: ProviderContext (
{rateTypes=[HISTORIC], providerDescription=Historic International Monetary Fond, days=0, provider=IMF-HIST})}, org.javamoney.moneta.internal.convert.ECBHistoricRateProvider{ context: ProviderContext (
{rateTypes=[DEFERRED, HISTORIC], providerDescription=European Central Bank, days=1500, provider=ECB-HIST})}, org.javamoney.moneta.internal.convert.ECBHistoric90RateProvider{ context: ProviderContext (
{rateTypes=[DEFERRED, HISTORIC], providerDescription=European Central Bank (last 90 days), days=90, provider=ECB-HIST90})}], query=ConversionQuery (
{Query.termCurrency=CAD, Query.baseCurrency=USD, java.time.LocalDate=2000-01-01})
    at org.javamoney.moneta.spi.CompoundRateProvider.getExchangeRate(CompoundRateProvider.java:121)
    at com.github.ryanholdren.portfolio.utilities.CurrencyTest.test(CurrencyTest.java:27)
    ...

Here is an example of one of my attempts:

import java.time.LocalDate;
import static java.time.Month.JANUARY;
import javax.money.convert.ConversionQuery;
import javax.money.convert.ConversionQueryBuilder;
import javax.money.convert.ExchangeRate;
import javax.money.convert.ExchangeRateProvider;
import static javax.money.convert.MonetaryConversions.getExchangeRateProvider;
import org.junit.Test;
import org.springframework.util.Assert;

public class ExchangeRateTest {
    @Test
    public void test() {
        final ConversionQuery query = ConversionQueryBuilder.of()
            .setBaseCurrency("USD")
            .setTermCurrency("CAD")
            .set(LocalDate.class, LocalDate.of(2000, JANUARY, 1))
            .build();
        final ExchangeRateProvider provider = getExchangeRateProvider(query);
        final ExchangeRate rate = provider.getExchangeRate(query);
        Assert.notNull(rate);
    }
}
keilw commented 7 years ago

Have to look at it. Out of curiosity, did you try with a release version like 1.0 or 1.1, too? Just trying to verify if it existed there or came up with modularization?

RyanHoldren commented 7 years ago

It looks like 1.0 doesn't throw an exception. Instead it returns the current exchange rate rather than the historical.

keilw commented 7 years ago

Ok, hard to say which one is better. Some may say throwing an exception is better than incorrect data. Will look into it.

keilw commented 7 years ago

For reasons of backward-compatibility the 1.0 behavior seems preferred.

atsticks commented 7 years ago

You try to pass your date as java.time.LocalDate, which is a JDK 8 type not available in JDK 7. The backport explicitly uses JDK7, so it is not aware of this type. Use the jsr354-ri with JDK8, please.

keilw commented 7 years ago

So is there any problem on the JSR side or mainly on how @RyanHoldren's application tries to call RI and API?

atsticks commented 6 years ago

It is the wrong parameter for the bp RI. Use either the Java 8 RI, or use Date or the LocalDate copy that comes with bp-ri. Issue closed.