
io.mstream.trader.datafeed.stocks.quandl.QuandlStockRepository Maven / Gradle / Ivy
The newest version!
package io.mstream.trader.datafeed.stocks.quandl;
import io.mstream.trader.commons.utils.exception.NotFoundException;
import io.mstream.trader.datafeed.stocks.StocksRepository;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import rx.Single;
import rx.functions.Func1;
import javax.inject.Inject;
import java.math.BigDecimal;
public class QuandlStockRepository
implements StocksRepository {
private final CurrencyUnit currencyUnit;
private final QuandlClient quandlClient;
@Inject
public QuandlStockRepository(
CurrencyUnit currencyUnit,
QuandlClient quandlClient
) {
this.currencyUnit = currencyUnit;
this.quandlClient = quandlClient;
}
@Override
public Single get(Key key) {
return quandlClient
.price(key.getStock(), key.getDate())
.map(resultMapper())
.onErrorResumeNext(errorHandler());
}
private Func1 resultMapper() {
return valueText -> {
BigDecimal decimalValue = new BigDecimal(valueText)
.setScale(2, BigDecimal.ROUND_HALF_UP);
return Money.of(currencyUnit, decimalValue);
};
}
private Func1> errorHandler() {
return throwable -> {
if (throwable instanceof NotFoundException) {
return Single.error(NotFoundException.get());
}
return Single.error(
new RuntimeException(
"could not get the stock price",
throwable
)
);
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy