feat: Add price data validation and debug logging to stock data retrieval
This commit is contained in:
parent
95e1a2bcd0
commit
f7d6d5d136
@ -34,6 +34,29 @@ 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()
|
||||||
|
|
||||||
|
# Add data validation
|
||||||
|
def validate_price_data(df: pd.DataFrame) -> pd.DataFrame:
|
||||||
|
if df.empty:
|
||||||
|
return df
|
||||||
|
|
||||||
|
# Remove rows with suspicious values
|
||||||
|
df = df[
|
||||||
|
(df['open'] > 0) &
|
||||||
|
(df['high'] > 0) &
|
||||||
|
(df['low'] > 0) &
|
||||||
|
(df['close'] > 0) &
|
||||||
|
(df['high'] >= df['low']) &
|
||||||
|
(df['high'] >= df['open']) &
|
||||||
|
(df['high'] >= df['close']) &
|
||||||
|
(df['low'] <= df['open']) &
|
||||||
|
(df['low'] <= df['close'])
|
||||||
|
]
|
||||||
|
|
||||||
|
# Sort by date and remove duplicates
|
||||||
|
df = df.sort_values('date').drop_duplicates(subset=['date'])
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
# Calculate proper date range (looking back from today)
|
# Calculate proper date range (looking back from today)
|
||||||
end_date = datetime.now()
|
end_date = datetime.now()
|
||||||
start_date = end_date - timedelta(days=60) # 60 days of history
|
start_date = end_date - timedelta(days=60) # 60 days of history
|
||||||
@ -94,6 +117,17 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv
|
|||||||
columns=['date', 'open', 'high', 'low', 'close', 'volume']
|
columns=['date', 'open', 'high', 'low', 'close', 'volume']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Add validation step
|
||||||
|
df = validate_price_data(df)
|
||||||
|
|
||||||
|
if df.empty:
|
||||||
|
print(f"No valid data found for {ticker} after validation")
|
||||||
|
return df
|
||||||
|
|
||||||
|
# Add debug output for last price
|
||||||
|
last_price = df.iloc[-1]
|
||||||
|
print(f"Debug: Last valid price for {ticker}: ${last_price['close']:.2f}")
|
||||||
|
|
||||||
if interval != "daily" and interval != "5min":
|
if interval != "daily" and interval != "5min":
|
||||||
# Resample to desired interval
|
# Resample to desired interval
|
||||||
df.set_index('date', inplace=True)
|
df.set_index('date', inplace=True)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user