Skip to content

Commit

Permalink
Adam/ct 1059 address issue with orderbookmidprice calculation (#2012)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9385c5a)
  • Loading branch information
adamfraser authored and mergify[bot] committed Aug 2, 2024
1 parent 6c4ae4e commit c6e4d92
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,52 @@ describe('orderbookLevelsCache', () => {
]);

const midPrice = await getOrderBookMidPrice(ticker, client);
expect(midPrice).toEqual(45350);
expect(midPrice).toEqual('45350');
});
});

it('returns the correct mid price for very small numbers', async () => {
await Promise.all([
updatePriceLevel({
ticker,
side: OrderSide.SELL,
humanPrice: '0.000000002346',
sizeDeltaInQuantums: '2000',
client,
}),
updatePriceLevel({
ticker,
side: OrderSide.BUY,
humanPrice: '0.000000002344',
sizeDeltaInQuantums: '2000',
client,
}),
]);

const midPrice = await getOrderBookMidPrice(ticker, client);
expect(midPrice).toEqual('0.000000002345');
});

it('returns the approprite amount of decimal precision', async () => {
await Promise.all([
updatePriceLevel({
ticker,
side: OrderSide.SELL,
humanPrice: '1.02',
sizeDeltaInQuantums: '2000',
client,
}),
updatePriceLevel({
ticker,
side: OrderSide.BUY,
humanPrice: '1.01',
sizeDeltaInQuantums: '2000',
client,
}),
]);

const midPrice = await getOrderBookMidPrice(ticker, client);
expect(midPrice).toEqual('1.015');
});

it('returns undefined if there are no bids or asks', async () => {
Expand Down
19 changes: 4 additions & 15 deletions indexer/packages/redis/src/caches/orderbook-levels-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ function convertToPriceLevels(
export async function getOrderBookMidPrice(
ticker: string,
client: RedisClient,
): Promise<number | undefined> {
): Promise<string | undefined> {
const levels = await getOrderBookLevels(ticker, client, {
removeZeros: true,
sortSides: true,
Expand All @@ -542,25 +542,14 @@ export async function getOrderBookMidPrice(
});

if (levels.bids.length === 0 || levels.asks.length === 0) {
const message: string = `Orderbook bid length: ${levels.bids.length}, ask length: ${levels.asks.length}. Expected > 0`;
logger.error({
at: 'orderbook-levels-cache#getOrderBookMidPrice',
message,
});
return undefined;
}

const bestAsk = Number(levels.asks[0].humanPrice);
const bestBid = Number(levels.bids[0].humanPrice);
const bestAsk = Big(levels.asks[0].humanPrice);
const bestBid = Big(levels.bids[0].humanPrice);

if (bestAsk === undefined || bestBid === undefined) {
const message: string = `Orderbook bid or ask failed to parse to Number, bid: ${levels.bids[0]}, ask: ${levels.asks[0]}`;
logger.error({
at: 'orderbook-levels-cache#getOrderBookMidPrice',
message,
});
return undefined;
}

return bestBid + (bestAsk - bestBid) / 2;
return bestBid.plus(bestAsk).div(2).toFixed();
}
2 changes: 1 addition & 1 deletion indexer/services/ender/src/lib/candles-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export async function getOrderbookMidPriceMap(): Promise<{ [ticker: string]: Ord
perpetualMarket.ticker,
redisClient,
);
return { [perpetualMarket.ticker]: price === undefined ? undefined : String(price) };
return { [perpetualMarket.ticker]: price === undefined ? undefined : price };
});

const pricesArray = await Promise.all(promises);
Expand Down

0 comments on commit c6e4d92

Please sign in to comment.