diff --git a/src/screener/t_sunnyband.py b/src/screener/t_sunnyband.py index 38c9537..d72afa9 100644 --- a/src/screener/t_sunnyband.py +++ b/src/screener/t_sunnyband.py @@ -35,25 +35,22 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv 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.date()}' AND '{end_date.date()}' + ORDER BY date ASC + """ else: table = "stock_prices" date_col = "window_start" - - 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.date()}' AND '{end_date.date()}' - """ - - # Add interval filtering for intraday data - if interval != "daily": minutes_map = { "5min": 5, "15min": 15, @@ -61,9 +58,21 @@ def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interv "1hour": 60 } minutes = minutes_map[interval] - query += f" AND (toMinute(fromUnixTimestamp(window_start)) % {minutes}) = 0" - - query += " ORDER BY date ASC" + + query = f""" + SELECT + fromUnixTimestamp({date_col}) as date, + open, + high, + low, + close, + volume + FROM stock_db.{table} + WHERE ticker = '{ticker}' + AND {date_col} BETWEEN toUnixTimestamp('{start_date.date()}') AND toUnixTimestamp('{end_date.date()}') + AND (toMinute(fromUnixTimestamp({date_col})) % {minutes}) = 0 + ORDER BY date ASC + """ try: result = client.query(query)