add getAggregate method
This commit is contained in:
@@ -3,13 +3,11 @@ import { open } from "lmdbx";
|
|||||||
|
|
||||||
const calendarAggregatesDb = open({
|
const calendarAggregatesDb = open({
|
||||||
path: "./calendar-aggregates.db",
|
path: "./calendar-aggregates.db",
|
||||||
// any options go here, we can turn on compression like this:
|
|
||||||
compression: true,
|
compression: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const calendarExistenceDb = open({
|
const calendarExistenceDb = open({
|
||||||
path: "./calendar-existence.db",
|
path: "./calendar-existence.db",
|
||||||
// any options go here, we can turn on compression like this:
|
|
||||||
compression: true,
|
compression: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -65,6 +63,19 @@ function makeCalendarDatabase(): CalendarDatabase {
|
|||||||
low: value.low,
|
low: value.low,
|
||||||
})).asArray;
|
})).asArray;
|
||||||
},
|
},
|
||||||
|
getAggregate: async ({
|
||||||
|
key: { symbol, frontExpirationDate, backExpirationDate, strike, type },
|
||||||
|
tsStart,
|
||||||
|
}) => {
|
||||||
|
return await calendarAggregatesDb.get([
|
||||||
|
symbol,
|
||||||
|
frontExpirationDate,
|
||||||
|
backExpirationDate,
|
||||||
|
strike,
|
||||||
|
type,
|
||||||
|
tsStart,
|
||||||
|
]);
|
||||||
|
},
|
||||||
insertAggregates: async (aggregates) => {
|
insertAggregates: async (aggregates) => {
|
||||||
await calendarExistenceDb.batch(() => {
|
await calendarExistenceDb.batch(() => {
|
||||||
for (const aggregate of aggregates) {
|
for (const aggregate of aggregates) {
|
||||||
@@ -77,7 +88,7 @@ function makeCalendarDatabase(): CalendarDatabase {
|
|||||||
aggregate.key.strike,
|
aggregate.key.strike,
|
||||||
aggregate.key.type,
|
aggregate.key.type,
|
||||||
],
|
],
|
||||||
null,
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -97,7 +108,7 @@ function makeCalendarDatabase(): CalendarDatabase {
|
|||||||
close: aggregate.close,
|
close: aggregate.close,
|
||||||
high: aggregate.high,
|
high: aggregate.high,
|
||||||
low: aggregate.low,
|
low: aggregate.low,
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -145,6 +145,40 @@ function makeCalendarDatabase(): CalendarDatabase {
|
|||||||
}
|
}
|
||||||
return minPrice;
|
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 ({
|
getTargetPriceByProbability: async ({
|
||||||
symbol,
|
symbol,
|
||||||
calendarSpan,
|
calendarSpan,
|
||||||
|
|||||||
@@ -15,15 +15,32 @@ export type AggregateDatabase<T> = {
|
|||||||
getKeys: ({
|
getKeys: ({
|
||||||
key,
|
key,
|
||||||
date,
|
date,
|
||||||
}: { key?: T | Partial<T>; date?: string }) => Promise<Array<T>>;
|
}: {
|
||||||
|
key?: T | Partial<T>;
|
||||||
|
date?: string;
|
||||||
|
}) => Promise<Array<T>>;
|
||||||
getAggregates: ({
|
getAggregates: ({
|
||||||
key,
|
key,
|
||||||
date,
|
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?: ({
|
getAggregatesSync?: ({
|
||||||
key,
|
key,
|
||||||
date,
|
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>;
|
insertAggregates: (aggregates: Array<Aggregate<T>>) => Promise<void>;
|
||||||
getClosingPrice: ({ key }: { key: T }) => Promise<number>;
|
getClosingPrice: ({ key }: { key: T }) => Promise<number>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -115,6 +115,18 @@ function makeOptionContractDatabase(): OptionContractDatabase {
|
|||||||
}
|
}
|
||||||
return minPrice;
|
return minPrice;
|
||||||
},
|
},
|
||||||
|
getAggregate: async ({
|
||||||
|
key: { symbol, expirationDate, strike, type },
|
||||||
|
tsStart,
|
||||||
|
}) => {
|
||||||
|
return await optionContractAggregatesDb.get([
|
||||||
|
symbol,
|
||||||
|
expirationDate,
|
||||||
|
strike,
|
||||||
|
type,
|
||||||
|
tsStart,
|
||||||
|
]);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user