Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate tickers first #88

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def run_hedge_fund(
model_name: str = "gpt-4o",
model_provider: str = "OpenAI",
):
# Validate tickers first
from tools.api import validate_tickers

is_valid, invalid_tickers = validate_tickers(tickers)
if not is_valid:
print(f"{Fore.RED}Error: The following tickers are invalid: {', '.join(invalid_tickers)}{Style.RESET_ALL}")
return None

# Start progress tracking
progress.start()

Expand Down
18 changes: 18 additions & 0 deletions src/tools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,21 @@ def prices_to_df(prices: list[Price]) -> pd.DataFrame:
def get_price_data(ticker: str, start_date: str, end_date: str) -> pd.DataFrame:
prices = get_prices(ticker, start_date, end_date)
return prices_to_df(prices)


def validate_tickers(tickers: list[str]) -> tuple[bool, list[str]]:
"""
Validate a list of tickers against the available tickers list.
Returns a tuple of (is_valid, invalid_tickers).
"""
try:
response = requests.get("https://virattt.github.io/datasets/financials/available_tickers.json")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use https://www.sec.gov/files/company_tickers.json

Financial Datasets also has a /tickers/ endpoint for each API, but may be too heavy for now!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that URL is not working I get:

U.S. Securities and Exchange Commission

Automated access to our sites must comply with SEC.gov's Privacy and Security Policy.

Please visit www.sec.gov/developer for more developer resources and Fair Access guidelines.

Please visit www.sec.gov/privacy for more information on Privacy Policy.

Reference ID: 0.8477d917.1739045494.29945d84

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to add headers since its the SEC

if response.status_code != 200:
raise Exception(f"Error fetching valid tickers: {response.status_code} - {response.text}")

valid_tickers = {ticker["symbol"] for ticker in response.json()["tickers"]}
invalid_tickers = [ticker for ticker in tickers if ticker not in valid_tickers]

return len(invalid_tickers) == 0, invalid_tickers
except Exception as e:
raise Exception(f"Error validating tickers: {str(e)}")