CREATE TABLE symbols ( symbol String, name String, sector LowCardinality(String), subindustry LowCardinality(String), headquarters_location String, date_added Date, cik UInt32, year_founded String ) ENGINE MergeTree() ORDER BY (symbol); CREATE TABLE option_contract_sync_statuses ( symbol String, asOfDate Date, status ENUM('not-started','pending','done') ) ENGINE MergeTree() ORDER BY (asOfDate, symbol); CREATE TABLE option_contracts ( asOfDate Date, symbol LowCardinality(String), expirationDate Date, strike Float32, type ENUM('call', 'put') ) ENGINE MergeTree() PRIMARY KEY (asOfDate, symbol) ORDER BY (asOfDate, symbol, expirationDate, strike, type); CREATE TABLE stock_aggregates ( symbol LowCardinality(String), tsStart DateTime32, open Float64, close Float64, low Float64, high Float64, volume UInt64, volume_weighted_price Float64 ) ENGINE MergeTree() ORDER BY (symbol, tsStart) CREATE TABLE option_aggregates ( symbol LowCardinality(String), expirationDate Date, optionType Enum('call', 'put'), strike Float64, tsStart DateTime32, open Float64, close Float64, low Float64, high Float64, volume UInt64, volumeWeightedPrice Float64 ) ENGINE MergeTree() ORDER BY (symbol, expirationDate, optionType, strike, tsStart) ALTER TABLE option_aggregates ADD INDEX idx_expirationDate expirationDate TYPE minmax GRANULARITY 2; ALTER TABLE option_aggregates ADD INDEX idx_strike strike TYPE minmax GRANULARITY 2; ALTER TABLE option_aggregates ADD INDEX idx_tsStart tsStart TYPE minmax GRANULARITY 2; CREATE TABLE option_histories ( symbol LowCardinality(String), expirationDate Date, strike Float64, tsStart DateTime32, open Float64, daysToFront UInt16, underlyingPrice Float64, strikePercentageFromUnderlyingPrice Float64 ) ENGINE MergeTree() ORDER BY (symbol, daysToFront, strikePercentageFromUnderlyingPrice) CREATE TABLE calendar_histories ( symbol LowCardinality(String), tsStart DateTime32, frontExpirationDate Date, backExpirationDate Date, daysToFrontExpiration UInt16, daysBetweenFrontAndBackExpiration UInt16, strike Float64, underlyingPrice Float64, strikePercentageFromUnderlyingPrice Float64, calendarPrice Float64 ) ENGINE MergeTree() PRIMARY KEY (symbol, daysToFrontExpiration, daysBetweenFrontAndBackExpiration, strikePercentageFromUnderlyingPrice) ORDER BY (symbol, daysToFrontExpiration, daysBetweenFrontAndBackExpiration, strikePercentageFromUnderlyingPrice, tsStart) -- INSERT INTO calendar_histories -- SELECT -- front_option.symbol as symbol, -- front_option.tsStart as tsStart, -- front_option.expirationDate as frontExpirationDate, -- back_option.expirationDate as backExpirationDate, -- front_option.daysToFront as daysToFrontExpiration, -- backExpirationDate - frontExpirationDate as daysBetweenFrontAndBackExpiration, -- front_option.strike as strike, -- front_option.underlyingPrice as underlyingPrice, -- front_option.strikePercentageFromUnderlyingPrice, -- back_option.open - front_option.open as calendarPrice -- FROM ( -- SELECT -- symbol, -- tsStart, -- expirationDate, -- strike, -- open, -- daysToFront, -- underlyingPrice, -- strikePercentageFromUnderlyingPrice -- FROM option_histories -- ) AS front_option -- LEFT JOIN option_aggregates as back_option -- ON front_option.symbol = back_option.symbol -- AND front_option.strike = back_option.strike -- AND front_option.tsStart = back_option.tsStart -- WHERE back_option.expirationDate > front_option.expirationDate -- SETTINGS join_algorithm = 'grace_hash', grace_hash_join_initial_buckets = 16, max_bytes_in_join = 536870912