Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,17 @@
|
|
| 2 |
import os
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
| 5 |
-
|
| 6 |
# 數據處理
|
| 7 |
import pandas as pd
|
| 8 |
import numpy as np
|
| 9 |
import yfinance as yf
|
| 10 |
|
| 11 |
# Dash & Plotly
|
| 12 |
-
from dash import Dash, dcc, html,
|
| 13 |
import dash
|
| 14 |
import plotly.express as px
|
| 15 |
import plotly.graph_objects as go
|
| 16 |
from plotly.subplots import make_subplots
|
| 17 |
-
import json
|
| 18 |
-
from predictor_logic import StockPredictor
|
| 19 |
-
try:
|
| 20 |
-
stock_predictor = StockPredictor()
|
| 21 |
-
print("模型和歷史數據載入成功!")
|
| 22 |
-
except FileNotFoundError as e:
|
| 23 |
-
print(f"初始化預測器失敗:{e}。請確認您的檔案路徑正確。")
|
| 24 |
-
stock_predictor = None
|
| 25 |
|
| 26 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 27 |
TAIWAN_STOCKS = {
|
|
@@ -590,82 +581,28 @@ app.layout = html.Div([
|
|
| 590 |
})
|
| 591 |
])
|
| 592 |
|
| 593 |
-
# 台指期獨立預測回調函數
|
| 594 |
-
@callback(
|
| 595 |
[dash.dependencies.Output('taiex-prediction-results', 'children'),
|
| 596 |
dash.dependencies.Output('taiex-prediction-chart', 'figure')],
|
| 597 |
-
[dash.dependencies.Input('taiex-
|
| 598 |
)
|
| 599 |
-
def update_taiex_prediction(
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
if not n_clicks or not stock_predictor:
|
| 605 |
-
# 由於需要返回兩個值 (文字和圖表),當沒有點擊時,回傳空值或預設值
|
| 606 |
-
return html.Div("請點擊按鈕以載入預測結果。", style={'text-align': 'center', 'margin-top': '20px'}), go.Figure()
|
| 607 |
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
if latest_data.empty:
|
| 614 |
-
raise ValueError("無法取得最新的台指期資料。")
|
| 615 |
-
|
| 616 |
-
# 取得最後一筆數據的日期,作為 predict 函式的輸入
|
| 617 |
-
last_date = latest_data.index[-1]
|
| 618 |
-
|
| 619 |
-
# 呼叫預測器物件的 predict 方法
|
| 620 |
-
predicted_price = stock_predictor.predict(last_date.strftime('%Y-%m-%d'))
|
| 621 |
-
|
| 622 |
-
# --- 繪製預測圖表 ---
|
| 623 |
-
|
| 624 |
-
# 建立一個 Plotly 圖表物件
|
| 625 |
-
fig = go.Figure()
|
| 626 |
-
|
| 627 |
-
# 添加歷史收盤價
|
| 628 |
-
fig.add_trace(go.Scatter(
|
| 629 |
-
x=latest_data.index,
|
| 630 |
-
y=latest_data['Close'],
|
| 631 |
-
mode='lines',
|
| 632 |
-
name='歷史收盤價',
|
| 633 |
-
line=dict(color='royalblue', width=2)
|
| 634 |
-
))
|
| 635 |
-
|
| 636 |
-
# 添加預測點
|
| 637 |
-
fig.add_trace(go.Scatter(
|
| 638 |
-
x=[last_date + pd.Timedelta(days=1)],
|
| 639 |
-
y=[predicted_price],
|
| 640 |
-
mode='markers+text',
|
| 641 |
-
name='預測價格',
|
| 642 |
-
text=[f'{predicted_price:.2f}'],
|
| 643 |
-
textposition='top center',
|
| 644 |
-
marker=dict(size=10, color='red')
|
| 645 |
-
))
|
| 646 |
-
|
| 647 |
-
# 更新圖表佈局
|
| 648 |
-
fig.update_layout(
|
| 649 |
-
title='台指期歷史價格與預測結果',
|
| 650 |
-
xaxis_title='日期',
|
| 651 |
-
yaxis_title='收盤價',
|
| 652 |
-
template='plotly_white',
|
| 653 |
-
hovermode='x unified'
|
| 654 |
-
)
|
| 655 |
-
|
| 656 |
-
# --- 準備文字結果 ---
|
| 657 |
-
|
| 658 |
-
result_div = html.Div([
|
| 659 |
-
html.P("【XGBoost 模型預測】", style={'font-size': '1.5em', 'font-weight': 'bold', 'color': '#28a745'}),
|
| 660 |
-
html.P(f"最後已知日期:{last_date.strftime('%Y-%m-%d')}", style={'font-size': '1.2em', 'font-weight': 'bold'}),
|
| 661 |
-
html.P(f"預測隔日收盤價:{predicted_price:.2f}", style={'font-size': '1.5em', 'font-weight': 'bold', 'color': '#17a2b8'})
|
| 662 |
-
], style={'text-align': 'center', 'margin-top': '20px'})
|
| 663 |
-
|
| 664 |
-
return result_div, fig
|
| 665 |
-
|
| 666 |
-
except (ValueError, IndexError) as e:
|
| 667 |
-
return html.Div(f"預測失敗:{e}", style={'text-align': 'center', 'margin-top': '20px', 'color': 'red'}), go.Figure()
|
| 668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
|
| 670 |
# --- 主要修改處:計算預測路徑 ---
|
| 671 |
# 1. 定義不同預測天期所包含的中間節點
|
|
|
|
| 2 |
import os
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
|
|
|
|
| 5 |
# 數據處理
|
| 6 |
import pandas as pd
|
| 7 |
import numpy as np
|
| 8 |
import yfinance as yf
|
| 9 |
|
| 10 |
# Dash & Plotly
|
| 11 |
+
from dash import Dash, dcc, html, callback
|
| 12 |
import dash
|
| 13 |
import plotly.express as px
|
| 14 |
import plotly.graph_objects as go
|
| 15 |
from plotly.subplots import make_subplots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 18 |
TAIWAN_STOCKS = {
|
|
|
|
| 581 |
})
|
| 582 |
])
|
| 583 |
|
| 584 |
+
# 台指期獨立預測回調函數 (新版本)
|
| 585 |
+
@app.callback(
|
| 586 |
[dash.dependencies.Output('taiex-prediction-results', 'children'),
|
| 587 |
dash.dependencies.Output('taiex-prediction-chart', 'figure')],
|
| 588 |
+
[dash.dependencies.Input('taiex-prediction-period', 'value')]
|
| 589 |
)
|
| 590 |
+
def update_taiex_prediction(predict_days):
|
| 591 |
+
# 獲取台指期歷史資料
|
| 592 |
+
data = get_stock_data('^TWII', '2y')
|
| 593 |
+
if data.empty:
|
| 594 |
+
return html.Div("無法獲取台指期資料"), {}
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
+
# 執行最終日的預測,用於顯示在結果卡片上
|
| 597 |
+
final_prediction = simple_lstm_predict(data, predict_days)
|
| 598 |
+
if final_prediction is None:
|
| 599 |
+
return html.Div("資料不足,無法進行預測"), {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
|
| 601 |
+
current_price = data['Close'].iloc[-1]
|
| 602 |
+
last_date = data.index[-1]
|
| 603 |
+
predicted_price = final_prediction['predicted_price']
|
| 604 |
+
change_pct = final_prediction['change_pct']
|
| 605 |
+
confidence = final_prediction['confidence']
|
| 606 |
|
| 607 |
# --- 主要修改處:計算預測路徑 ---
|
| 608 |
# 1. 定義不同預測天期所包含的中間節點
|