From 666ff1658363d24358997a0901f7b5ac1aeb147e Mon Sep 17 00:00:00 2001 From: avraham Date: Sun, 11 Aug 2024 17:33:31 -0400 Subject: [PATCH] add `getAggregate` method --- server/src/calendardb/lmdbx.ts | 19 +++++++++++--- server/src/calendardb/optiondb-lmdbx.ts | 34 +++++++++++++++++++++++++ server/src/interfaces.ts | 23 ++++++++++++++--- server/src/optiondb/lmdbx.ts | 12 +++++++++ 4 files changed, 81 insertions(+), 7 deletions(-) diff --git a/server/src/calendardb/lmdbx.ts b/server/src/calendardb/lmdbx.ts index b8f27df..b9d1626 100644 --- a/server/src/calendardb/lmdbx.ts +++ b/server/src/calendardb/lmdbx.ts @@ -3,13 +3,11 @@ import { open } from "lmdbx"; const calendarAggregatesDb = open({ path: "./calendar-aggregates.db", - // any options go here, we can turn on compression like this: compression: true, }); const calendarExistenceDb = open({ path: "./calendar-existence.db", - // any options go here, we can turn on compression like this: compression: true, }); @@ -65,6 +63,19 @@ function makeCalendarDatabase(): CalendarDatabase { low: value.low, })).asArray; }, + getAggregate: async ({ + key: { symbol, frontExpirationDate, backExpirationDate, strike, type }, + tsStart, + }) => { + return await calendarAggregatesDb.get([ + symbol, + frontExpirationDate, + backExpirationDate, + strike, + type, + tsStart, + ]); + }, insertAggregates: async (aggregates) => { await calendarExistenceDb.batch(() => { for (const aggregate of aggregates) { @@ -77,7 +88,7 @@ function makeCalendarDatabase(): CalendarDatabase { aggregate.key.strike, aggregate.key.type, ], - null, + null ); } }); @@ -97,7 +108,7 @@ function makeCalendarDatabase(): CalendarDatabase { close: aggregate.close, high: aggregate.high, low: aggregate.low, - }, + } ); } }); diff --git a/server/src/calendardb/optiondb-lmdbx.ts b/server/src/calendardb/optiondb-lmdbx.ts index 7f5bf75..f944280 100644 --- a/server/src/calendardb/optiondb-lmdbx.ts +++ b/server/src/calendardb/optiondb-lmdbx.ts @@ -145,6 +145,40 @@ function makeCalendarDatabase(): CalendarDatabase { } return minPrice; }, + getAggregate: async ({ + key: { symbol, frontExpirationDate, backExpirationDate, strike, type }, + tsStart, + }) => { + const [frontOptionContractAggregate, backOptionContractAggregate] = + await Promise.all([ + optionContractDatabase.getAggregate({ + key: { symbol, expirationDate: frontExpirationDate, strike, type }, + tsStart, + }), + optionContractDatabase.getAggregate({ + key: { symbol, expirationDate: backExpirationDate, strike, type }, + tsStart, + }), + ]); + // only return the calendar aggregate if its constituent front and back option contract aggregates exist: + if (frontOptionContractAggregate && backOptionContractAggregate) { + return { + tsStart, + open: + backOptionContractAggregate.open - + frontOptionContractAggregate.open, + close: + backOptionContractAggregate.close - + frontOptionContractAggregate.close, + high: + backOptionContractAggregate.high - + frontOptionContractAggregate.high, + low: + backOptionContractAggregate.low - frontOptionContractAggregate.low, + }; + } + return undefined; + }, getTargetPriceByProbability: async ({ symbol, calendarSpan, diff --git a/server/src/interfaces.ts b/server/src/interfaces.ts index f66f048..065af66 100644 --- a/server/src/interfaces.ts +++ b/server/src/interfaces.ts @@ -15,15 +15,32 @@ export type AggregateDatabase = { getKeys: ({ key, date, - }: { key?: T | Partial; date?: string }) => Promise>; + }: { + key?: T | Partial; + date?: string; + }) => Promise>; getAggregates: ({ key, date, - }: { key: T; date: string }) => Promise, "key">>>; + }: { + key: T; + date: string; + }) => Promise, "key">>>; + /** Since an aggregate may not exist at the specified `tsStart`, return `undefined` if it doesn't exist. */ + getAggregate: ({ + key, + tsStart, + }: { + key: T; + tsStart: number; + }) => Promise, "key"> | undefined>; getAggregatesSync?: ({ key, date, - }: { key: T; date: string }) => Array, "key">>; + }: { + key: T; + date: string; + }) => Array, "key">>; insertAggregates: (aggregates: Array>) => Promise; getClosingPrice: ({ key }: { key: T }) => Promise; }; diff --git a/server/src/optiondb/lmdbx.ts b/server/src/optiondb/lmdbx.ts index 127898d..f2c61cc 100644 --- a/server/src/optiondb/lmdbx.ts +++ b/server/src/optiondb/lmdbx.ts @@ -115,6 +115,18 @@ function makeOptionContractDatabase(): OptionContractDatabase { } return minPrice; }, + getAggregate: async ({ + key: { symbol, expirationDate, strike, type }, + tsStart, + }) => { + return await optionContractAggregatesDb.get([ + symbol, + expirationDate, + strike, + type, + tsStart, + ]); + }, }; return {