Skip to content

Commit

Permalink
Updating yfinance to fix API, using try/catch for API
Browse files Browse the repository at this point in the history
  • Loading branch information
bvsam committed Nov 22, 2023
1 parent 6c66055 commit 7abc8e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ npm run dev

### Tips for Running

- If the api fails to validate basic tickers properly (e.g. AAPL), then you may have to upgrade `yfinance` to the latest version (using `pip install -U yfinance`).
- If the api fails to validate basic tickers properly (e.g. AAPL), then one of the following may be true
- the Yahoo Finance API that `yfinance` package is accessing may be temporarily down. This can be resolved by trying later
- you may have to upgrade `yfinance` to the latest version (using `pip install -U yfinance`).
- Additionally, the api might be overly strict as to what qualifies as a valid ticker. Change the dict keys checked for in `src/app/backend/api.py` if this is the case (set `required_keys` in function `validate_ticker`).

## Model Zoo
Expand Down
Binary file modified requirements.txt
Binary file not shown.
29 changes: 23 additions & 6 deletions src/app/backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import pathlib
import sys

import requests
import tensorflow as tf
Expand Down Expand Up @@ -30,6 +31,13 @@
invalid = set()


def eprint(*args, **kwargs):
"""
Helper function to allow for error messages to be easily printed to stderr
"""
print(*args, file=sys.stderr, **kwargs)


def validate_ticker(ticker):
if ticker in predictors:
return True
Expand All @@ -43,11 +51,8 @@ def validate_ticker(ticker):
"symbol",
"underlyingSymbol",
}
try:
info = yf.Ticker(ticker).info
except requests.exceptions.HTTPError:
return False

info = yf.Ticker(ticker).info
valid = required_keys.issubset(info.keys())
if valid:
predictor = RNNPredictor(
Expand All @@ -62,7 +67,13 @@ def validate_ticker(ticker):

@app.route("/info/<ticker>")
def get_ticker_info(ticker):
if not validate_ticker(ticker):
try:
valid = validate_ticker(ticker)
except requests.exceptions.HTTPError as e:
eprint(f"Server Error: {e}")
return {"ticker": ticker, "exists": False}, 500

if not valid:
return {"ticker": ticker, "exists": False}, 400
predictor = predictors[ticker]

Expand All @@ -79,7 +90,13 @@ def get_performance(ticker):
start_date = request.args.get("startDate")
end_date = request.args.get("endDate")

if start_date is None or end_date is None or not validate_ticker(ticker):
try:
valid = validate_ticker(ticker)
except requests.exceptions.HTTPError as e:
eprint(f"Server Error: {e}")
return {"success": False, "ticker": ticker}, 500

if start_date is None or end_date is None or not valid:
return {"success": False, "ticker": ticker}, 400

try:
Expand Down

0 comments on commit 7abc8e1

Please sign in to comment.