Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -533,7 +533,7 @@ app.layout = html.Div([
|
|
| 533 |
})
|
| 534 |
])
|
| 535 |
|
| 536 |
-
# 台指期獨立預測回調函數
|
| 537 |
@app.callback(
|
| 538 |
[dash.dependencies.Output('taiex-prediction-results', 'children'),
|
| 539 |
dash.dependencies.Output('taiex-prediction-chart', 'figure')],
|
|
@@ -545,17 +545,41 @@ def update_taiex_prediction(predict_days):
|
|
| 545 |
if data.empty:
|
| 546 |
return html.Div("無法獲取台指期資料"), {}
|
| 547 |
|
| 548 |
-
#
|
| 549 |
-
|
| 550 |
-
if
|
| 551 |
return html.Div("資料不足,無法進行預測"), {}
|
| 552 |
|
| 553 |
current_price = data['Close'].iloc[-1]
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
|
| 558 |
-
# 預測結果卡片
|
| 559 |
color = '#00C851' if change_pct >= 0 else '#FF4444'
|
| 560 |
arrow = '📈' if change_pct >= 0 else '📉'
|
| 561 |
|
|
@@ -592,16 +616,17 @@ def update_taiex_prediction(predict_days):
|
|
| 592 |
line=dict(color='#FFA726', width=2)
|
| 593 |
))
|
| 594 |
|
| 595 |
-
#
|
| 596 |
-
|
| 597 |
fig.add_trace(go.Scatter(
|
| 598 |
-
x=
|
| 599 |
-
y=
|
| 600 |
mode='lines+markers',
|
| 601 |
-
name=f'{predict_days}
|
| 602 |
line=dict(color=color, width=3, dash='dash'),
|
| 603 |
marker=dict(size=8)
|
| 604 |
))
|
|
|
|
| 605 |
|
| 606 |
fig.update_layout(
|
| 607 |
title=f'台指期 {predict_days}日預測走勢',
|
|
|
|
| 533 |
})
|
| 534 |
])
|
| 535 |
|
| 536 |
+
# 台指期獨立預測回調函數 (新版本)
|
| 537 |
@app.callback(
|
| 538 |
[dash.dependencies.Output('taiex-prediction-results', 'children'),
|
| 539 |
dash.dependencies.Output('taiex-prediction-chart', 'figure')],
|
|
|
|
| 545 |
if data.empty:
|
| 546 |
return html.Div("無法獲取台指期資料"), {}
|
| 547 |
|
| 548 |
+
# 執行最終日的預測,用於顯示在結果卡片上
|
| 549 |
+
final_prediction = simple_lstm_predict(data, predict_days)
|
| 550 |
+
if final_prediction is None:
|
| 551 |
return html.Div("資料不足,無法進行預測"), {}
|
| 552 |
|
| 553 |
current_price = data['Close'].iloc[-1]
|
| 554 |
+
last_date = data.index[-1]
|
| 555 |
+
predicted_price = final_prediction['predicted_price']
|
| 556 |
+
change_pct = final_prediction['change_pct']
|
| 557 |
+
confidence = final_prediction['confidence']
|
| 558 |
+
|
| 559 |
+
# --- 主要修改處:計算預測路徑 ---
|
| 560 |
+
# 1. 定義不同預測天期所包含的中間節點
|
| 561 |
+
prediction_paths = {
|
| 562 |
+
1: [1],
|
| 563 |
+
5: [1, 5],
|
| 564 |
+
10: [1, 5, 10],
|
| 565 |
+
20: [1, 10, 20],
|
| 566 |
+
60: [1, 10, 20, 60]
|
| 567 |
+
}
|
| 568 |
+
intervals_to_predict = prediction_paths.get(predict_days, [predict_days])
|
| 569 |
+
|
| 570 |
+
# 2. 準備儲存預測路徑的座標點 (起始點為目前價格)
|
| 571 |
+
prediction_dates = [last_date]
|
| 572 |
+
prediction_prices = [current_price]
|
| 573 |
+
|
| 574 |
+
# 3. 循環計算路徑上每個點的預測值
|
| 575 |
+
for days in intervals_to_predict:
|
| 576 |
+
interim_prediction = simple_lstm_predict(data, days)
|
| 577 |
+
if interim_prediction:
|
| 578 |
+
prediction_dates.append(last_date + timedelta(days=days))
|
| 579 |
+
prediction_prices.append(interim_prediction['predicted_price'])
|
| 580 |
+
# --- 修改結束 ---
|
| 581 |
|
| 582 |
+
# 預測結果卡片 (維持不變)
|
| 583 |
color = '#00C851' if change_pct >= 0 else '#FF4444'
|
| 584 |
arrow = '📈' if change_pct >= 0 else '📉'
|
| 585 |
|
|
|
|
| 616 |
line=dict(color='#FFA726', width=2)
|
| 617 |
))
|
| 618 |
|
| 619 |
+
# --- 修改處:使用新的座標點繪製預測線 ---
|
| 620 |
+
# 4. 繪製由多個預測點連接而成的路徑
|
| 621 |
fig.add_trace(go.Scatter(
|
| 622 |
+
x=prediction_dates, # 使用包含多個日期的列表
|
| 623 |
+
y=prediction_prices, # 使用包含多個預測價格的列表
|
| 624 |
mode='lines+markers',
|
| 625 |
+
name=f'{predict_days}日預測路徑',
|
| 626 |
line=dict(color=color, width=3, dash='dash'),
|
| 627 |
marker=dict(size=8)
|
| 628 |
))
|
| 629 |
+
# --- 修改結束 ---
|
| 630 |
|
| 631 |
fig.update_layout(
|
| 632 |
title=f'台指期 {predict_days}日預測走勢',
|