Search in sources :

Example 1 with PriceFeedService

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);
    }
}
Also used : MarketPrice(io.bitsquare.btc.pricefeed.MarketPrice) PriceFeedService(io.bitsquare.btc.pricefeed.PriceFeedService) IOException(java.io.IOException) MarketPriceNotAvailableException(io.bitsquare.trade.exceptions.MarketPriceNotAvailableException) TradePriceOutOfToleranceException(io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException) Nullable(javax.annotation.Nullable)

Aggregations

MarketPrice (io.bitsquare.btc.pricefeed.MarketPrice)1 PriceFeedService (io.bitsquare.btc.pricefeed.PriceFeedService)1 MarketPriceNotAvailableException (io.bitsquare.trade.exceptions.MarketPriceNotAvailableException)1 TradePriceOutOfToleranceException (io.bitsquare.trade.exceptions.TradePriceOutOfToleranceException)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1