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.
|
Determines if a stock is a leader in its industry group.
|
||||||
|
|
||||||
Criteria:
|
Criteria:
|
||||||
- Relative Strength (RS) Rating should be **80 or higher**.
|
- RSI should be 60 or higher as a strength indicator
|
||||||
- Stock should be in the top-performing industry groups (top 40-50).
|
- Stock should be trading near its 52-week high
|
||||||
- Stock should be trading near its **52-week high**.
|
- Must have valid industry classification
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
symbol (str): Stock ticker symbol.
|
symbol (str): Stock ticker symbol.
|
||||||
@ -42,33 +42,58 @@ def check_industry_leadership(symbol):
|
|||||||
"""
|
"""
|
||||||
client = create_client()
|
client = create_client()
|
||||||
|
|
||||||
query = f"""
|
# First get the SIC code from stock_tickers table
|
||||||
SELECT
|
sic_query = f"""
|
||||||
ticker,
|
SELECT sic_code
|
||||||
sic,
|
FROM stock_db.stock_tickers
|
||||||
relative_strength,
|
|
||||||
high_52_week,
|
|
||||||
current_price
|
|
||||||
FROM stock_db.stock_indicators
|
|
||||||
WHERE ticker = '{symbol}'
|
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
|
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:
|
if not sic or str(sic) not in SIC_LOOKUP:
|
||||||
return 0.25 # No SIC industry data available
|
return 0.25 # No SIC industry data available
|
||||||
|
|
||||||
# Ensure RS rating is valid
|
current_price, high_52_week, rsi = metrics_result.result_rows[0]
|
||||||
if rs_rating is None or high_52_week is None or current_price is None:
|
|
||||||
|
# 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
|
return 0.25 # Missing necessary data
|
||||||
|
|
||||||
# Industry Leader Criteria
|
# 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
|
near_high = current_price >= (high_52_week * 0.90) # Within 10% of 52-week high
|
||||||
|
|
||||||
if passes_rs and near_high:
|
if passes_rs and near_high:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user