From 64a5172ea81f0b505f17d06c68d52aaa531c1c84 Mon Sep 17 00:00:00 2001 From: avraham Date: Fri, 7 Feb 2025 09:23:47 -0500 Subject: [PATCH] use new clickhouse tables --- server/src/CalendarCharacteristicsForm.ts | 2 +- server/src/CalendarExitPriceChart.ts | 23 +++++++++++------------ server/src/SimilarCalendarPriceChart.ts | 21 ++++++++++----------- server/src/StockPriceChart.ts | 11 ++++++----- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/server/src/CalendarCharacteristicsForm.ts b/server/src/CalendarCharacteristicsForm.ts index ccb172b..2357e2e 100644 --- a/server/src/CalendarCharacteristicsForm.ts +++ b/server/src/CalendarCharacteristicsForm.ts @@ -13,7 +13,7 @@ export const getAvailableUnderlyings = publicProcedure.query(async (opts) => { // SELECT DISTINCT(symbol) as symbol FROM option_contract_existences WHERE asOfDate = (SELECT max(asOfDate) FROM option_contract_existences) // `) // ).map(({ symbol }) => symbol); - return ["AAPL", "AMD", "GOOGL", "MSFT", "NFLX"]; + return ["SPY"]; }); export const getAvailableAsOfDates = publicProcedure diff --git a/server/src/CalendarExitPriceChart.ts b/server/src/CalendarExitPriceChart.ts index 4e3e423..f2fb205 100644 --- a/server/src/CalendarExitPriceChart.ts +++ b/server/src/CalendarExitPriceChart.ts @@ -30,18 +30,17 @@ export const getChartData = publicProcedure } = opts.input; return await query<[number, number, number]>( ` - SELECT - FLOOR(strikePercentageFromUnderlyingPrice, 1) as x, - FLOOR(calendarPrice, 1) as y, - count(*) as n - FROM calendar_histories - WHERE symbol = '${underlying}' - AND daysToFrontExpiration = ${daysToFrontExpiration} - AND strikePercentageFromUnderlyingPrice >= -5.0 - AND strikePercentageFromUnderlyingPrice <= 5.0 - AND daysBetweenFrontAndBackExpiration = ${daysBetweenFrontAndBackExpiration} - AND tsStart >= '${lookbackPeriodStart} 00:00:00' - AND tsStart <= '${lookbackPeriodEnd} 00:00:00' + SELECT + moniness as x, + FLOOR(price, 1) as y, + sum(number_of_quotes) as n + FROM calendar_stats + WHERE dte = ${daysToFrontExpiration} + AND moniness >= -0.05 + AND moniness <= 0.05 + AND span = ${daysBetweenFrontAndBackExpiration} + AND date >= '${lookbackPeriodStart}' + AND date <= '${lookbackPeriodEnd}' GROUP BY x, y ORDER BY x ASC, y ASC `, diff --git a/server/src/SimilarCalendarPriceChart.ts b/server/src/SimilarCalendarPriceChart.ts index 3407592..21e1f45 100644 --- a/server/src/SimilarCalendarPriceChart.ts +++ b/server/src/SimilarCalendarPriceChart.ts @@ -34,17 +34,16 @@ export const getChartData = publicProcedure } = opts.input; return await query<[number, number]>( ` - SELECT - toUnixTimestamp(tsStart) as x, - truncate(calendarPrice, 2) as y - FROM calendar_histories - WHERE symbol = '${underlying}' - AND daysToFrontExpiration = ${daysToFrontExpiration} - AND strikePercentageFromUnderlyingPrice >= ${strikePercentageFromUnderlyingPriceRangeMin} - AND strikePercentageFromUnderlyingPrice <= ${strikePercentageFromUnderlyingPriceRangeMax} - AND daysBetweenFrontAndBackExpiration = ${daysBetweenFrontAndBackExpiration} - AND tsStart >= '${lookbackPeriodStart} 00:00:00' - AND tsStart <= '${lookbackPeriodEnd} 00:00:00' + SELECT + toUnixTimestamp(date) as x, + price as y + FROM calendar_stats + WHERE dte = ${daysToFrontExpiration} + AND moniness >= ${strikePercentageFromUnderlyingPriceRangeMin} + AND moniness <= ${strikePercentageFromUnderlyingPriceRangeMax} + AND span = ${daysBetweenFrontAndBackExpiration} + AND date >= '${lookbackPeriodStart}' + AND date <= '${lookbackPeriodEnd}' `, "JSONEachRow" ); diff --git a/server/src/StockPriceChart.ts b/server/src/StockPriceChart.ts index eb12218..78a6649 100644 --- a/server/src/StockPriceChart.ts +++ b/server/src/StockPriceChart.ts @@ -17,12 +17,13 @@ export const getChartData = publicProcedure return await query<[number, number]>( ` SELECT - toUnixTimestamp(tsStart) as x, - open as y - FROM stock_aggregates + toUnixTimestamp(toStartOfHour(ts)) as x, + avg(price) as y + FROM stock_aggregates_filled WHERE symbol = '${underlying}' - AND tsStart >= '${lookbackPeriodStart} 00:00:00' - AND tsStart <= '${lookbackPeriodEnd} 00:00:00' + AND ts >= '${lookbackPeriodStart} 00:00:00' + AND ts <= '${lookbackPeriodEnd} 00:00:00' + GROUP BY x ORDER BY x ASC `, "JSONEachRow"