refactor: Update industry leadership check to use multiple tables and RSI
This commit is contained in:
parent
0b212e8847
commit
327c3b8dfe
@ -30,9 +30,9 @@ def check_industry_leadership(symbol):
|
||||
Determines if a stock is a leader in its industry group.
|
||||
|
||||
Criteria:
|
||||
- Relative Strength (RS) Rating should be **80 or higher**.
|
||||
- Stock should be in the top-performing industry groups (top 40-50).
|
||||
- Stock should be trading near its **52-week high**.
|
||||
- RSI should be 60 or higher as a strength indicator
|
||||
- Stock should be trading near its 52-week high
|
||||
- Must have valid industry classification
|
||||
|
||||
Args:
|
||||
symbol (str): Stock ticker symbol.
|
||||
@ -42,33 +42,58 @@ def check_industry_leadership(symbol):
|
||||
"""
|
||||
client = create_client()
|
||||
|
||||
query = f"""
|
||||
SELECT
|
||||
ticker,
|
||||
sic,
|
||||
relative_strength,
|
||||
high_52_week,
|
||||
current_price
|
||||
FROM stock_db.stock_indicators
|
||||
# First get the SIC code from stock_tickers table
|
||||
sic_query = f"""
|
||||
SELECT sic_code
|
||||
FROM stock_db.stock_tickers
|
||||
WHERE ticker = '{symbol}'
|
||||
"""
|
||||
|
||||
result = client.query(query)
|
||||
# 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
|
||||
FROM stock_db.stock_prices_daily
|
||||
WHERE ticker = '{symbol}'
|
||||
ORDER BY date DESC
|
||||
LIMIT 1
|
||||
),
|
||||
latest_rsi AS (
|
||||
SELECT rsi
|
||||
FROM stock_db.stock_indicators
|
||||
WHERE ticker = '{symbol}'
|
||||
ORDER BY date DESC
|
||||
LIMIT 1
|
||||
)
|
||||
SELECT
|
||||
current_price,
|
||||
high_52_week,
|
||||
rsi
|
||||
FROM latest_price
|
||||
CROSS JOIN latest_rsi
|
||||
"""
|
||||
|
||||
if not result.result_rows:
|
||||
# Execute queries
|
||||
sic_result = client.query(sic_query)
|
||||
metrics_result = client.query(metrics_query)
|
||||
|
||||
if not sic_result.result_rows or not metrics_result.result_rows:
|
||||
return 0.25 # Not enough data
|
||||
|
||||
_, sic, rs_rating, high_52_week, current_price = result.result_rows[0]
|
||||
|
||||
sic = sic_result.result_rows[0][0]
|
||||
if not sic or str(sic) not in SIC_LOOKUP:
|
||||
return 0.25 # No SIC industry data available
|
||||
|
||||
# Ensure RS rating is valid
|
||||
if rs_rating is None or high_52_week is None or current_price is None:
|
||||
current_price, high_52_week, rsi = metrics_result.result_rows[0]
|
||||
|
||||
# Ensure we have valid data
|
||||
if high_52_week is None or current_price is None or rsi is None:
|
||||
return 0.25 # Missing necessary data
|
||||
|
||||
# Industry Leader Criteria
|
||||
passes_rs = rs_rating >= 80
|
||||
passes_rs = rsi >= 60 # Using RSI as a substitute for relative strength
|
||||
near_high = current_price >= (high_52_week * 0.90) # Within 10% of 52-week high
|
||||
|
||||
if passes_rs and near_high:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user