Skip to content

Commit

Permalink
Adam/add candles hloc (#2047)
Browse files Browse the repository at this point in the history
(cherry picked from commit 73b04dc)

# Conflicts:
#	indexer/packages/postgres/src/db/migrations/migration_files/20240627152937_candles_add_mid_book_price_open_and_close.ts
#	indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts
#	indexer/packages/redis/src/caches/orderbook-levels-cache.ts
#	indexer/services/ender/src/lib/candles-generator.ts
  • Loading branch information
adamfraser authored and mergify[bot] committed Aug 21, 2024
1 parent 38528b2 commit 5c3d156
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('candles', (table) => {
<<<<<<< HEAD
table.decimal('orderbookMidPriceOpen', null);
table.decimal('orderbookMidPriceClose', null);
=======
table.decimal('orderbookMidPriceOpen', null).nullable();
table.decimal('orderbookMidPriceClose', null).nullable();
>>>>>>> 73b04dc3 (Adam/add candles hloc (#2047))
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,58 @@ describe('orderbookLevelsCache', () => {
]);

const midPrice = await getOrderBookMidPrice(ticker, client);
<<<<<<< HEAD
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');
>>>>>>> 73b04dc3 (Adam/add candles hloc (#2047))
});

it('returns undefined if there are no bids or asks', async () => {
Expand Down
17 changes: 17 additions & 0 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,11 @@ function convertToPriceLevels(
export async function getOrderBookMidPrice(
ticker: string,
client: RedisClient,
<<<<<<< HEAD
): Promise<number | undefined> {
=======
): Promise<string | undefined> {
>>>>>>> 73b04dc3 (Adam/add candles hloc (#2047))
const levels = await getOrderBookLevels(ticker, client, {
removeZeros: true,
sortSides: true,
Expand All @@ -542,6 +546,7 @@ export async function getOrderBookMidPrice(
});

if (levels.bids.length === 0 || levels.asks.length === 0) {
<<<<<<< HEAD
const message: string = `Orderbook bid length: ${levels.bids.length}, ask length: ${levels.asks.length}. Expected > 0`;
logger.error({
at: 'orderbook-levels-cache#getOrderBookMidPrice',
Expand All @@ -563,4 +568,16 @@ export async function getOrderBookMidPrice(
}

return bestBid + (bestAsk - bestBid) / 2;
=======
return undefined;
}

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

if (bestAsk === undefined || bestBid === undefined) {
return undefined;
}
return bestBid.plus(bestAsk).div(2).toFixed();
>>>>>>> 73b04dc3 (Adam/add candles hloc (#2047))
}
4 changes: 4 additions & 0 deletions indexer/services/ender/src/lib/candles-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ export async function getOrderbookMidPriceMap(): Promise<{ [ticker: string]: Ord
perpetualMarket.ticker,
redisClient,
);
<<<<<<< HEAD
return { [perpetualMarket.ticker]: price === undefined ? undefined : String(price) };
=======
return { [perpetualMarket.ticker]: price === undefined ? undefined : price };
>>>>>>> 73b04dc3 (Adam/add candles hloc (#2047))
});

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

0 comments on commit 5c3d156

Please sign in to comment.