refactor: Update scanner queries to calculate daily close prices from timestamps

This commit is contained in:
Bobby (aider) 2025-02-09 00:20:10 -08:00
parent cd7a33cb7e
commit d4e0bc2c92
3 changed files with 51 additions and 21 deletions

View File

@ -78,20 +78,30 @@ def run_atr_ema_scanner(min_price: float, max_price: float, min_volume: int, por
ticker, ticker,
window_start, window_start,
close, close,
volume volume,
toDate(toDateTime(window_start/1000000000)) as trade_date
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE window_start BETWEEN {start_ts} AND {end_ts} WHERE window_start BETWEEN {start_ts} AND {end_ts}
AND toDateTime(window_start/1000000000) <= now() AND toDateTime(window_start/1000000000) <= now()
AND close BETWEEN {min_price} AND {max_price} ),
AND volume >= {min_volume} daily_data AS (
SELECT
ticker,
trade_date,
argMax(close, window_start) as daily_close,
sum(volume) as daily_volume
FROM filtered_data
GROUP BY ticker, trade_date
HAVING daily_close BETWEEN {min_price} AND {max_price}
AND daily_volume >= {min_volume}
), ),
latest_data AS ( latest_data AS (
SELECT SELECT
ticker, ticker,
argMax(close, window_start) as last_close, argMax(daily_close, trade_date) as last_close,
sum(volume) as total_volume, sum(daily_volume) as total_volume,
max(window_start) as last_update max(trade_date) as last_update
FROM filtered_data FROM daily_data
GROUP BY ticker GROUP BY ticker
) )
SELECT SELECT

View File

@ -87,20 +87,30 @@ def run_atr_ema_scanner_v2(min_price: float, max_price: float, min_volume: int,
ticker, ticker,
window_start, window_start,
close, close,
volume volume,
toDate(toDateTime(window_start/1000000000)) as trade_date
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE window_start BETWEEN {start_ts} AND {end_ts} WHERE window_start BETWEEN {start_ts} AND {end_ts}
AND toDateTime(window_start/1000000000) <= now() AND toDateTime(window_start/1000000000) <= now()
AND close BETWEEN {min_price} AND {max_price} ),
AND volume >= {min_volume} daily_data AS (
SELECT
ticker,
trade_date,
argMax(close, window_start) as daily_close,
sum(volume) as daily_volume
FROM filtered_data
GROUP BY ticker, trade_date
HAVING daily_close BETWEEN {min_price} AND {max_price}
AND daily_volume >= {min_volume}
), ),
latest_data AS ( latest_data AS (
SELECT SELECT
ticker, ticker,
argMax(close, window_start) as last_close, argMax(daily_close, trade_date) as last_close,
sum(volume) as total_volume, sum(daily_volume) as total_volume,
max(window_start) as last_update max(trade_date) as last_update
FROM filtered_data FROM daily_data
GROUP BY ticker GROUP BY ticker
) )
SELECT SELECT

View File

@ -221,20 +221,30 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int, portf
ticker, ticker,
window_start, window_start,
close, close,
volume volume,
toDate(toDateTime(window_start/1000000000)) as trade_date
FROM stock_db.stock_prices FROM stock_db.stock_prices
WHERE window_start BETWEEN {start_ts} AND {end_ts} WHERE window_start BETWEEN {start_ts} AND {end_ts}
AND toDateTime(window_start/1000000000) <= now() AND toDateTime(window_start/1000000000) <= now()
AND close BETWEEN {min_price} AND {max_price} ),
AND volume >= {min_volume} daily_data AS (
SELECT
ticker,
trade_date,
argMax(close, window_start) as daily_close,
sum(volume) as daily_volume
FROM filtered_data
GROUP BY ticker, trade_date
HAVING daily_close BETWEEN {min_price} AND {max_price}
AND daily_volume >= {min_volume}
), ),
latest_data AS ( latest_data AS (
SELECT SELECT
ticker, ticker,
argMax(close, window_start) as last_close, argMax(daily_close, trade_date) as last_close,
sum(volume) as total_volume, sum(daily_volume) as total_volume,
max(window_start) as last_update max(trade_date) as last_update
FROM filtered_data FROM daily_data
GROUP BY ticker GROUP BY ticker
) )
SELECT SELECT