fix: Improve date handling for stock data retrieval and ticker selection
This commit is contained in:
parent
99ea46f6b1
commit
507687dc90
@ -31,9 +31,9 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
|
|||||||
"""Fetch stock data from the database"""
|
"""Fetch stock data from the database"""
|
||||||
client = create_client()
|
client = create_client()
|
||||||
|
|
||||||
# Always look back 60 days for proper DMA calculation
|
# Calculate proper date range (looking back from today)
|
||||||
today = datetime.now().date()
|
end_date = datetime.now()
|
||||||
start_date = today - timedelta(days=60)
|
start_date = end_date - timedelta(days=60) # 60 days of history
|
||||||
|
|
||||||
if interval == "daily":
|
if interval == "daily":
|
||||||
table = "stock_prices_daily"
|
table = "stock_prices_daily"
|
||||||
@ -113,13 +113,17 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
|
|||||||
def get_valid_tickers(min_price: float, max_price: float, min_volume: int, interval: str) -> list:
|
def get_valid_tickers(min_price: float, max_price: float, min_volume: int, interval: str) -> list:
|
||||||
"""Get tickers that meet the price and volume criteria"""
|
"""Get tickers that meet the price and volume criteria"""
|
||||||
client = create_client()
|
client = create_client()
|
||||||
yesterday = (datetime.now() - timedelta(days=1)).date()
|
|
||||||
|
# Get the most recent trading day
|
||||||
|
today = datetime.now().date()
|
||||||
|
if today.weekday() >= 5: # If it's weekend
|
||||||
|
today = today - timedelta(days=today.weekday() - 4) # Go back to Friday
|
||||||
|
|
||||||
# First get valid tickers from daily data
|
# First get valid tickers from daily data
|
||||||
daily_query = f"""
|
daily_query = f"""
|
||||||
SELECT DISTINCT ticker
|
SELECT DISTINCT ticker
|
||||||
FROM stock_db.stock_prices_daily
|
FROM stock_db.stock_prices_daily
|
||||||
WHERE date = '{yesterday}'
|
WHERE date = '{today}'
|
||||||
AND close BETWEEN {min_price} AND {max_price}
|
AND close BETWEEN {min_price} AND {max_price}
|
||||||
AND volume >= {min_volume}
|
AND volume >= {min_volume}
|
||||||
ORDER BY ticker ASC
|
ORDER BY ticker ASC
|
||||||
@ -128,26 +132,35 @@ def get_valid_tickers(min_price: float, max_price: float, min_volume: int, inter
|
|||||||
try:
|
try:
|
||||||
result = client.query(daily_query)
|
result = client.query(daily_query)
|
||||||
tickers = [row[0] for row in result.result_rows]
|
tickers = [row[0] for row in result.result_rows]
|
||||||
|
|
||||||
|
if not tickers: # If no data for today, try yesterday
|
||||||
|
yesterday = today - timedelta(days=1)
|
||||||
|
if yesterday.weekday() >= 5: # If yesterday was weekend
|
||||||
|
yesterday = yesterday - timedelta(days=yesterday.weekday() - 4) # Go back to Friday
|
||||||
|
|
||||||
|
daily_query = f"""
|
||||||
|
SELECT DISTINCT ticker
|
||||||
|
FROM stock_db.stock_prices_daily
|
||||||
|
WHERE date = '{yesterday}'
|
||||||
|
AND close BETWEEN {min_price} AND {max_price}
|
||||||
|
AND volume >= {min_volume}
|
||||||
|
ORDER BY ticker ASC
|
||||||
|
"""
|
||||||
|
result = client.query(daily_query)
|
||||||
|
tickers = [row[0] for row in result.result_rows]
|
||||||
|
|
||||||
print(f"\nFound {len(tickers)} stocks matching price and volume criteria")
|
print(f"\nFound {len(tickers)} stocks matching price and volume criteria")
|
||||||
|
|
||||||
if interval != "daily":
|
if interval != "daily":
|
||||||
# Now verify these tickers have intraday data in the last trading day
|
# Now verify these tickers have intraday data
|
||||||
# Get the most recent trading day's timestamp range
|
market_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp())
|
||||||
today = datetime.now().date()
|
market_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp())
|
||||||
yesterday_open = int(datetime.combine(yesterday, datetime.strptime("09:30", "%H:%M").time()).timestamp())
|
|
||||||
yesterday_close = int(datetime.combine(yesterday, datetime.strptime("16:00", "%H:%M").time()).timestamp())
|
|
||||||
today_open = int(datetime.combine(today, datetime.strptime("09:30", "%H:%M").time()).timestamp())
|
|
||||||
today_close = int(datetime.combine(today, datetime.strptime("16:00", "%H:%M").time()).timestamp())
|
|
||||||
|
|
||||||
intraday_query = f"""
|
intraday_query = f"""
|
||||||
SELECT DISTINCT ticker
|
SELECT DISTINCT ticker
|
||||||
FROM stock_db.stock_prices
|
FROM stock_db.stock_prices
|
||||||
WHERE ticker IN ({','.join([f"'{t}'" for t in tickers])})
|
WHERE ticker IN ({','.join([f"'{t}'" for t in tickers])})
|
||||||
AND (
|
AND window_start BETWEEN {market_open - 86400} AND {market_close} -- Include last 24 hours
|
||||||
(window_start BETWEEN {yesterday_open} AND {yesterday_close})
|
|
||||||
OR
|
|
||||||
(window_start BETWEEN {today_open} AND {today_close})
|
|
||||||
)
|
|
||||||
GROUP BY ticker
|
GROUP BY ticker
|
||||||
HAVING count() >= 10 -- Ensure we have enough data points
|
HAVING count() >= 10 -- Ensure we have enough data points
|
||||||
"""
|
"""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user