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);
}
});
}
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());
}
}
Aggregations