diff --git a/src/screener/t_sunnyband.py b/src/screener/t_sunnyband.py index d0d6256..add6fac 100644 --- a/src/screener/t_sunnyband.py +++ b/src/screener/t_sunnyband.py @@ -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()}") try: - # Get price data - df = get_stock_data(ticker, start_date, end_date, interval) + # Construct query + client = create_client() + today = datetime.now().date() + start_date = today - timedelta(days=60) - if df.empty: - print("No data found for this stock") + if interval == "daily": + 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 - print("\nData Sample:") - print("First 5 rows:") - print(df.head().to_string()) - print("\nLast 5 rows:") - print(df.tail().to_string()) + # Convert to DataFrame + df = pd.DataFrame( + result.result_rows, + columns=['date', 'open', 'high', 'low', 'close', 'volume'] + ) - 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 sunny = SunnyBands()