Skip to content

Commit ba1663c

Browse files
committed
chore: add improvements
1 parent 08d4b8a commit ba1663c

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

.github/workflows/python-app.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
run: |
2424
python -m pip install --upgrade pip
2525
pip install -r requirements.txt
26+
- name: Run with black
27+
run:
28+
pip install black
29+
black . --max-line-length=78
2630
- name: Lint with flake8
2731
run: |
2832
pip install flake8

src/__init__.py

Whitespace-only changes.

src/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
data = pd.read_csv("data/sample_stock.csv")
2828
data.columns = data.columns.str.strip() # Remove any spaces
2929
print(data.columns)
30-
data = pd.read_csv("data/sample_stock.csv", parse_dates=["Date"], index_col="Date")
30+
data = pd.read_csv(
31+
"data/sample_stock.csv", parse_dates=["Date"], index_col="Date"
32+
)
3133
st.line_chart(data["Close"])
3234
st.subheader("Latest News Sentiment")
3335
sentiment_results = []

src/fetch_news.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77

88

99
def fetch_news(query, page_size=10):
10-
"""Fetch latest news articles using NewsAPI"""
1110
api_key = os.getenv("NEWS_API_KEY")
1211
if not api_key:
13-
raise ValueError("Please set NEWS_API_KEY in .env file")
14-
url = (
15-
f"https://newsapi.org/v2/everything?q={query}&"
16-
f"pageSize={page_size}&apiKey={api_key}"
17-
)
18-
response = requests.get(url)
12+
raise RuntimeError("NEWS_API_KEY not found")
13+
14+
url = "https://newsapi.org/v2/everything"
15+
params = {
16+
"q": query,
17+
"pageSize": page_size,
18+
"apiKey": api_key,
19+
"language": "en",
20+
"sortBy": "publishedAt",
21+
}
22+
23+
headers = {"User-Agent": "StockSentimentAnalyzer/1.0"}
24+
25+
response = requests.get(url, params=params, headers=headers)
26+
response.raise_for_status()
27+
1928
articles = response.json().get("articles", [])
20-
return [(a["title"], a["description"]) for a in articles]
29+
return [(a["title"], a.get("description", "")) for a in articles]

src/visualization.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
import matplotlib.pyplot as plt
2+
import matplotlib.dates as mdates
23
import streamlit as st
34

45

56
def plot_stock(data, ticker):
67
plt.figure(figsize=(10, 5))
7-
plt.plot(data.index, data["Close"], label=f"{ticker} Close Price")
8-
plt.xlabel("Time")
8+
9+
# Get today's high, low, and current price
10+
today_high = data["High"].iloc[-1]
11+
today_low = data["Low"].iloc[-1]
12+
current_price = data["Close"].iloc[-1]
13+
14+
# Plot current price as a horizontal line
15+
plt.hlines(
16+
current_price,
17+
xmin=data.index[0],
18+
xmax=data.index[-1],
19+
colors="red",
20+
linewidth=2,
21+
label=f"Current Price: {current_price:.2f}",
22+
)
23+
24+
# Set y-axis limits to today's low/high with padding
25+
plt.ylim(today_low * 0.995, today_high * 1.005)
26+
27+
# Format x-axis with ticks every 2 hours
28+
ax = plt.gca()
29+
30+
# 2-hour interval
31+
ax.xaxis.set_major_locator(mdates.HourLocator(interval=2))
32+
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
33+
plt.xticks(rotation=45)
34+
35+
# Annotate current price
36+
plt.text(
37+
data.index[len(data) // 2],
38+
current_price * 1.001,
39+
f"{current_price:.2f}",
40+
color="red",
41+
fontsize=12,
42+
ha="center",
43+
)
44+
945
plt.ylabel("Price")
10-
plt.title(f"{ticker} Stock Price")
46+
plt.xlabel("Time (Market Hours)")
47+
plt.title(f"{ticker} Price Today")
1148
plt.legend()
1249
st.pyplot(plt)
1350

0 commit comments

Comments
 (0)