Skip to content

Commit

Permalink
Fix MarketOrderBook avgPriceAndMatchedVolumeBuy
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 10, 2024
1 parent 571e014 commit b478810
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/objects/include/marketorderbook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class MarketOrderBook {

AmountAtPrice avgPriceAndMatchedVolumeSell(MonetaryAmount baseAmount, MonetaryAmount price) const;

AmountAtPrice avgPriceAndMatchedVolumeBuy(MonetaryAmount quoteAmount, MonetaryAmount price) const;
AmountAtPrice avgPriceAndMatchedVolumeBuy(MonetaryAmount amountInBaseOrQuote, MonetaryAmount price) const;

/// Attempt to convert given amount expressed in base currency to quote currency.
/// It may not be possible, in which case an empty optional will be returned.
Expand Down
43 changes: 28 additions & 15 deletions src/objects/src/marketorderbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ MarketOrderBook::AmountPerPriceVec MarketOrderBook::computePricesAtWhichAmountWo
MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolumeSell(MonetaryAmount baseAmount,
MonetaryAmount price) const {
MonetaryAmount avgPrice(0, _market.quote());

MonetaryAmount remainingBaseAmount = baseAmount;
for (int pos = _lowestAskPricePos - 1; pos >= 0; --pos) {
const MonetaryAmount linePrice = priceAt(pos);
Expand All @@ -283,13 +284,17 @@ MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolumeSell(Mon
}
}
MonetaryAmount matchedAmount = baseAmount - remainingBaseAmount;
return {matchedAmount, avgPrice / matchedAmount.toNeutral()};
if (matchedAmount != 0) {
avgPrice /= matchedAmount.toNeutral();
}
return {matchedAmount, avgPrice};
}

MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolumeBuy(MonetaryAmount quoteAmount,
MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolumeBuy(MonetaryAmount amountInBaseOrQuote,
MonetaryAmount price) const {
MonetaryAmount remainingQuoteAmount = quoteAmount;
MonetaryAmount matchedAmount;
MonetaryAmount remainingAmountInBaseOrQuote = amountInBaseOrQuote;
MonetaryAmount matchedAmount(0, _market.base());
MonetaryAmount avgPrice(0, _market.quote());
const int nbOrders = _orders.size();
for (int pos = _highestBidPricePos + 1; pos < nbOrders; ++pos) {
const MonetaryAmount linePrice = priceAt(pos);
Expand All @@ -299,24 +304,32 @@ MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolumeBuy(Mone
const MonetaryAmount lineAmount = negAmountAt(pos);
MonetaryAmount quoteAmountToEat = lineAmount.toNeutral() * linePrice;

if (quoteAmountToEat < remainingQuoteAmount) {
matchedAmount += lineAmount;
if (remainingAmountInBaseOrQuote.currencyCode() == _market.quote()) {
if (quoteAmountToEat < remainingAmountInBaseOrQuote) {
matchedAmount += lineAmount;
} else {
quoteAmountToEat = remainingAmountInBaseOrQuote;
matchedAmount += MonetaryAmount(remainingAmountInBaseOrQuote / linePrice, _market.base());
}
remainingAmountInBaseOrQuote -= quoteAmountToEat;
avgPrice += quoteAmountToEat;
} else {
quoteAmountToEat = remainingQuoteAmount;
matchedAmount += MonetaryAmount(remainingQuoteAmount / linePrice, _market.base());
}
// amountInBaseOrQuote is in base currency
const MonetaryAmount baseAmountToEat = std::min(lineAmount, remainingAmountInBaseOrQuote);
matchedAmount += baseAmountToEat;
remainingAmountInBaseOrQuote -= baseAmountToEat;

remainingQuoteAmount -= quoteAmountToEat;
avgPrice += baseAmountToEat.toNeutral() * linePrice;
}

if (remainingQuoteAmount == 0 || pos + 1 == nbOrders) {
if (remainingAmountInBaseOrQuote == 0 || pos + 1 == nbOrders) {
break;
}
}
price = quoteAmount - remainingQuoteAmount;
if (matchedAmount != 0) {
price /= matchedAmount.toNeutral();
avgPrice /= matchedAmount.toNeutral();
}
return {matchedAmount, price};
return {matchedAmount, avgPrice};
}

std::optional<MonetaryAmount> MarketOrderBook::computeMinPriceAtWhichAmountWouldBeSoldImmediately(
Expand Down Expand Up @@ -421,7 +434,7 @@ MarketOrderBook::AmountAtPrice MarketOrderBook::avgPriceAndMatchedVolume(TradeSi
MonetaryAmount price) const {
switch (tradeSide) {
case TradeSide::kBuy:
return avgPriceAndMatchedVolumeBuy(amount * price, price);
return avgPriceAndMatchedVolumeBuy(amount, price);
case TradeSide::kSell:
return avgPriceAndMatchedVolumeSell(amount, price);
default:
Expand Down
16 changes: 16 additions & 0 deletions src/objects/test/marketorderbook_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ TEST_F(MarketOrderBookTestCase3, Convert) {
MonetaryAmount("216266.409928471248", "XLM"));
}

TEST_F(MarketOrderBookTestCase3, AvgPriceAndMatchedVolume) {
EXPECT_EQ(marketOrderBook.avgPriceAndMatchedVolume(TradeSide::kBuy, MonetaryAmount(100000, "XLM"),
MonetaryAmount("0.000007121", "BTC")),
AmountAtPrice(MonetaryAmount(100000, "XLM"), MonetaryAmount("0.0000071176273715", "BTC")));
EXPECT_EQ(marketOrderBook.avgPriceAndMatchedVolume(TradeSide::kBuy, MonetaryAmount(100000, "XLM"),
MonetaryAmount("0.000007090", "BTC")),
AmountAtPrice(MonetaryAmount(0, "XLM"), MonetaryAmount(0, "BTC")));

EXPECT_EQ(marketOrderBook.avgPriceAndMatchedVolume(TradeSide::kSell, MonetaryAmount("4500000", "XLM"),
MonetaryAmount("0.000007079", "BTC")),
AmountAtPrice(MonetaryAmount("411248.27", "XLM"), MonetaryAmount("0.00000708595487037", "BTC")));
EXPECT_EQ(marketOrderBook.avgPriceAndMatchedVolume(TradeSide::kSell, MonetaryAmount("4500000", "XLM"),
MonetaryAmount("0.000007110", "BTC")),
AmountAtPrice(MonetaryAmount(0, "XLM"), MonetaryAmount(0, "BTC")));
}

class MarketOrderBookTestCaseExtended1 : public ::testing::Test {
protected:
TimePoint time{};
Expand Down

0 comments on commit b478810

Please sign in to comment.