Files
smart_trading/utils.py
2025-11-29 14:07:45 -06:00

29 lines
1.1 KiB
Python

import requests
import pandas as pd
def get_slickchart_tickers(index: str) -> list[str]:
url = f"https://www.slickcharts.com/{index}"
user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0" # Default user-agent fails.
response = requests.get(url, headers={"User-Agent": user_agent})
data = pd.read_html(response.text, match="Symbol", index_col="Symbol")[0]
return data.index.to_list()
def get_sp500_tickers() -> list[str]:
try:
return get_slickchart_tickers("sp500")
except Exception as e:
print(f"Error fetching S&P 500 tickers. Using a sample list. Error: {e}")
# Fallback to a sample list if scraping fails
return ["AAPL", "MSFT", "AMZN", "GOOGL", "NVDA"]
def get_nasdaq100_tickers() -> list[str]:
try:
return get_slickchart_tickers("nasdaq100")
except Exception as e:
print(f"Error fetching NASDAQ 100 tickers. Using a sample list. Error: {e}")
# Fallback to a sample list if scraping fails
return ["AAPL", "MSFT", "AMZN", "GOOGL", "NVDA"]