Spaces:
Running
Running
Commit
·
585716d
1
Parent(s):
c4c99cc
added error handling
Browse files
app.py
CHANGED
@@ -12,11 +12,13 @@ load_dotenv()
|
|
12 |
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY")
|
13 |
|
14 |
def fetch_stock_data(ticker):
|
|
|
15 |
stock = yf.Ticker(ticker)
|
16 |
hist = stock.history(period="1mo")
|
17 |
return hist.tail(5)
|
18 |
|
19 |
def fetch_stock_news(ticker, NEWSAPI_KEY):
|
|
|
20 |
api_url = f"https://newsapi.org/v2/everything?q={ticker}&apiKey={NEWSAPI_KEY}"
|
21 |
response = requests.get(api_url)
|
22 |
if response.status_code == 200:
|
@@ -26,6 +28,7 @@ def fetch_stock_news(ticker, NEWSAPI_KEY):
|
|
26 |
return [{"error": "Unable to fetch news."}]
|
27 |
|
28 |
def calculate_moving_average(ticker, window=5):
|
|
|
29 |
stock = yf.Ticker(ticker)
|
30 |
hist = stock.history(period="1mo")
|
31 |
hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
|
|
|
12 |
NEWSAPI_KEY = os.getenv("NEWSAPI_KEY")
|
13 |
|
14 |
def fetch_stock_data(ticker):
|
15 |
+
print("fetching stock data")
|
16 |
stock = yf.Ticker(ticker)
|
17 |
hist = stock.history(period="1mo")
|
18 |
return hist.tail(5)
|
19 |
|
20 |
def fetch_stock_news(ticker, NEWSAPI_KEY):
|
21 |
+
print("fetching stock news")
|
22 |
api_url = f"https://newsapi.org/v2/everything?q={ticker}&apiKey={NEWSAPI_KEY}"
|
23 |
response = requests.get(api_url)
|
24 |
if response.status_code == 200:
|
|
|
28 |
return [{"error": "Unable to fetch news."}]
|
29 |
|
30 |
def calculate_moving_average(ticker, window=5):
|
31 |
+
print("calculating moving average")
|
32 |
stock = yf.Ticker(ticker)
|
33 |
hist = stock.history(period="1mo")
|
34 |
hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
|