Skip to content

Commit

Permalink
Merge pull request #4259 from dmos62/offerbook-secondary-sort-order-r…
Browse files Browse the repository at this point in the history
…efactor

Secondary sort order for offer book (rewrite)
  • Loading branch information
sqrrm authored May 25, 2020
2 parents 07fc648 + 3dc13b6 commit 6665713
Showing 1 changed file with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,32 @@ private boolean isAnyPricePresent() {
}

private void updateChartData() {

// Offer price can be null (if price feed unavailable), thus a null-tolerant comparator is used.
Comparator<Offer> offerPriceComparator = Comparator.comparing(Offer::getPrice, Comparator.nullsLast(Comparator.naturalOrder()));

// Trading btc-fiat is considered as buying/selling BTC, but trading btc-altcoin is
// considered as buying/selling Altcoin. Because of this, when viewing a btc-altcoin pair,
// the buy column is actually the sell column and vice versa. To maintain the expected
// ordering, we have to reverse the price comparator.
boolean isCrypto = CurrencyUtil.isCryptoCurrency(getCurrencyCode());
if (isCrypto) offerPriceComparator = offerPriceComparator.reversed();

// Offer amounts are used for the secondary sort. They are sorted from high to low.
Comparator<Offer> offerAmountComparator = Comparator.comparing(Offer::getAmount).reversed();

var buyOfferSortComparator =
offerPriceComparator.reversed() // Buy offers, as opposed to sell offers, are primarily sorted from high price to low.
.thenComparing(offerAmountComparator);
var sellOfferSortComparator =
offerPriceComparator
.thenComparing(offerAmountComparator);

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;
}
})
.sorted(buyOfferSortComparator)
.collect(Collectors.toList());

final Optional<Offer> highestBuyPriceOffer = allBuyOffers.stream()
Expand Down Expand Up @@ -320,18 +330,7 @@ private void updateChartData() {
.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;
}
})
.sorted(sellOfferSortComparator)
.collect(Collectors.toList());

final Optional<Offer> highestSellPriceOffer = allSellOffers.stream()
Expand Down

0 comments on commit 6665713

Please sign in to comment.