use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.
the class OfferBookChartViewModel method updateChartData.
private void updateChartData() {
List<Offer> allBuyOffers = offerBookListItems.stream().map(OfferBookListItem::getOffer).filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && 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 (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode()))
return a > b ? 1 : -1;
else
return a < b ? 1 : -1;
} else {
return 0;
}
}).collect(Collectors.toList());
allBuyOffers = filterOffersWithRelevantPrices(allBuyOffers);
final Optional<Offer> highestBuyPriceOffer = allBuyOffers.stream().filter(o -> o.getPrice() != null).max(Comparator.comparingLong(o -> o.getPrice().getValue()));
if (highestBuyPriceOffer.isPresent()) {
final Offer offer = highestBuyPriceOffer.get();
maxPlacesForBuyPrice.set(formatPrice(offer, false).length());
}
final Optional<Offer> highestBuyVolumeOffer = allBuyOffers.stream().filter(o -> o.getVolume() != null).max(Comparator.comparingLong(o -> o.getVolume().getValue()));
if (highestBuyVolumeOffer.isPresent()) {
final Offer offer = highestBuyVolumeOffer.get();
maxPlacesForBuyVolume.set(formatVolume(offer, false).length());
}
buildChartAndTableEntries(allBuyOffers, OfferPayload.Direction.BUY, buyData, topBuyOfferList);
List<Offer> allSellOffers = offerBookListItems.stream().map(OfferBookListItem::getOffer).filter(e -> e.getCurrencyCode().equals(selectedTradeCurrencyProperty.get().getCode()) && 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 (CurrencyUtil.isCryptoCurrency(o1.getCurrencyCode()))
return a < b ? 1 : -1;
else
return a > b ? 1 : -1;
} else {
return 0;
}
}).collect(Collectors.toList());
allSellOffers = filterOffersWithRelevantPrices(allSellOffers);
final Optional<Offer> highestSellPriceOffer = allSellOffers.stream().filter(o -> o.getPrice() != null).max(Comparator.comparingLong(o -> o.getPrice().getValue()));
if (highestSellPriceOffer.isPresent()) {
final Offer offer = highestSellPriceOffer.get();
maxPlacesForSellPrice.set(formatPrice(offer, false).length());
}
final Optional<Offer> highestSellVolumeOffer = allSellOffers.stream().filter(o -> o.getVolume() != null).max(Comparator.comparingLong(o -> o.getVolume().getValue()));
if (highestSellVolumeOffer.isPresent()) {
final Offer offer = highestSellVolumeOffer.get();
maxPlacesForSellVolume.set(formatVolume(offer, false).length());
}
buildChartAndTableEntries(allSellOffers, OfferPayload.Direction.SELL, sellData, topSellOfferList);
}
use of bisq.core.offer.Offer 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.offer.Offer 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());
}
use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.
the class OfferBookView method getAvatarColumn.
private TableColumn<OfferBookListItem, OfferBookListItem> getAvatarColumn() {
TableColumn<OfferBookListItem, OfferBookListItem> column = new AutoTooltipTableColumn<OfferBookListItem, OfferBookListItem>(Res.get("offerbook.trader")) {
{
setMinWidth(80);
setMaxWidth(80);
setSortable(true);
}
};
column.setCellValueFactory((offer) -> new ReadOnlyObjectWrapper<>(offer.getValue()));
column.setCellFactory(new Callback<TableColumn<OfferBookListItem, OfferBookListItem>, TableCell<OfferBookListItem, OfferBookListItem>>() {
@Override
public TableCell<OfferBookListItem, OfferBookListItem> call(TableColumn<OfferBookListItem, OfferBookListItem> column) {
return new TableCell<OfferBookListItem, OfferBookListItem>() {
@Override
public void updateItem(final OfferBookListItem newItem, boolean empty) {
super.updateItem(newItem, empty);
if (newItem != null && !empty) {
final Offer offer = newItem.getOffer();
final NodeAddress makersNodeAddress = offer.getOwnerNodeAddress();
String role = Res.get("peerInfoIcon.tooltip.maker");
int numTrades = model.getNumTrades(offer);
PeerInfoIcon peerInfoIcon = new PeerInfoIcon(makersNodeAddress, role, numTrades, privateNotificationManager, offer, model.preferences, model.accountAgeWitnessService, formatter, useDevPrivilegeKeys);
setGraphic(peerInfoIcon);
} else {
setGraphic(null);
}
}
};
}
});
return column;
}
use of bisq.core.offer.Offer in project bisq-desktop by bisq-network.
the class OfferBookViewModel method applyFilterPredicate.
// /////////////////////////////////////////////////////////////////////////////////////////
// Filters
// /////////////////////////////////////////////////////////////////////////////////////////
private void applyFilterPredicate() {
filteredItems.setPredicate(offerBookListItem -> {
Offer offer = offerBookListItem.getOffer();
boolean directionResult = offer.getDirection() != direction;
boolean currencyResult = (showAllTradeCurrenciesProperty.get()) || offer.getCurrencyCode().equals(selectedTradeCurrency.getCode());
boolean paymentMethodResult = showAllPaymentMethods || offer.getPaymentMethod().equals(selectedPaymentMethod);
boolean notMyOfferOrShowMyOffersActivated = !isMyOffer(offerBookListItem.getOffer()) || preferences.isShowOwnOffersInOfferBook();
return directionResult && currencyResult && paymentMethodResult && notMyOfferOrShowMyOffersActivated;
});
}
Aggregations