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 all commits
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
24 changes: 23 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ def create_workflow(selected_analysts=None):
# Parse tickers from comma-separated string
tickers = [ticker.strip() for ticker in args.tickers.split(",")]

# Validate tickers before proceeding
from tools.api import validate_tickers
is_valid, invalid_tickers = validate_tickers(tickers)
if not is_valid:
valid_tickers = [ticker for ticker in tickers if ticker not in invalid_tickers]
if not valid_tickers:
print(f"{Fore.RED}Error: All provided tickers are invalid: {', '.join(invalid_tickers)}{Style.RESET_ALL}")
sys.exit(1)

print(f"{Fore.YELLOW}Warning: The following tickers are invalid: {', '.join(invalid_tickers)}{Style.RESET_ALL}")
proceed = questionary.confirm(
f"Do you want to proceed with only the valid tickers: {', '.join(valid_tickers)}?",
default=True
).ask()

if not proceed:
print("\nExiting...")
sys.exit(0)

print(f"\nProceeding with tickers: {', '.join(valid_tickers)}\n")
tickers = valid_tickers

# Select analysts
selected_analysts = None
choices = questionary.checkbox(
Expand All @@ -193,7 +215,7 @@ def create_workflow(selected_analysts=None):
sys.exit(0)
else:
selected_analysts = choices
print(f"\nSelected analysts: {', '.join(Fore.GREEN + choice.title().replace('_', ' ') + Style.RESET_ALL for choice in choices)}\n")
print(f"\nSelected analysts: {', '.join(choice.title().replace('_', ' ') for choice in choices)}\n")

# Select LLM model
model_choice = questionary.select(
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)}")