zyy0815 commited on
Commit
eacf067
·
verified ·
1 Parent(s): d4bda50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -23,8 +23,8 @@ st.markdown("### Group Members")
23
  st.markdown("""
24
  - Zhang, Yuanyuan [yz133@illinois.edu](mailto:yz133@illinois.edu)
25
  - Peng, Yongzhen [ypeng16@illinois.edu](mailto:ypeng16@illinois.edu)
26
- - Wei, Jiahe[wei51@illinois.edu](mailto:wei51@illinois.edu)
27
- - Wang, Tiannuo[tiannuo3@illinois.edu](mailto:tiannuo3@illinois.edu)
28
  """)
29
 
30
 
@@ -55,7 +55,7 @@ st.markdown(
55
 
56
 
57
 
58
- # 获取股票数据
59
  end_date = datetime.now().strftime('%Y-%m-%d')
60
  start_date = "2022-01-01"
61
 
@@ -77,44 +77,44 @@ company_names = {
77
  tickers = list(company_names.keys())
78
  stock_data = yf.download(tickers, start=start_date, end=end_date)
79
 
80
- # 处理 MultiIndex 数据
81
  if isinstance(stock_data.columns, pd.MultiIndex):
82
  stock_data.columns = ['_'.join(col).strip() for col in stock_data.columns]
83
 
84
  stock_data.reset_index(inplace=True)
85
 
86
- # 添加月度聚合数据
87
  stock_data["Month"] = stock_data["Date"].dt.to_period("M")
88
  monthly_data = stock_data.groupby("Month").mean()
89
 
90
- # Streamlit 应用
91
  st.title("Stock Dashboard")
92
 
93
- # 用户选择
94
  selected_stock = st.selectbox("Select a Stock:", options=company_names.keys())
95
  metric1 = st.selectbox("Select Metric 1:", options=["Open", "High", "Low", "Close", "Adj Close", "Volume"])
96
  metric2 = st.selectbox("Select Metric 2:", options=["Open", "High", "Low", "Close", "Adj Close", "Volume"])
97
 
98
- # 数据过滤
99
  dates = stock_data["Date"]
100
  metric1_data = stock_data[f"{metric1}_{selected_stock}"]
101
  metric2_data = stock_data[f"{metric2}_{selected_stock}"]
102
 
103
- # 绘制折线图
104
  fig1 = go.Figure()
105
  fig1.add_trace(go.Scatter(x=dates, y=metric1_data, mode='lines', name=metric1))
106
  fig1.update_layout(title=f"{metric1} Over Time for {selected_stock}", xaxis_title="Date", yaxis_title=metric1)
107
 
108
- # 绘制散点图
109
  fig2 = go.Figure()
110
  fig2.add_trace(go.Scatter(x=metric1_data, y=metric2_data, mode='markers', name=f"{metric1} vs {metric2}"))
111
  fig2.update_layout(title=f"{metric1} vs {metric2} for {selected_stock}", xaxis_title=metric1, yaxis_title=metric2)
112
 
113
- # 显示图表
114
  st.plotly_chart(fig1)
115
  st.plotly_chart(fig2)
116
 
117
- # 热力图显示月度平均数据
118
  metrics10 = ["Adj Close", "Open", "High", "Low"]
119
 
120
  for metric in metrics10:
@@ -187,7 +187,7 @@ st.markdown("[GitHub Repository: IS_445_FINAL](https://github.com/WJHWJH1208/IS_
187
 
188
 
189
 
190
- # 添加相关性热力图
191
  metrics = ["Adj Close", "Volume", "Open", "High", "Low"]
192
  st.header("Correlation Heatmap")
193
  metric_corr = st.selectbox("Select a Metric for Correlation Analysis:", metrics)
@@ -196,24 +196,24 @@ cols = [col for col in stock_data.columns if col.startswith(metric_corr + "_")]
196
  if cols:
197
  corr = stock_data[cols].corr()
198
 
199
- # 绘制相关性热力图
200
  fig, ax = plt.subplots(figsize=(10, 8))
201
  im = ax.imshow(corr, cmap="coolwarm", vmin=-1, vmax=1)
202
 
203
- # 设置标签
204
  tickers = [col.replace(metric_corr + "_", "") for col in cols]
205
  ax.set_xticks(np.arange(len(tickers)))
206
  ax.set_yticks(np.arange(len(tickers)))
207
  ax.set_xticklabels(tickers, rotation=45, ha="right")
208
  ax.set_yticklabels(tickers)
209
 
210
- # 在热力图上显示相关性值
211
  for i in range(len(tickers)):
212
  for j in range(len(tickers)):
213
  text_color = "white" if abs(corr.iloc[i, j]) > 0.5 else "black"
214
  ax.text(j, i, f"{corr.iloc[i, j]:.2f}", ha="center", va="center", color=text_color)
215
 
216
- # 添加颜色条
217
  cbar = plt.colorbar(im, ax=ax)
218
  cbar.set_label("Correlation Coefficient")
219
 
 
23
  st.markdown("""
24
  - Zhang, Yuanyuan [yz133@illinois.edu](mailto:yz133@illinois.edu)
25
  - Peng, Yongzhen [ypeng16@illinois.edu](mailto:ypeng16@illinois.edu)
26
+ - Wei, Jiahe [wei51@illinois.edu](mailto:wei51@illinois.edu)
27
+ - Wang, Tiannuo [tiannuo3@illinois.edu](mailto:tiannuo3@illinois.edu)
28
  """)
29
 
30
 
 
55
 
56
 
57
 
58
+
59
  end_date = datetime.now().strftime('%Y-%m-%d')
60
  start_date = "2022-01-01"
61
 
 
77
  tickers = list(company_names.keys())
78
  stock_data = yf.download(tickers, start=start_date, end=end_date)
79
 
80
+
81
  if isinstance(stock_data.columns, pd.MultiIndex):
82
  stock_data.columns = ['_'.join(col).strip() for col in stock_data.columns]
83
 
84
  stock_data.reset_index(inplace=True)
85
 
86
+
87
  stock_data["Month"] = stock_data["Date"].dt.to_period("M")
88
  monthly_data = stock_data.groupby("Month").mean()
89
 
90
+
91
  st.title("Stock Dashboard")
92
 
93
+
94
  selected_stock = st.selectbox("Select a Stock:", options=company_names.keys())
95
  metric1 = st.selectbox("Select Metric 1:", options=["Open", "High", "Low", "Close", "Adj Close", "Volume"])
96
  metric2 = st.selectbox("Select Metric 2:", options=["Open", "High", "Low", "Close", "Adj Close", "Volume"])
97
 
98
+
99
  dates = stock_data["Date"]
100
  metric1_data = stock_data[f"{metric1}_{selected_stock}"]
101
  metric2_data = stock_data[f"{metric2}_{selected_stock}"]
102
 
103
+
104
  fig1 = go.Figure()
105
  fig1.add_trace(go.Scatter(x=dates, y=metric1_data, mode='lines', name=metric1))
106
  fig1.update_layout(title=f"{metric1} Over Time for {selected_stock}", xaxis_title="Date", yaxis_title=metric1)
107
 
108
+
109
  fig2 = go.Figure()
110
  fig2.add_trace(go.Scatter(x=metric1_data, y=metric2_data, mode='markers', name=f"{metric1} vs {metric2}"))
111
  fig2.update_layout(title=f"{metric1} vs {metric2} for {selected_stock}", xaxis_title=metric1, yaxis_title=metric2)
112
 
113
+
114
  st.plotly_chart(fig1)
115
  st.plotly_chart(fig2)
116
 
117
+
118
  metrics10 = ["Adj Close", "Open", "High", "Low"]
119
 
120
  for metric in metrics10:
 
187
 
188
 
189
 
190
+
191
  metrics = ["Adj Close", "Volume", "Open", "High", "Low"]
192
  st.header("Correlation Heatmap")
193
  metric_corr = st.selectbox("Select a Metric for Correlation Analysis:", metrics)
 
196
  if cols:
197
  corr = stock_data[cols].corr()
198
 
199
+
200
  fig, ax = plt.subplots(figsize=(10, 8))
201
  im = ax.imshow(corr, cmap="coolwarm", vmin=-1, vmax=1)
202
 
203
+
204
  tickers = [col.replace(metric_corr + "_", "") for col in cols]
205
  ax.set_xticks(np.arange(len(tickers)))
206
  ax.set_yticks(np.arange(len(tickers)))
207
  ax.set_xticklabels(tickers, rotation=45, ha="right")
208
  ax.set_yticklabels(tickers)
209
 
210
+
211
  for i in range(len(tickers)):
212
  for j in range(len(tickers)):
213
  text_color = "white" if abs(corr.iloc[i, j]) > 0.5 else "black"
214
  ax.text(j, i, f"{corr.iloc[i, j]:.2f}", ha="center", va="center", color=text_color)
215
 
216
+
217
  cbar = plt.colorbar(im, ax=ax)
218
  cbar.set_label("Correlation Coefficient")
219