Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secondary sort order for offer book (rewrite) #4259

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,32 @@ private boolean isAnyPricePresent() {
}

private void updateChartData() {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer no blank lines at the beginning of methods.

// 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();

dmos62 marked this conversation as resolved.
Show resolved Hide resolved
var buyOfferSortComparator =
offerPriceComparator.reversed() // Buy offers, as opposed to sell offers, are primarily sorted from high price to low.
dmos62 marked this conversation as resolved.
Show resolved Hide resolved
.thenComparing(offerAmountComparator);
var sellOfferSortComparator =
offerPriceComparator
dmos62 marked this conversation as resolved.
Show resolved Hide resolved
.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