Search in sources :

Example 6 with MarketPrice

use of bisq.core.provider.price.MarketPrice in project bisq-desktop by bisq-network.

the class MainViewModel method setMarketPriceInItems.

private void setMarketPriceInItems() {
    priceFeedComboBoxItems.stream().forEach(item -> {
        String currencyCode = item.currencyCode;
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        String priceString;
        if (marketPrice != null && marketPrice.isPriceAvailable()) {
            priceString = formatter.formatMarketPrice(marketPrice.getPrice(), currencyCode);
            item.setPriceAvailable(true);
            item.setExternallyProvidedPrice(marketPrice.isExternallyProvidedPrice());
        } else {
            priceString = Res.get("shared.na");
            item.setPriceAvailable(false);
        }
        item.setDisplayString(formatter.getCurrencyPair(currencyCode) + ": " + priceString);
        final String code = item.currencyCode;
        if (selectedPriceFeedComboBoxItemProperty.get() != null && selectedPriceFeedComboBoxItemProperty.get().currencyCode.equals(code)) {
            isFiatCurrencyPriceFeedSelected.set(CurrencyUtil.isFiatCurrency(code) && CurrencyUtil.getFiatCurrency(code).isPresent() && item.isPriceAvailable() && item.isExternallyProvidedPrice());
            isCryptoCurrencyPriceFeedSelected.set(CurrencyUtil.isCryptoCurrency(code) && CurrencyUtil.getCryptoCurrency(code).isPresent() && item.isPriceAvailable() && item.isExternallyProvidedPrice());
            isExternallyProvidedPrice.set(item.isExternallyProvidedPrice());
            isPriceAvailable.set(item.isPriceAvailable());
            marketPriceUpdated.set(marketPriceUpdated.get() + 1);
        }
    });
}
Also used : MarketPrice(bisq.core.provider.price.MarketPrice)

Example 7 with MarketPrice

use of bisq.core.provider.price.MarketPrice in project bisq-core by bisq-network.

the class Offer method getPrice.

@Nullable
public Price getPrice() {
    String currencyCode = getCurrencyCode();
    if (offerPayload.isUseMarketBasedPrice()) {
        checkNotNull(priceFeedService, "priceFeed must not be null");
        MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
        if (marketPrice != null && marketPrice.isRecentExternalPriceAvailable()) {
            double factor;
            double marketPriceMargin = offerPayload.getMarketPriceMargin();
            if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
                factor = getDirection() == OfferPayload.Direction.SELL ? 1 - marketPriceMargin : 1 + marketPriceMargin;
            } else {
                factor = getDirection() == OfferPayload.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
            }
            double marketPriceAsDouble = marketPrice.getPrice();
            double targetPriceAsDouble = marketPriceAsDouble * factor;
            try {
                int precision = CurrencyUtil.isCryptoCurrency(currencyCode) ? Altcoin.SMALLEST_UNIT_EXPONENT : Fiat.SMALLEST_UNIT_EXPONENT;
                double scaled = MathUtils.scaleUpByPowerOf10(targetPriceAsDouble, precision);
                final long roundedToLong = MathUtils.roundDoubleToLong(scaled);
                return Price.valueOf(currencyCode, roundedToLong);
            } 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 Price.valueOf(currencyCode, offerPayload.getPrice());
    }
}
Also used : MarketPrice(bisq.core.provider.price.MarketPrice) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException) Nullable(javax.annotation.Nullable)

Aggregations

MarketPrice (bisq.core.provider.price.MarketPrice)7 PriceFeedService (bisq.core.provider.price.PriceFeedService)3 BSFormatter (bisq.desktop.util.BSFormatter)3 OfferPayload (bisq.core.offer.OfferPayload)2 Popup (bisq.desktop.main.overlays.popups.Popup)2 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)2 AddressEntry (bisq.core.btc.AddressEntry)1 BsqWalletService (bisq.core.btc.wallet.BsqWalletService)1 BtcWalletService (bisq.core.btc.wallet.BtcWalletService)1 TradePriceOutOfToleranceException (bisq.core.exceptions.TradePriceOutOfToleranceException)1 CryptoCurrency (bisq.core.locale.CryptoCurrency)1 CurrencyUtil (bisq.core.locale.CurrencyUtil)1 Altcoin (bisq.core.monetary.Altcoin)1 Price (bisq.core.monetary.Price)1 Offer (bisq.core.offer.Offer)1 OpenOfferManager (bisq.core.offer.OpenOfferManager)1 PaymentAccount (bisq.core.payment.PaymentAccount)1 FeeService (bisq.core.provider.fee.FeeService)1 User (bisq.core.user.User)1 InputValidator (bisq.core.util.validation.InputValidator)1