feat: Add time interval selection for SunnyBand scanner

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 22:00:26 -08:00
parent 1b6e93badf
commit 9c440f08da
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -3,23 +3,67 @@ import pandas as pd
from db.db_connection import create_client
from indicators.sunny_bands import SunnyBands
def get_stock_data(ticker: str, start_date: datetime, end_date: datetime) -> pd.DataFrame:
def get_interval_choice() -> str:
"""Get user's preferred time interval"""
print("\nSelect Time Interval:")
print("1. Daily")
print("2. 5 minute")
print("3. 15 minute")
print("4. 30 minute")
print("5. 1 hour")
while True:
choice = input("\nEnter your choice (1-5): ")
if choice == "1":
return "daily"
elif choice == "2":
return "5min"
elif choice == "3":
return "15min"
elif choice == "4":
return "30min"
elif choice == "5":
return "1hour"
else:
print("Invalid choice. Please try again.")
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()
# Select appropriate table based on interval
if interval == "daily":
table = "stock_prices_daily"
date_col = "date"
else:
table = "stock_prices"
date_col = "window_start"
query = f"""
SELECT
date,
{date_col} as date,
open,
high,
low,
close,
volume
FROM stock_db.stock_prices_daily
FROM stock_db.{table}
WHERE ticker = '{ticker}'
AND date BETWEEN '{start_date.date()}' AND '{end_date.date()}'
ORDER BY date ASC
"""
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,
"30min": 30,
"1hour": 60
}
minutes = minutes_map[interval]
query += f" AND (toMinute(fromUnixTimestamp(window_start)) % {minutes}) = 0"
query += " ORDER BY date ASC"
try:
result = client.query(query)
@ -57,9 +101,15 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int) -> No
print(f"\nInitializing scan for stocks between ${min_price:.2f} and ${max_price:.2f}")
print(f"Minimum volume: {min_volume:,}")
# Get date range (60 days of data for calculations)
# Get user's preferred interval
interval = get_interval_choice()
# Adjust date range based on interval
end_date = datetime.now()
start_date = end_date - timedelta(days=60)
if interval == "daily":
start_date = end_date - timedelta(days=60)
else:
start_date = end_date - timedelta(days=5) # Less history needed for intraday
# Get valid tickers
print("\nFetching qualified stocks...")
@ -93,7 +143,7 @@ def run_sunny_scanner(min_price: float, max_price: float, min_volume: int) -> No
try:
# Get price data
df = get_stock_data(ticker, start_date, end_date)
df = get_stock_data(ticker, start_date, end_date, interval)
if df.empty:
continue