From bc7d20e1797cc61ebd00aa72658480bb3f0e7c66 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Thu, 6 Feb 2025 23:45:21 -0800 Subject: [PATCH] feat: Add get_current_price function to retrieve latest stock price --- src/screener/t_sunnyband.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/screener/t_sunnyband.py b/src/screener/t_sunnyband.py index 37e88fd..59c9468 100644 --- a/src/screener/t_sunnyband.py +++ b/src/screener/t_sunnyband.py @@ -30,6 +30,27 @@ def get_interval_choice() -> str: else: print("Invalid choice. Please try again.") +def get_current_price(ticker: str) -> float: + """Get the most recent price for a ticker from stock_prices table""" + client = create_client() + + query = f""" + SELECT close + FROM stock_db.stock_prices + WHERE ticker = '{ticker}' + ORDER BY window_start DESC + LIMIT 1 + """ + + try: + result = client.query(query) + if result.result_rows: + return float(result.result_rows[0][0]) + return None + except Exception as e: + print(f"Error getting current price for {ticker}: {str(e)}") + return None + def get_stock_data(ticker: str, start_date: datetime, end_date: datetime, interval: str) -> pd.DataFrame: """Fetch stock data from the database""" client = create_client()