Search in sources :

Example 6 with OfferBookListItem

use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.

the class SpreadViewModelTest method testMaxCharactersForAmount.

@Test
public void testMaxCharactersForAmount() {
    OfferBook offerBook = mock(OfferBook.class);
    final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
    offerBookListItems.addAll(make(OfferBookListItemMaker.btcItem));
    when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
    final SpreadViewModel model = new SpreadViewModel(offerBook, null, new BSFormatter());
    model.activate();
    // 0.001
    assertEquals(6, model.maxPlacesForAmount.intValue());
    offerBookListItems.addAll(make(btcItem.but(with(OfferBookListItemMaker.amount, 1403000000L))));
    // 14.0300
    assertEquals(7, model.maxPlacesForAmount.intValue());
}
Also used : OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) BSFormatter(bisq.desktop.util.BSFormatter) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with OfferBookListItem

use of bisq.desktop.main.offer.offerbook.OfferBookListItem 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);
}
Also used : GUIUtil(bisq.desktop.util.GUIUtil) TradeCurrency(bisq.core.locale.TradeCurrency) CurrencyListItem(bisq.desktop.util.CurrencyListItem) CurrencyList(bisq.desktop.util.CurrencyList) Inject(com.google.inject.Inject) FXCollections(javafx.collections.FXCollections) XYChart(javafx.scene.chart.XYChart) IntegerProperty(javafx.beans.property.IntegerProperty) BSFormatter(bisq.desktop.util.BSFormatter) ArrayList(java.util.ArrayList) OfferPayload(bisq.core.offer.OfferPayload) ListChangeListener(javafx.collections.ListChangeListener) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) CurrencyUtil(bisq.core.locale.CurrencyUtil) Navigation(bisq.desktop.Navigation) Offer(bisq.core.offer.Offer) ObjectProperty(javafx.beans.property.ObjectProperty) OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) GlobalSettings(bisq.core.locale.GlobalSettings) LongMath(com.google.common.math.LongMath) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) Collectors(java.util.stream.Collectors) ActivatableViewModel(bisq.desktop.common.model.ActivatableViewModel) MainView(bisq.desktop.main.MainView) List(java.util.List) PriceFeedService(bisq.core.provider.price.PriceFeedService) PreferencesView(bisq.desktop.main.settings.preferences.PreferencesView) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Preferences(bisq.core.user.Preferences) Price(bisq.core.monetary.Price) ObservableValue(javafx.beans.value.ObservableValue) Optional(java.util.Optional) ObservableList(javafx.collections.ObservableList) ChangeListener(javafx.beans.value.ChangeListener) Comparator(java.util.Comparator) SettingsView(bisq.desktop.main.settings.SettingsView) Offer(bisq.core.offer.Offer)

Example 8 with OfferBookListItem

use of bisq.desktop.main.offer.offerbook.OfferBookListItem 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());
}
Also used : Altcoin(bisq.core.monetary.Altcoin) GUIUtil(bisq.desktop.util.GUIUtil) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) IntegerProperty(javafx.beans.property.IntegerProperty) BSFormatter(bisq.desktop.util.BSFormatter) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) OfferPayload(bisq.core.offer.OfferPayload) ListChangeListener(javafx.collections.ListChangeListener) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Map(java.util.Map) CurrencyUtil(bisq.core.locale.CurrencyUtil) RoundingMode(java.math.RoundingMode) Popup(bisq.desktop.main.overlays.popups.Popup) Offer(bisq.core.offer.Offer) OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) Fiat(org.bitcoinj.utils.Fiat) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) Collectors(java.util.stream.Collectors) ActivatableViewModel(bisq.desktop.common.model.ActivatableViewModel) List(java.util.List) PriceFeedService(bisq.core.provider.price.PriceFeedService) Price(bisq.core.monetary.Price) ObservableList(javafx.collections.ObservableList) MarketPrice(bisq.core.provider.price.MarketPrice) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Coin(org.bitcoinj.core.Coin) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) MarketPrice(bisq.core.provider.price.MarketPrice) Offer(bisq.core.offer.Offer) Price(bisq.core.monetary.Price) MarketPrice(bisq.core.provider.price.MarketPrice) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList)

Example 9 with OfferBookListItem

use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.

the class OfferBookChartViewModelTest method testMaxCharactersForBuyVolumeWithNoOffers.

@Test
public void testMaxCharactersForBuyVolumeWithNoOffers() {
    OfferBook offerBook = mock(OfferBook.class);
    final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
    when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
    final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, null, null, new BSFormatter());
    assertEquals(0, model.maxPlacesForBuyVolume.intValue());
}
Also used : OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) BSFormatter(bisq.desktop.util.BSFormatter) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with OfferBookListItem

use of bisq.desktop.main.offer.offerbook.OfferBookListItem in project bisq-desktop by bisq-network.

the class OfferBookChartViewModelTest method testMaxCharactersForSellPriceWithOfflinePriceFeedService.

@Test
public void testMaxCharactersForSellPriceWithOfflinePriceFeedService() {
    OfferBook offerBook = mock(OfferBook.class);
    PriceFeedService priceFeedService = mock(PriceFeedService.class);
    final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
    final OfferBookListItem item = make(OfferBookListItemMaker.btcSellItem.but(with(OfferBookListItemMaker.useMarketBasedPrice, true)));
    item.getOffer().setPriceFeedService(priceFeedService);
    offerBookListItems.addAll(item);
    when(priceFeedService.getMarketPrice(anyString())).thenReturn(null);
    when(priceFeedService.updateCounterProperty()).thenReturn(new SimpleIntegerProperty());
    when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
    final OfferBookChartViewModel model = new OfferBookChartViewModel(offerBook, empty, priceFeedService, null, new BSFormatter());
    model.activate();
    assertEquals(0, model.maxPlacesForSellPrice.intValue());
}
Also used : OfferBook(bisq.desktop.main.offer.offerbook.OfferBook) OfferBookListItem(bisq.desktop.main.offer.offerbook.OfferBookListItem) PriceFeedService(bisq.core.provider.price.PriceFeedService) BSFormatter(bisq.desktop.util.BSFormatter) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

OfferBook (bisq.desktop.main.offer.offerbook.OfferBook)14 OfferBookListItem (bisq.desktop.main.offer.offerbook.OfferBookListItem)14 BSFormatter (bisq.desktop.util.BSFormatter)14 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 PriceFeedService (bisq.core.provider.price.PriceFeedService)8 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)4 CurrencyUtil (bisq.core.locale.CurrencyUtil)2 Price (bisq.core.monetary.Price)2 Offer (bisq.core.offer.Offer)2 OfferPayload (bisq.core.offer.OfferPayload)2 ActivatableViewModel (bisq.desktop.common.model.ActivatableViewModel)2 GUIUtil (bisq.desktop.util.GUIUtil)2 Inject (com.google.inject.Inject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IntegerProperty (javafx.beans.property.IntegerProperty)2 FXCollections (javafx.collections.FXCollections)2 ListChangeListener (javafx.collections.ListChangeListener)2