fix: Refactor 52-week high calculation in industry leadership check
This commit is contained in:
parent
327c3b8dfe
commit
8302d796e0
@ -51,14 +51,26 @@ def check_industry_leadership(symbol):
|
||||
|
||||
# Get RSI from stock_indicators and price data from stock_prices_daily
|
||||
metrics_query = f"""
|
||||
WITH latest_price AS (
|
||||
SELECT
|
||||
close as current_price,
|
||||
MAX(close) OVER (RANGE BETWEEN INTERVAL 52 WEEK PRECEDING AND CURRENT ROW) as high_52_week
|
||||
WITH latest_date AS (
|
||||
SELECT MAX(date) as max_date
|
||||
FROM stock_db.stock_prices_daily
|
||||
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 (
|
||||
SELECT rsi
|
||||
@ -68,11 +80,11 @@ def check_industry_leadership(symbol):
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT
|
||||
current_price,
|
||||
high_52_week,
|
||||
rsi
|
||||
FROM latest_price
|
||||
CROSS JOIN latest_rsi
|
||||
yr.current_price,
|
||||
yr.high_52_week,
|
||||
lr.rsi
|
||||
FROM year_range yr
|
||||
CROSS JOIN latest_rsi lr
|
||||
"""
|
||||
|
||||
# Execute queries
|
||||
|
||||
Loading…
Reference in New Issue
Block a user