refactor: Modify view_stock_details to show detailed query and raw results
This commit is contained in:
parent
bb3c5fe933
commit
99ea46f6b1
@ -169,20 +169,80 @@ def view_stock_details(ticker: str, interval: str, start_date: datetime, end_dat
|
|||||||
print(f"Date Range: {start_date.date()} to {end_date.date()}")
|
print(f"Date Range: {start_date.date()} to {end_date.date()}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get price data
|
# Construct query
|
||||||
df = get_stock_data(ticker, start_date, end_date, interval)
|
client = create_client()
|
||||||
|
today = datetime.now().date()
|
||||||
|
start_date = today - timedelta(days=60)
|
||||||
|
|
||||||
if df.empty:
|
if interval == "daily":
|
||||||
print("No data found for this stock")
|
table = "stock_prices_daily"
|
||||||
|
date_col = "date"
|
||||||
|
query = f"""
|
||||||
|
SELECT
|
||||||
|
{date_col} as date,
|
||||||
|
open,
|
||||||
|
high,
|
||||||
|
low,
|
||||||
|
close,
|
||||||
|
volume
|
||||||
|
FROM stock_db.{table}
|
||||||
|
WHERE ticker = '{ticker}'
|
||||||
|
AND {date_col} BETWEEN '{start_date}' AND '{today}'
|
||||||
|
ORDER BY date ASC
|
||||||
|
"""
|
||||||
|
else:
|
||||||
|
table = "stock_prices"
|
||||||
|
date_col = "window_start"
|
||||||
|
minutes_map = {
|
||||||
|
"5min": 5,
|
||||||
|
"15min": 15,
|
||||||
|
"30min": 30,
|
||||||
|
"1hour": 60
|
||||||
|
}
|
||||||
|
minutes = minutes_map[interval]
|
||||||
|
|
||||||
|
query = f"""
|
||||||
|
SELECT
|
||||||
|
fromUnixTimestamp(intDiv({date_col}, 300) * 300) as interval_start,
|
||||||
|
min(open) as open,
|
||||||
|
max(high) as high,
|
||||||
|
min(low) as low,
|
||||||
|
argMax(close, {date_col}) as close,
|
||||||
|
sum(volume) as volume
|
||||||
|
FROM stock_db.{table}
|
||||||
|
WHERE ticker = '{ticker}'
|
||||||
|
AND {date_col} BETWEEN toUnixTimestamp('{start_date}') AND toUnixTimestamp('{today}')
|
||||||
|
GROUP BY interval_start
|
||||||
|
ORDER BY interval_start ASC
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Print the actual query being executed
|
||||||
|
print("\nExecuting Query:")
|
||||||
|
print(query)
|
||||||
|
|
||||||
|
# Execute query and get results
|
||||||
|
result = client.query(query)
|
||||||
|
|
||||||
|
if not result.result_rows:
|
||||||
|
print("\nNo data found for this stock")
|
||||||
return
|
return
|
||||||
|
|
||||||
print("\nData Sample:")
|
# Convert to DataFrame
|
||||||
print("First 5 rows:")
|
df = pd.DataFrame(
|
||||||
print(df.head().to_string())
|
result.result_rows,
|
||||||
print("\nLast 5 rows:")
|
columns=['date', 'open', 'high', 'low', 'close', 'volume']
|
||||||
print(df.tail().to_string())
|
)
|
||||||
|
|
||||||
print(f"\nTotal rows: {len(df)}")
|
# Print raw query results
|
||||||
|
print("\nRaw Query Results:")
|
||||||
|
print(f"Number of rows returned: {len(result.result_rows)}")
|
||||||
|
print("\nFirst 5 rows of raw data:")
|
||||||
|
for i, row in enumerate(result.result_rows[:5]):
|
||||||
|
print(f"Row {i + 1}: {row}")
|
||||||
|
|
||||||
|
print("\nLast 5 rows of raw data:")
|
||||||
|
for i, row in enumerate(result.result_rows[-5:]):
|
||||||
|
print(f"Row {len(result.result_rows) - 4 + i}: {row}")
|
||||||
|
|
||||||
# Calculate SunnyBands
|
# Calculate SunnyBands
|
||||||
sunny = SunnyBands()
|
sunny = SunnyBands()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user