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

Adam/ct 1059 address issue with orderbookmidprice calculation #2012

Merged
Merged
Show file tree
Hide file tree
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 @@ -744,9 +744,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
Loading