fix: `getClosingPrice()` for two calendardb modules

main
avraham 9 months ago
parent 5b3e9f85f6
commit 8d908521fd

@ -116,11 +116,12 @@ function makeCalendarDatabase(): CalendarDatabase {
getClosingPrice: async ({ getClosingPrice: async ({
key: { symbol, strike, type, frontExpirationDate, backExpirationDate }, key: { symbol, strike, type, frontExpirationDate, backExpirationDate },
}) => { }) => {
const startOfLastHourUnix = new Date( const startOfExpirationDateUnix = new Date(
`${frontExpirationDate}T00:00:00Z`, `${frontExpirationDate}T23:59:59Z`
).valueOf();
const endOfExpirationDateUnix = new Date(
`${frontExpirationDate}T00:00:00Z`
).valueOf(); ).valueOf();
const endOfLastHourUnix = startOfLastHourUnix + 3600 * 1000;
let minPrice = 0;
for (const { value } of calendarAggregatesDb.getRange({ for (const { value } of calendarAggregatesDb.getRange({
start: [ start: [
symbol, symbol,
@ -128,7 +129,7 @@ function makeCalendarDatabase(): CalendarDatabase {
backExpirationDate, backExpirationDate,
strike, strike,
type, type,
startOfLastHourUnix, startOfExpirationDateUnix,
], ],
end: [ end: [
symbol, symbol,
@ -136,14 +137,15 @@ function makeCalendarDatabase(): CalendarDatabase {
backExpirationDate, backExpirationDate,
strike, strike,
type, type,
endOfLastHourUnix, endOfExpirationDateUnix,
], ],
reverse: true,
})) { })) {
if (value.close < minPrice || minPrice === 0) { if (value.close > 0) {
minPrice = value.close; return value.close;
} }
} }
return minPrice; return 0;
}, },
getTargetPriceByProbability: async ({ getTargetPriceByProbability: async ({
symbol, symbol,

@ -93,9 +93,10 @@ function makeCalendarDatabase(): CalendarDatabase {
getClosingPrice: async ({ getClosingPrice: async ({
key: { symbol, strike, type, frontExpirationDate, backExpirationDate }, key: { symbol, strike, type, frontExpirationDate, backExpirationDate },
}) => { }) => {
// get unix timestamp, in milliseconds, of the start of the last hour, which is 03:30PM in the `America/New_York` timezone on the front expiration date:
const startOfLastHourUnix = new Date( const startOfLastHourUnix = new Date(
`${frontExpirationDate}T00:00:00Z`, `${frontExpirationDate}T19:30:00Z`
).valueOf(); ).getTime();
const endOfLastHourUnix = startOfLastHourUnix + 3600 * 1000; const endOfLastHourUnix = startOfLastHourUnix + 3600 * 1000;
const frontOptionContractAggregates = ( const frontOptionContractAggregates = (
await optionContractDatabase.getAggregates({ await optionContractDatabase.getAggregates({
@ -104,7 +105,7 @@ function makeCalendarDatabase(): CalendarDatabase {
}) })
).filter( ).filter(
({ tsStart }) => ({ tsStart }) =>
tsStart >= startOfLastHourUnix && tsStart < endOfLastHourUnix, tsStart >= startOfLastHourUnix && tsStart < endOfLastHourUnix
); );
const backOptionContractAggregates = ( const backOptionContractAggregates = (
await optionContractDatabase.getAggregates({ await optionContractDatabase.getAggregates({
@ -113,7 +114,7 @@ function makeCalendarDatabase(): CalendarDatabase {
}) })
).filter( ).filter(
({ tsStart }) => ({ tsStart }) =>
tsStart >= startOfLastHourUnix && tsStart < endOfLastHourUnix, tsStart >= startOfLastHourUnix && tsStart < endOfLastHourUnix
); );
let i = 0; let i = 0;
let j = 0; let j = 0;
@ -128,7 +129,7 @@ function makeCalendarDatabase(): CalendarDatabase {
) { ) {
const calendarClosePrice = const calendarClosePrice =
backOptionContractAggregates[j].close - backOptionContractAggregates[j].close -
frontOptionContractAggregates[j].close; frontOptionContractAggregates[i].close;
if (calendarClosePrice < minPrice || minPrice === 0) { if (calendarClosePrice < minPrice || minPrice === 0) {
minPrice = calendarClosePrice; minPrice = calendarClosePrice;
} }

Loading…
Cancel
Save