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:
|
try:
|
||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
query = f"""
|
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
|
SELECT
|
||||||
ticker,
|
ticker,
|
||||||
argMax(close, window_start) as last_close,
|
argMax(close, window_start) as last_close,
|
||||||
sum(volume) as total_volume,
|
sum(volume) as total_volume,
|
||||||
max(window_start) as last_update
|
max(window_start) as last_update
|
||||||
FROM stock_db.stock_prices
|
FROM filtered_data
|
||||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
|
||||||
AND toDateTime(window_start/1000000000) <= now()
|
|
||||||
GROUP BY ticker
|
GROUP BY ticker
|
||||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
|
||||||
AND total_volume >= {min_volume}
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
ticker,
|
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:
|
with create_client() as client:
|
||||||
# Query to get qualified stocks
|
# Query to get qualified stocks
|
||||||
query = f"""
|
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
|
SELECT
|
||||||
ticker,
|
ticker,
|
||||||
argMax(close, window_start) as last_close,
|
argMax(close, window_start) as last_close,
|
||||||
sum(volume) as total_volume,
|
sum(volume) as total_volume,
|
||||||
max(window_start) as last_update
|
max(window_start) as last_update
|
||||||
FROM stock_db.stock_prices
|
FROM filtered_data
|
||||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
|
||||||
AND toDateTime(window_start/1000000000) <= now()
|
|
||||||
GROUP BY ticker
|
GROUP BY ticker
|
||||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
|
||||||
AND total_volume >= {min_volume}
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
ticker,
|
ticker,
|
||||||
|
|||||||
@ -216,18 +216,26 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int, portf
|
|||||||
with create_client() as client:
|
with create_client() as client:
|
||||||
# Query to get stocks meeting criteria with their latest data
|
# Query to get stocks meeting criteria with their latest data
|
||||||
query = f"""
|
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
|
SELECT
|
||||||
ticker,
|
ticker,
|
||||||
argMax(close, window_start) as last_close,
|
argMax(close, window_start) as last_close,
|
||||||
sum(volume) as total_volume,
|
sum(volume) as total_volume,
|
||||||
max(window_start) as last_update
|
max(window_start) as last_update
|
||||||
FROM stock_db.stock_prices
|
FROM filtered_data
|
||||||
WHERE window_start BETWEEN {start_ts} AND {end_ts}
|
|
||||||
AND toDateTime(window_start/1000000000) <= now()
|
|
||||||
GROUP BY ticker
|
GROUP BY ticker
|
||||||
HAVING last_close BETWEEN {min_price} AND {max_price}
|
|
||||||
AND total_volume >= {min_volume}
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
ticker,
|
ticker,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user