use of bisq.core.monetary.Price in project bisq-core by bisq-network.
the class OfferForJson method setDisplayStrings.
private void setDisplayStrings() {
try {
final Price price = getPrice();
if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
primaryMarketDirection = direction == OfferPayload.Direction.BUY ? OfferPayload.Direction.SELL : OfferPayload.Direction.BUY;
currencyPair = currencyCode + "/" + Res.getBaseCurrencyCode();
// int precision = 8;
// decimalFormat.setMaximumFractionDigits(precision);
// amount and volume is inverted for json
priceDisplayString = altcoinFormat.noCode().format(price.getMonetary()).toString();
primaryMarketMinAmountDisplayString = altcoinFormat.noCode().format(getMinVolume().getMonetary()).toString();
primaryMarketAmountDisplayString = altcoinFormat.noCode().format(getVolume().getMonetary()).toString();
primaryMarketMinVolumeDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
primaryMarketVolumeDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
primaryMarketPrice = price.getValue();
primaryMarketMinAmount = getMinVolume().getValue();
primaryMarketAmount = getVolume().getValue();
primaryMarketMinVolume = getMinAmountAsCoin().getValue();
primaryMarketVolume = getAmountAsCoin().getValue();
} else {
primaryMarketDirection = direction;
currencyPair = Res.getBaseCurrencyCode() + "/" + currencyCode;
priceDisplayString = fiatFormat.noCode().format(price.getMonetary()).toString();
primaryMarketMinAmountDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
primaryMarketAmountDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
primaryMarketMinVolumeDisplayString = fiatFormat.noCode().format(getMinVolume().getMonetary()).toString();
primaryMarketVolumeDisplayString = fiatFormat.noCode().format(getVolume().getMonetary()).toString();
// we use precision 4 for fiat based price but on the markets api we use precision 8 so we scale up by 10000
primaryMarketPrice = (long) MathUtils.scaleUpByPowerOf10(price.getValue(), 4);
primaryMarketMinVolume = (long) MathUtils.scaleUpByPowerOf10(getMinVolume().getValue(), 4);
primaryMarketVolume = (long) MathUtils.scaleUpByPowerOf10(getVolume().getValue(), 4);
primaryMarketMinAmount = getMinAmountAsCoin().getValue();
primaryMarketAmount = getAmountAsCoin().getValue();
}
} catch (Throwable t) {
log.error("Error at setDisplayStrings: " + t.getMessage());
}
}
use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.
the class OfferBookView method initialize.
@Override
public void initialize() {
root.setPadding(new Insets(20, 25, 5, 25));
addTitledGroupBg(root, gridRow, 2, Res.get("offerbook.availableOffers"));
final Tuple3<HBox, AutoTooltipLabel, ComboBox> filterBoxTuple = addHBoxLabelComboBox(root, gridRow, Res.get("offerbook.filterByCurrency"), Layout.FIRST_ROW_DISTANCE);
final HBox filterBox = filterBoxTuple.first;
currencyComboBox = filterBoxTuple.third;
currencyComboBox.setPromptText(Res.get("list.currency.select"));
// noinspection unchecked
paymentMethodComboBox = new ComboBox<>();
final Label paymentMethodLabel = new AutoTooltipLabel(Res.getWithCol("offerbook.filterByPaymentMethod"));
paymentMethodLabel.setPadding(new Insets(0, 0, 0, 10));
filterBox.getChildren().addAll(paymentMethodLabel, paymentMethodComboBox);
paymentMethodComboBox.setPromptText(Res.get("shared.selectPaymentMethod"));
paymentMethodComboBox.setVisibleRowCount(20);
paymentMethodComboBox.setConverter(new StringConverter<PaymentMethod>() {
@Override
public String toString(PaymentMethod paymentMethod) {
String id = paymentMethod.getId();
if (id.equals(GUIUtil.SHOW_ALL_FLAG))
return "▶ " + Res.get("list.currency.showAll");
else if (paymentMethod.equals(PaymentMethod.BLOCK_CHAINS))
return "✦ " + Res.get(id);
else
return "★ " + Res.get(id);
}
@Override
public PaymentMethod fromString(String s) {
return null;
}
});
tableView = new TableView<>();
GridPane.setRowIndex(tableView, ++gridRow);
GridPane.setColumnIndex(tableView, 0);
GridPane.setColumnSpan(tableView, 2);
GridPane.setMargin(tableView, new Insets(10, -10, -10, -10));
GridPane.setVgrow(tableView, Priority.ALWAYS);
root.getChildren().add(tableView);
marketColumn = getMarketColumn();
priceColumn = getPriceColumn();
tableView.getColumns().add(priceColumn);
amountColumn = getAmountColumn();
tableView.getColumns().add(amountColumn);
volumeColumn = getVolumeColumn();
tableView.getColumns().add(volumeColumn);
TableColumn<OfferBookListItem, OfferBookListItem> paymentMethodColumn = getPaymentMethodColumn();
tableView.getColumns().add(paymentMethodColumn);
TableColumn<OfferBookListItem, OfferBookListItem> avatarColumn = getAvatarColumn();
tableView.getColumns().add(avatarColumn);
tableView.getColumns().add(getActionColumn());
tableView.getSortOrder().add(priceColumn);
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
Label placeholder = new AutoTooltipLabel(Res.get("table.placeholder.noItems", Res.get("shared.multipleOffers")));
placeholder.setWrapText(true);
tableView.setPlaceholder(placeholder);
marketColumn.setComparator((o1, o2) -> {
String str1 = formatter.getCurrencyPair(o1.getOffer().getCurrencyCode());
String str2 = formatter.getCurrencyPair(o2.getOffer().getCurrencyCode());
return str1 != null && str2 != null ? str1.compareTo(str2) : 0;
});
priceColumn.setComparator((o1, o2) -> {
Price price1 = o1.getOffer().getPrice();
Price price2 = o2.getOffer().getPrice();
return price1 != null && price2 != null ? price1.compareTo(price2) : 0;
});
amountColumn.setComparator(Comparator.comparing(o -> o.getOffer().getAmount()));
volumeColumn.setComparator((o1, o2) -> {
Volume offerVolume1 = o1.getOffer().getVolume();
Volume offerVolume2 = o2.getOffer().getVolume();
return offerVolume1 != null && offerVolume2 != null ? offerVolume1.compareTo(offerVolume2) : 0;
});
paymentMethodColumn.setComparator(Comparator.comparing(o -> o.getOffer().getPaymentMethod()));
avatarColumn.setComparator(Comparator.comparing(o -> o.getOffer().getOwnerNodeAddress().getFullAddress()));
nrOfOffersLabel = new AutoTooltipLabel("");
nrOfOffersLabel.setId("num-offers");
GridPane.setHalignment(nrOfOffersLabel, HPos.LEFT);
GridPane.setVgrow(nrOfOffersLabel, Priority.NEVER);
GridPane.setValignment(nrOfOffersLabel, VPos.TOP);
GridPane.setRowIndex(nrOfOffersLabel, ++gridRow);
GridPane.setColumnIndex(nrOfOffersLabel, 0);
GridPane.setMargin(nrOfOffersLabel, new Insets(10, 0, 0, -5));
root.getChildren().add(nrOfOffersLabel);
createOfferButton = addButton(root, gridRow, "");
createOfferButton.setMinHeight(40);
createOfferButton.setPadding(new Insets(0, 20, 0, 20));
createOfferButton.setGraphicTextGap(10);
GridPane.setMargin(createOfferButton, new Insets(15, 0, 0, 0));
GridPane.setHalignment(createOfferButton, HPos.RIGHT);
GridPane.setVgrow(createOfferButton, Priority.NEVER);
GridPane.setValignment(createOfferButton, VPos.TOP);
offerListListener = c -> nrOfOffersLabel.setText(Res.get("offerbook.nrOffers", model.getOfferList().size()));
// Fixes incorrect ordering of Available offers:
// https://github.com/bisq-network/exchange/issues/588
priceFeedUpdateCounterListener = (observable, oldValue, newValue) -> tableView.sort();
}
use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.
the class OfferBookViewModel method getPrice.
String getPrice(OfferBookListItem item) {
if ((item == null))
return "";
final Offer offer = item.getOffer();
final Price price = offer.getPrice();
if (price != null) {
return formatPrice(offer, true) + formatMarketPriceMargin(offer, true);
} else {
return Res.get("shared.na");
}
}
use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.
the class OfferBookChartViewModel method buildChartAndTableEntries.
private void buildChartAndTableEntries(List<Offer> sortedList, OfferPayload.Direction direction, List<XYChart.Data> data, ObservableList<OfferListItem> offerTableList) {
data.clear();
double accumulatedAmount = 0;
List<OfferListItem> offerTableListTemp = new ArrayList<>();
for (Offer offer : sortedList) {
Price price = offer.getPrice();
if (price != null) {
double amount = (double) offer.getAmount().value / LongMath.pow(10, offer.getAmount().smallestUnitExponent());
accumulatedAmount += amount;
offerTableListTemp.add(new OfferListItem(offer, accumulatedAmount));
double priceAsDouble = (double) price.getValue() / LongMath.pow(10, price.smallestUnitExponent());
if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
if (direction.equals(OfferPayload.Direction.SELL))
data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
else
data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
} else {
if (direction.equals(OfferPayload.Direction.BUY))
data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
else
data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
}
}
}
offerTableList.setAll(offerTableListTemp);
}
use of bisq.core.monetary.Price in project bisq-desktop by bisq-network.
the class SpreadViewModel method update.
private void update(ObservableList<OfferBookListItem> offerBookListItems) {
Map<String, List<Offer>> offersByCurrencyMap = new HashMap<>();
for (OfferBookListItem offerBookListItem : offerBookListItems) {
Offer offer = offerBookListItem.getOffer();
String currencyCode = offer.getCurrencyCode();
if (!offersByCurrencyMap.containsKey(currencyCode))
offersByCurrencyMap.put(currencyCode, new ArrayList<>());
offersByCurrencyMap.get(currencyCode).add(offer);
}
spreadItems.clear();
Coin totalAmount = null;
for (String currencyCode : offersByCurrencyMap.keySet()) {
List<Offer> offers = offersByCurrencyMap.get(currencyCode);
final boolean isFiatCurrency = CurrencyUtil.isFiatCurrency(currencyCode);
List<Offer> buyOffers = offers.stream().filter(e -> e.getDirection().equals(OfferPayload.Direction.BUY)).sorted((o1, o2) -> {
long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0;
long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0;
if (a != b) {
if (isFiatCurrency) {
return a < b ? 1 : -1;
} else {
return a < b ? -1 : 1;
}
}
return 0;
}).collect(Collectors.toList());
List<Offer> sellOffers = offers.stream().filter(e -> e.getDirection().equals(OfferPayload.Direction.SELL)).sorted((o1, o2) -> {
long a = o1.getPrice() != null ? o1.getPrice().getValue() : 0;
long b = o2.getPrice() != null ? o2.getPrice().getValue() : 0;
if (a != b) {
if (isFiatCurrency) {
return a > b ? 1 : -1;
} else {
return a > b ? -1 : 1;
}
}
return 0;
}).collect(Collectors.toList());
Price spread = null;
String percentage = "";
Price bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice();
Price bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice();
if (bestBuyOfferPrice != null && bestSellOfferPrice != null) {
MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
// happens again
try {
if (isFiatCurrency)
spread = bestSellOfferPrice.subtract(bestBuyOfferPrice);
else
spread = bestBuyOfferPrice.subtract(bestSellOfferPrice);
if (spread != null && marketPrice != null && marketPrice.isPriceAvailable()) {
double marketPriceAsDouble = marketPrice.getPrice();
final double precision = isFiatCurrency ? Math.pow(10, Fiat.SMALLEST_UNIT_EXPONENT) : Math.pow(10, Altcoin.SMALLEST_UNIT_EXPONENT);
BigDecimal marketPriceAsBigDecimal = BigDecimal.valueOf(marketPriceAsDouble).multiply(BigDecimal.valueOf(precision));
// We multiply with 10000 because we use precision of 2 at % (100.00%)
double result = BigDecimal.valueOf(spread.getValue()).multiply(BigDecimal.valueOf(10000)).divide(marketPriceAsBigDecimal, RoundingMode.HALF_UP).doubleValue() / 10000;
percentage = formatter.formatPercentagePrice(result);
}
} catch (Throwable t) {
try {
// Don't translate msg. It is just for rare error cases and can be removed probably later if
// that error never gets reported again.
String msg = "An error occurred at the spread calculation.\n" + "Error msg: " + t.toString() + "\n" + "Details of offer data: \n" + "bestSellOfferPrice: " + bestSellOfferPrice.getValue() + "\n" + "bestBuyOfferPrice: " + bestBuyOfferPrice.getValue() + "\n" + "sellOffer getCurrencyCode: " + sellOffers.get(0).getCurrencyCode() + "\n" + "buyOffer getCurrencyCode: " + buyOffers.get(0).getCurrencyCode() + "\n\n" + "Please copy and paste this data and send it to the developers so they can investigate the issue.";
new Popup<>().error(msg).show();
log.error(t.toString());
t.printStackTrace();
} catch (Throwable t2) {
log.error(t2.toString());
t2.printStackTrace();
}
}
}
totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(), offers.size(), spread, percentage, totalAmount));
}
maxPlacesForAmount.set(formatAmount(totalAmount, false).length());
}
Aggregations