Search in sources :

Example 1 with OfferBookListItem

use of io.bitsquare.gui.main.offer.offerbook.OfferBookListItem in project bitsquare by bitsquare.

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();
    for (String currencyCode : offersByCurrencyMap.keySet()) {
        List<Offer> offers = offersByCurrencyMap.get(currencyCode);
        List<Offer> buyOffers = offers.stream().filter(e -> e.getDirection().equals(Offer.Direction.BUY)).sorted((o1, o2) -> {
            long a = o1.getPrice() != null ? o1.getPrice().value : 0;
            long b = o2.getPrice() != null ? o2.getPrice().value : 0;
            if (a != b)
                return a < b ? 1 : -1;
            return 0;
        }).collect(Collectors.toList());
        List<Offer> sellOffers = offers.stream().filter(e -> e.getDirection().equals(Offer.Direction.SELL)).sorted((o1, o2) -> {
            long a = o1.getPrice() != null ? o1.getPrice().value : 0;
            long b = o2.getPrice() != null ? o2.getPrice().value : 0;
            if (a != b)
                return a > b ? 1 : -1;
            return 0;
        }).collect(Collectors.toList());
        Fiat spread = null;
        String percentage = "";
        Fiat bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice();
        Fiat bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice();
        if (bestBuyOfferPrice != null && bestSellOfferPrice != null) {
            MarketPrice marketPrice = priceFeedService.getMarketPrice(currencyCode);
            // happens again
            try {
                spread = bestSellOfferPrice.subtract(bestBuyOfferPrice);
                if (spread != null && marketPrice != null) {
                    double marketPriceAsDouble = marketPrice.getPrice(PriceFeedService.Type.LAST);
                    if (CurrencyUtil.isFiatCurrency(currencyCode)) {
                        double result = ((double) spread.value / 10000D) / marketPriceAsDouble;
                        percentage = " (" + formatter.formatPercentagePrice(result) + ")";
                    } else {
                        final double spreadAsDouble = spread.value != 0 ? 10000D / spread.value : 0;
                        double result = marketPriceAsDouble / spreadAsDouble;
                        percentage = " (" + formatter.formatPercentagePrice(result) + ")";
                    }
                }
            } catch (Throwable t) {
                try {
                    String msg = "An error occurred at the spread calculation.\n" + "Error msg: " + t.toString() + "\n" + "Details of offer data: \n" + "bestSellOfferPrice: " + bestSellOfferPrice.value + "\n" + "bestBuyOfferPrice: " + bestBuyOfferPrice.value + "\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();
                }
            }
        }
        Coin 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));
    }
}
Also used : PriceFeedService(io.bitsquare.btc.pricefeed.PriceFeedService) Popup(io.bitsquare.gui.main.overlays.popups.Popup) MarketPrice(io.bitsquare.btc.pricefeed.MarketPrice) ActivatableViewModel(io.bitsquare.gui.common.model.ActivatableViewModel) Coin(org.bitcoinj.core.Coin) Inject(com.google.inject.Inject) FXCollections(javafx.collections.FXCollections) Fiat(org.bitcoinj.utils.Fiat) HashMap(java.util.HashMap) OfferBookListItem(io.bitsquare.gui.main.offer.offerbook.OfferBookListItem) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Offer(io.bitsquare.trade.offer.Offer) List(java.util.List) OfferBook(io.bitsquare.gui.main.offer.offerbook.OfferBook) ListChangeListener(javafx.collections.ListChangeListener) Map(java.util.Map) ObservableList(javafx.collections.ObservableList) CurrencyUtil(io.bitsquare.locale.CurrencyUtil) BSFormatter(io.bitsquare.gui.util.BSFormatter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Coin(org.bitcoinj.core.Coin) OfferBookListItem(io.bitsquare.gui.main.offer.offerbook.OfferBookListItem) MarketPrice(io.bitsquare.btc.pricefeed.MarketPrice) Offer(io.bitsquare.trade.offer.Offer) Fiat(org.bitcoinj.utils.Fiat) Popup(io.bitsquare.gui.main.overlays.popups.Popup) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList)

Aggregations

Inject (com.google.inject.Inject)1 MarketPrice (io.bitsquare.btc.pricefeed.MarketPrice)1 PriceFeedService (io.bitsquare.btc.pricefeed.PriceFeedService)1 ActivatableViewModel (io.bitsquare.gui.common.model.ActivatableViewModel)1 OfferBook (io.bitsquare.gui.main.offer.offerbook.OfferBook)1 OfferBookListItem (io.bitsquare.gui.main.offer.offerbook.OfferBookListItem)1 Popup (io.bitsquare.gui.main.overlays.popups.Popup)1 BSFormatter (io.bitsquare.gui.util.BSFormatter)1 CurrencyUtil (io.bitsquare.locale.CurrencyUtil)1 Offer (io.bitsquare.trade.offer.Offer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 FXCollections (javafx.collections.FXCollections)1 ListChangeListener (javafx.collections.ListChangeListener)1 ObservableList (javafx.collections.ObservableList)1 Coin (org.bitcoinj.core.Coin)1 Fiat (org.bitcoinj.utils.Fiat)1