use of io.bitsquare.btc.pricefeed.PriceFeedService in project bitsquare by bitsquare.
the class Offer method getPrice.
@Nullable
public Fiat getPrice() {
if (useMarketBasedPrice) {
checkNotNull(priceFeedService, "priceFeed must not be null");
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
if (marketPrice != null) {
PriceFeedService.Type priceFeedType;
double factor;
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
priceFeedType = direction == Direction.BUY ? PriceFeedService.Type.ASK : PriceFeedService.Type.BID;
factor = direction == Offer.Direction.SELL ? 1 - marketPriceMargin : 1 + marketPriceMargin;
} else {
priceFeedType = direction == Direction.SELL ? PriceFeedService.Type.ASK : PriceFeedService.Type.BID;
factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
}
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double targetPrice = marketPriceAsDouble * factor;
if (CurrencyUtil.isCryptoCurrency(currencyCode))
targetPrice = targetPrice != 0 ? 1d / targetPrice : 0;
try {
final double rounded = MathUtils.roundDouble(targetPrice, Fiat.SMALLEST_UNIT_EXPONENT);
return Fiat.parseFiat(currencyCode, decimalFormat.format(rounded).replace(",", "."));
} catch (Exception e) {
log.error("Exception at getPrice / parseToFiat: " + e.toString() + "\n" + "That case should never happen.");
return null;
}
} else {
log.debug("We don't have a market price.\n" + "That case could only happen if you don't have a price feed.");
return null;
}
} else {
return Fiat.valueOf(currencyCode, fiatPrice);
}
}
Aggregations