add `getAggregate` method

main
avraham 9 months ago
parent 35b3278d08
commit 666ff16583

@ -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,
},
}
);
}
});

@ -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,

@ -15,15 +15,32 @@ export type AggregateDatabase<T> = {
getKeys: ({
key,
date,
}: { key?: T | Partial<T>; date?: string }) => Promise<Array<T>>;
}: {
key?: T | Partial<T>;
date?: string;
}) => Promise<Array<T>>;
getAggregates: ({
key,
date,
}: { key: T; date: string }) => Promise<Array<Omit<Aggregate<T>, "key">>>;
}: {
key: T;
date: string;
}) => Promise<Array<Omit<Aggregate<T>, "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<Omit<Aggregate<T>, "key"> | undefined>;
getAggregatesSync?: ({
key,
date,
}: { key: T; date: string }) => Array<Omit<Aggregate<T>, "key">>;
}: {
key: T;
date: string;
}) => Array<Omit<Aggregate<T>, "key">>;
insertAggregates: (aggregates: Array<Aggregate<T>>) => Promise<void>;
getClosingPrice: ({ key }: { key: T }) => Promise<number>;
};

@ -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 {

Loading…
Cancel
Save