fix: Properly use context manager for database connections in scanners

This commit is contained in:
Bobby (aider) 2025-02-08 20:54:31 -08:00
parent 865438bbf8
commit 4984d4944a
3 changed files with 16 additions and 18 deletions

View File

@ -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) start_ts = int(start_date.timestamp() * 1000000000)
end_ts = int(end_date.timestamp() * 1000000000) end_ts = int(end_date.timestamp() * 1000000000)
client = create_client()
try: try:
query = f""" with create_client() as client:
query = f"""
WITH latest_data AS ( WITH latest_data AS (
SELECT SELECT
ticker, ticker,

View File

@ -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) start_ts = int(start_date.timestamp() * 1000000000)
end_ts = int(end_date.timestamp() * 1000000000) end_ts = int(end_date.timestamp() * 1000000000)
client = create_client()
try: try:
# Query to get qualified stocks with create_client() as client:
query = f""" # Query to get qualified stocks
query = f"""
WITH latest_data AS ( WITH latest_data AS (
SELECT SELECT
ticker, ticker,

View File

@ -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: 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""" """Get tickers that meet the price and volume criteria"""
client = create_client()
# Get the most recent trading day # Get the most recent trading day
today = datetime.now().date() today = datetime.now().date()
yesterday = today - timedelta(days=1) yesterday = today - timedelta(days=1)
# First get valid tickers from daily data try:
daily_query = f""" with create_client() as client:
# First get valid tickers from daily data
daily_query = f"""
SELECT DISTINCT ticker SELECT DISTINCT ticker
FROM stock_db.stock_prices_daily FROM stock_db.stock_prices_daily
WHERE date = '{yesterday}' 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()}") print(f"Date Range: {start_date.date()} to {end_date.date()}")
try: try:
# Construct query with create_client() as client:
client = create_client() # Construct query
today = datetime.now().date() today = datetime.now().date()
start_date = today - timedelta(days=60) start_date = today - timedelta(days=60)
if interval == "daily": 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() start_date, end_date = get_date_range()
# First get qualified stocks from database # First get qualified stocks from database
client = create_client()
# Convert dates to Unix timestamp in nanoseconds # Convert dates to Unix timestamp in nanoseconds
end_ts = int(end_date.timestamp() * 1000000000) end_ts = int(end_date.timestamp() * 1000000000)
start_ts = int(start_date.timestamp() * 1000000000) start_ts = int(start_date.timestamp() * 1000000000)
# Query to get stocks meeting criteria with their latest data try:
query = f""" with create_client() as client:
# Query to get stocks meeting criteria with their latest data
query = f"""
WITH latest_data AS ( WITH latest_data AS (
SELECT SELECT
ticker, ticker,