Skip to content

Commit

Permalink
Adam/add candles hloc (backport #2047) (#2134)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Fraser <[email protected]>
  • Loading branch information
mergify[bot] and adamfraser authored Aug 21, 2024
1 parent 38528b2 commit 82c72c5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('candles', (table) => {
table.decimal('orderbookMidPriceOpen', null);
table.decimal('orderbookMidPriceClose', null);
table.decimal('orderbookMidPriceOpen', null).nullable();
table.decimal('orderbookMidPriceClose', null).nullable();
});
}

Expand Down
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

0 comments on commit 82c72c5

Please sign in to comment.