AlanRex commited on
Commit
1b3f174
·
verified ·
1 Parent(s): aed3c26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -60
app.py CHANGED
@@ -539,13 +539,7 @@ app.layout = html.Div([
539
  dash.dependencies.Output('taiex-prediction-chart', 'figure')],
540
  [dash.dependencies.Input('taiex-prediction-period', 'value')]
541
  )
542
- # 新增:儲存不同預測天數的預測結果
543
- taiex_predictions = {}
544
-
545
  def update_taiex_prediction(predict_days):
546
- """更新台指期預測結果和圖表"""
547
- global taiex_predictions # 使用 global 關鍵字來修改全域變數
548
-
549
  # 獲取台指期歷史資料
550
  data = get_stock_data('^TWII', '2y')
551
  if data.empty:
@@ -561,28 +555,10 @@ def update_taiex_prediction(predict_days):
561
  change_pct = prediction['change_pct']
562
  confidence = prediction['confidence']
563
 
564
- # 儲存當前預測天數的預測結果
565
- taiex_predictions[predict_days] = {
566
- 'date': data.index[-1] + timedelta(days=predict_days),
567
- 'price': predicted_price,
568
- 'change_pct': change_pct,
569
- 'confidence': confidence
570
- }
571
-
572
- # 根據預測天數,篩選出需要顯示的預測點
573
- # 例如,如果選擇預測 60 天,我們需要 1, 10, 20, 60 天的預測點
574
- # 如果選擇預測 20 天,我們需要 1, 10, 20 天的預測點
575
- days_to_show = sorted([d for d in taiex_predictions.keys() if d <= predict_days] + [predict_days])
576
-
577
- # 確保包含預設的 1, 5, 10, 20, 60 天選項
578
- all_options = sorted(list(set([1, 5, 10, 20, 60] + list(taiex_predictions.keys()))))
579
- days_to_show = sorted([d for d in all_options if d <= predict_days])
580
-
581
  # 預測結果卡片
582
  color = '#00C851' if change_pct >= 0 else '#FF4444'
583
  arrow = '📈' if change_pct >= 0 else '📉'
584
 
585
- # 顯示當前選擇預測天數的結果
586
  result_card = html.Div([
587
  html.H4(f"{predict_days}日後預測結果", style={'margin': '0 0 15px 0', 'color': 'white'}),
588
  html.Div([
@@ -616,42 +592,16 @@ def update_taiex_prediction(predict_days):
616
  line=dict(color='#FFA726', width=2)
617
  ))
618
 
619
- # 繪製所有選定天數的預測點和連線
620
- future_prediction_points = []
621
- for day in days_to_show:
622
- if day in taiex_predictions:
623
- future_prediction_points.append(taiex_predictions[day])
624
-
625
- if future_prediction_points:
626
- # 按日期排序,確保預測線是連續的
627
- future_prediction_points.sort(key=lambda x: x['date'])
628
-
629
- pred_dates = [p['date'] for p in future_prediction_points]
630
- pred_prices = [p['price'] for p in future_prediction_points]
631
-
632
- # 獲取最後一個歷史點的日期和價格
633
- last_hist_date = recent_data.index[-1]
634
- last_hist_price = current_price
635
-
636
- # 添加預測線
637
- fig.add_trace(go.Scatter(
638
- x=[last_hist_date] + pred_dates,
639
- y=[last_hist_price] + pred_prices,
640
- mode='lines+markers',
641
- name=f'預測線 ({predict_days}日)', # 標示為當前選擇的預測總天數
642
- line=dict(color=color, width=3, dash='dash'),
643
- marker=dict(size=8)
644
- ))
645
-
646
- # 標示出當前選擇的預測點
647
- if predict_days in taiex_predictions:
648
- fig.add_trace(go.Scatter(
649
- x=[taiex_predictions[predict_days]['date']],
650
- y=[taiex_predictions[predict_days]['price']],
651
- mode='markers',
652
- name=f'{predict_days}日預測點',
653
- marker=dict(size=10, color=color, symbol='diamond')
654
- ))
655
 
656
  fig.update_layout(
657
  title=f'台指期 {predict_days}日預測走勢',
 
539
  dash.dependencies.Output('taiex-prediction-chart', 'figure')],
540
  [dash.dependencies.Input('taiex-prediction-period', 'value')]
541
  )
 
 
 
542
  def update_taiex_prediction(predict_days):
 
 
 
543
  # 獲取台指期歷史資料
544
  data = get_stock_data('^TWII', '2y')
545
  if data.empty:
 
555
  change_pct = prediction['change_pct']
556
  confidence = prediction['confidence']
557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
  # 預測結果卡片
559
  color = '#00C851' if change_pct >= 0 else '#FF4444'
560
  arrow = '📈' if change_pct >= 0 else '📉'
561
 
 
562
  result_card = html.Div([
563
  html.H4(f"{predict_days}日後預測結果", style={'margin': '0 0 15px 0', 'color': 'white'}),
564
  html.Div([
 
592
  line=dict(color='#FFA726', width=2)
593
  ))
594
 
595
+ # 預測點
596
+ future_date = recent_data.index[-1] + timedelta(days=predict_days)
597
+ fig.add_trace(go.Scatter(
598
+ x=[recent_data.index[-1], future_date],
599
+ y=[current_price, predicted_price],
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}日預測走勢',