refactor: Filter stock data before aggregation in scanner queries
This commit is contained in:
parent
a9d380897a
commit
cd7a33cb7e
@ -73,18 +73,26 @@ def run_atr_ema_scanner(min_price: float, max_price: float, min_volume: int, por
|
||||
try:
|
||||
with create_client() as client:
|
||||
query = f"""
|
||||
WITH latest_data AS (
|
||||
WITH filtered_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
window_start,
|
||||
close,
|
||||
volume
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
AND close BETWEEN {min_price} AND {max_price}
|
||||
AND volume >= {min_volume}
|
||||
),
|
||||
latest_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
argMax(close, window_start) as last_close,
|
||||
sum(volume) as total_volume,
|
||||
max(window_start) as last_update
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
FROM filtered_data
|
||||
GROUP BY ticker
|
||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
||||
AND total_volume >= {min_volume}
|
||||
)
|
||||
SELECT
|
||||
ticker,
|
||||
|
||||
@ -82,18 +82,26 @@ def run_atr_ema_scanner_v2(min_price: float, max_price: float, min_volume: int,
|
||||
with create_client() as client:
|
||||
# Query to get qualified stocks
|
||||
query = f"""
|
||||
WITH latest_data AS (
|
||||
WITH filtered_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
window_start,
|
||||
close,
|
||||
volume
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
AND close BETWEEN {min_price} AND {max_price}
|
||||
AND volume >= {min_volume}
|
||||
),
|
||||
latest_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
argMax(close, window_start) as last_close,
|
||||
sum(volume) as total_volume,
|
||||
max(window_start) as last_update
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
FROM filtered_data
|
||||
GROUP BY ticker
|
||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
||||
AND total_volume >= {min_volume}
|
||||
)
|
||||
SELECT
|
||||
ticker,
|
||||
|
||||
@ -216,18 +216,26 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int, portf
|
||||
with create_client() as client:
|
||||
# Query to get stocks meeting criteria with their latest data
|
||||
query = f"""
|
||||
WITH latest_data AS (
|
||||
WITH filtered_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
window_start,
|
||||
close,
|
||||
volume
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
AND close BETWEEN {min_price} AND {max_price}
|
||||
AND volume >= {min_volume}
|
||||
),
|
||||
latest_data AS (
|
||||
SELECT
|
||||
ticker,
|
||||
argMax(close, window_start) as last_close,
|
||||
sum(volume) as total_volume,
|
||||
max(window_start) as last_update
|
||||
FROM stock_db.stock_prices
|
||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
||||
AND toDateTime(window_start/1000000000) <= now()
|
||||
FROM filtered_data
|
||||
GROUP BY ticker
|
||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
||||
AND total_volume >= {min_volume}
|
||||
)
|
||||
SELECT
|
||||
ticker,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user