fix: Refactor 52-week high calculation in industry leadership check

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 19:07:42 -08:00
parent 327c3b8dfe
commit 8302d796e0
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -51,14 +51,26 @@ def check_industry_leadership(symbol):
# Get RSI from stock_indicators and price data from stock_prices_daily # Get RSI from stock_indicators and price data from stock_prices_daily
metrics_query = f""" metrics_query = f"""
WITH latest_price AS ( WITH latest_date AS (
SELECT SELECT MAX(date) as max_date
close as current_price,
MAX(close) OVER (RANGE BETWEEN INTERVAL 52 WEEK PRECEDING AND CURRENT ROW) as high_52_week
FROM stock_db.stock_prices_daily FROM stock_db.stock_prices_daily
WHERE ticker = '{symbol}' WHERE ticker = '{symbol}'
ORDER BY date DESC ),
LIMIT 1 year_range AS (
SELECT
close as current_price,
(
SELECT MAX(close)
FROM stock_db.stock_prices_daily
WHERE ticker = '{symbol}'
AND date >= DATE_SUB(
(SELECT max_date FROM latest_date),
INTERVAL 52 WEEK
)
) as high_52_week
FROM stock_db.stock_prices_daily
WHERE ticker = '{symbol}'
AND date = (SELECT max_date FROM latest_date)
), ),
latest_rsi AS ( latest_rsi AS (
SELECT rsi SELECT rsi
@ -68,11 +80,11 @@ def check_industry_leadership(symbol):
LIMIT 1 LIMIT 1
) )
SELECT SELECT
current_price, yr.current_price,
high_52_week, yr.high_52_week,
rsi lr.rsi
FROM latest_price FROM year_range yr
CROSS JOIN latest_rsi CROSS JOIN latest_rsi lr
""" """
# Execute queries # Execute queries