diff --git a/src/screener/t_atr_ema.py b/src/screener/t_atr_ema.py index a421324..aa9909c 100644 --- a/src/screener/t_atr_ema.py +++ b/src/screener/t_atr_ema.py @@ -65,10 +65,9 @@ def run_atr_ema_scanner(min_price: float, max_price: float, min_volume: int, por start_ts = int(start_date.timestamp() * 1000000000) end_ts = int(end_date.timestamp() * 1000000000) - client = create_client() - try: - query = f""" + with create_client() as client: + query = f""" WITH latest_data AS ( SELECT ticker, diff --git a/src/screener/t_atr_ema_v2.py b/src/screener/t_atr_ema_v2.py index bdd19c6..c8a273e 100644 --- a/src/screener/t_atr_ema_v2.py +++ b/src/screener/t_atr_ema_v2.py @@ -73,11 +73,10 @@ def run_atr_ema_scanner_v2(min_price: float, max_price: float, min_volume: int, start_ts = int(start_date.timestamp() * 1000000000) end_ts = int(end_date.timestamp() * 1000000000) - client = create_client() - try: - # Query to get qualified stocks - query = f""" + with create_client() as client: + # Query to get qualified stocks + query = f""" WITH latest_data AS ( SELECT ticker, diff --git a/src/screener/t_sunnyband.py b/src/screener/t_sunnyband.py index fe2ecf9..1c9a931 100644 --- a/src/screener/t_sunnyband.py +++ b/src/screener/t_sunnyband.py @@ -50,14 +50,14 @@ def check_entry_signal(df: pd.DataFrame) -> list: def get_valid_tickers(min_price: float, max_price: float, min_volume: int, interval: str) -> list: """Get tickers that meet the price and volume criteria""" - client = create_client() - # Get the most recent trading day today = datetime.now().date() yesterday = today - timedelta(days=1) - # First get valid tickers from daily data - daily_query = f""" + try: + with create_client() as client: + # First get valid tickers from daily data + daily_query = f""" SELECT DISTINCT ticker FROM stock_db.stock_prices_daily WHERE date = '{yesterday}' @@ -104,9 +104,9 @@ 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: - # Construct query - client = create_client() - today = datetime.now().date() + with create_client() as client: + # Construct query + today = datetime.now().date() start_date = today - timedelta(days=60) if interval == "daily": @@ -209,14 +209,14 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int, portf start_date, end_date = get_date_range() # First get qualified stocks from database - client = create_client() - # Convert dates to Unix timestamp in nanoseconds end_ts = int(end_date.timestamp() * 1000000000) start_ts = int(start_date.timestamp() * 1000000000) - # Query to get stocks meeting criteria with their latest data - query = f""" + try: + with create_client() as client: + # Query to get stocks meeting criteria with their latest data + query = f""" WITH latest_data AS ( SELECT ticker,