Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,14 @@
|
|
1 |
import streamlit as st
|
2 |
-
import yfinance as yf
|
3 |
import plotly.graph_objects as go
|
4 |
from plotly.subplots import make_subplots
|
5 |
import pandas as pd
|
6 |
import twstock
|
7 |
from datetime import datetime, timedelta
|
8 |
|
9 |
-
def plot_stock_data(stock_symbols
|
10 |
"""
|
11 |
繪製股票價格圖表
|
12 |
:param stock_symbols: 股票代號列表
|
13 |
-
:param period: 時間區間
|
14 |
:return: Plotly figure
|
15 |
"""
|
16 |
# 創建子圖
|
@@ -26,13 +24,28 @@ def plot_stock_data(stock_symbols, period='1y'):
|
|
26 |
for idx, symbol in enumerate(stock_symbols, 1):
|
27 |
try:
|
28 |
# 獲取股票數據
|
29 |
-
stock =
|
30 |
-
|
31 |
|
32 |
-
if
|
33 |
st.warning(f"無法找到 {symbol} 的股票數據")
|
34 |
continue
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# 添加蠟燭圖
|
37 |
fig.add_trace(
|
38 |
go.Candlestick(
|
@@ -59,7 +72,7 @@ def plot_stock_data(stock_symbols, period='1y'):
|
|
59 |
)
|
60 |
|
61 |
# 添加移動平均線
|
62 |
-
for ma_days in [5, 20
|
63 |
ma = df['Close'].rolling(window=ma_days).mean()
|
64 |
fig.add_trace(
|
65 |
go.Scatter(
|
@@ -136,15 +149,8 @@ def main():
|
|
136 |
# 股票代碼輸入
|
137 |
stock_input = st.text_input(
|
138 |
"股票代號 (用逗號分隔)",
|
139 |
-
value="2330
|
140 |
-
placeholder="例如: 2330
|
141 |
-
)
|
142 |
-
|
143 |
-
# 時間區間選擇
|
144 |
-
period_select = st.selectbox(
|
145 |
-
"選擇時間區間",
|
146 |
-
["1mo", "3mo", "6mo", "1y", "2y", "5y", "max"],
|
147 |
-
index=3 # 預設為 1y
|
148 |
)
|
149 |
|
150 |
# 股票分析頁籤
|
@@ -155,10 +161,9 @@ def main():
|
|
155 |
if st.button("繪製股價走勢圖"):
|
156 |
# 處理股票代號
|
157 |
stocks = [s.strip() for s in stock_input.split(',')]
|
158 |
-
stocks = [f"{s}.TW" if not s.endswith('.TW') and s.isdigit() else s for s in stocks]
|
159 |
|
160 |
# 創建圖表
|
161 |
-
fig = plot_stock_data(stocks
|
162 |
st.plotly_chart(fig, use_container_width=True)
|
163 |
|
164 |
with tab2:
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import plotly.graph_objects as go
|
3 |
from plotly.subplots import make_subplots
|
4 |
import pandas as pd
|
5 |
import twstock
|
6 |
from datetime import datetime, timedelta
|
7 |
|
8 |
+
def plot_stock_data(stock_symbols):
|
9 |
"""
|
10 |
繪製股票價格圖表
|
11 |
:param stock_symbols: 股票代號列表
|
|
|
12 |
:return: Plotly figure
|
13 |
"""
|
14 |
# 創建子圖
|
|
|
24 |
for idx, symbol in enumerate(stock_symbols, 1):
|
25 |
try:
|
26 |
# 獲取股票數據
|
27 |
+
stock = twstock.Stock(symbol)
|
28 |
+
recent_data = stock.fetch_31() # 抓取最近 31 天的交易數據
|
29 |
|
30 |
+
if not recent_data:
|
31 |
st.warning(f"無法找到 {symbol} 的股票數據")
|
32 |
continue
|
33 |
|
34 |
+
# 準備繪圖數據
|
35 |
+
df = pd.DataFrame([
|
36 |
+
{
|
37 |
+
"Date": data.date,
|
38 |
+
"Open": data.open,
|
39 |
+
"High": data.high,
|
40 |
+
"Low": data.low,
|
41 |
+
"Close": data.close,
|
42 |
+
"Volume": data.transaction
|
43 |
+
}
|
44 |
+
for data in recent_data
|
45 |
+
])
|
46 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
47 |
+
df.set_index('Date', inplace=True)
|
48 |
+
|
49 |
# 添加蠟燭圖
|
50 |
fig.add_trace(
|
51 |
go.Candlestick(
|
|
|
72 |
)
|
73 |
|
74 |
# 添加移動平均線
|
75 |
+
for ma_days in [5, 20]:
|
76 |
ma = df['Close'].rolling(window=ma_days).mean()
|
77 |
fig.add_trace(
|
78 |
go.Scatter(
|
|
|
149 |
# 股票代碼輸入
|
150 |
stock_input = st.text_input(
|
151 |
"股票代號 (用逗號分隔)",
|
152 |
+
value="2330,2454",
|
153 |
+
placeholder="例如: 2330,2454"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
)
|
155 |
|
156 |
# 股票分析頁籤
|
|
|
161 |
if st.button("繪製股價走勢圖"):
|
162 |
# 處理股票代號
|
163 |
stocks = [s.strip() for s in stock_input.split(',')]
|
|
|
164 |
|
165 |
# 創建圖表
|
166 |
+
fig = plot_stock_data(stocks)
|
167 |
st.plotly_chart(fig, use_container_width=True)
|
168 |
|
169 |
with tab2:
|