Skip to content

Commit

Permalink
refactor opening price calc
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahriar-0 committed Jun 1, 2024
1 parent ef629ef commit c733dba
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public Matcher(ContinuousMatchingControl continuousMatchingControl, AuctionMatch
}

private boolean hasOrderForAuction(OrderBook orderBook) {
return orderBook.hasOrderOfType(Side.BUY) &&
orderBook.hasOrderOfType(Side.SELL);
return orderBook.hasOrderOfType(Side.BUY) && orderBook.hasOrderOfType(Side.SELL);
}

public int calcOpeningAuctionPrice(OrderBook orderBook, int lastTradePrice) {
if(!hasOrderForAuction(orderBook))
if(!hasOrderForAuction(orderBook)) {
return lastTradePrice;
}

int maxTradableQuantity = 0;
int openingPrice = lastTradePrice;
Expand All @@ -37,18 +37,21 @@ public int calcOpeningAuctionPrice(OrderBook orderBook, int lastTradePrice) {

for (int price = minPrice; price <= maxPrice; price++) {
int currentTradableQuantity = calcTradableQuantity(orderBook, price);
if (currentTradableQuantity > maxTradableQuantity) {
if (shouldUpdateOpeningAuctionPrice(openingPrice, price, lastTradePrice, maxTradableQuantity, currentTradableQuantity)) {
openingPrice = price;
maxTradableQuantity = currentTradableQuantity;
}
else if (currentTradableQuantity == maxTradableQuantity && Math.abs(price - lastTradePrice) < Math.abs(openingPrice - lastTradePrice)) {
openingPrice = price;
}
}

return openingPrice;
}

private boolean shouldUpdateOpeningAuctionPrice(int openingPrice, int newOpeningPrice, int lastTradePrice, int openingTradableQuantity, int newOpeningTradableQuantity) {
return newOpeningTradableQuantity > openingTradableQuantity ||
(newOpeningTradableQuantity == openingTradableQuantity &&
Math.abs(newOpeningPrice - lastTradePrice) < Math.abs(openingPrice - lastTradePrice));
}

public int calcTradableQuantity(OrderBook orderBook, int openingPrice) {
int buysQuantity = calcTradableQuantityInQueue(orderBook.getBuyQueue(), openingPrice);
int sellsQuantity = calcTradableQuantityInQueue(orderBook.getSellQueue(), openingPrice);;
Expand Down

0 comments on commit c733dba

Please sign in to comment.