Johan713 commited on
Commit
01bf896
·
verified ·
1 Parent(s): 7c20310

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -15
app.py CHANGED
@@ -417,27 +417,83 @@ def predict_stock_prices():
417
  predictions = predictor.predict(days=days_to_predict)
418
 
419
  if predictions is not None:
420
- plot = create_prediction_plot(data, predictions, company_name)
421
- st.plotly_chart(plot, use_container_width=True)
422
-
423
- candlestick_plot = create_candlestick_plot(data, company_name)
424
- st.plotly_chart(candlestick_plot, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
 
426
  st.subheader("Predicted Prices")
427
  pred_df = predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_to_predict)
428
  pred_df.columns = ['Date', 'Predicted Price', 'Lower Bound', 'Upper Bound']
429
  st.dataframe(pred_df)
430
 
431
- # Component-wise forecast
432
- st.subheader("Forecast Components")
433
- fig = predictor.model.plot_components(predictions)
434
- st.pyplot(fig)
435
-
436
- # Changepoints
437
- st.subheader("Detected Changepoints")
438
- fig = predictor.model.plot(predictions)
439
- a = add_changepoints_to_plot(fig.gca(), predictor.model, predictions)
440
- st.pyplot(fig)
441
 
442
  news = fetch_news(company_name)
443
  st.subheader("Latest News")
@@ -447,6 +503,8 @@ def predict_stock_prices():
447
  st.error("Failed to generate predictions. The predicted data is None.")
448
  else:
449
  st.error("Failed to train the Prophet model. Please try a different dataset.")
 
 
450
 
451
  def explore_data():
452
  st.header("Explore Stock Data")
 
417
  predictions = predictor.predict(days=days_to_predict)
418
 
419
  if predictions is not None:
420
+ # Create prediction plot
421
+ fig = go.Figure()
422
+
423
+ fig.add_trace(go.Scatter(
424
+ x=data.index,
425
+ y=data['Close'],
426
+ mode='lines',
427
+ name='Historical Data',
428
+ line=dict(color='cyan')
429
+ ))
430
+
431
+ future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=days_to_predict)
432
+ fig.add_trace(go.Scatter(
433
+ x=future_dates,
434
+ y=predictions['yhat'].tail(days_to_predict),
435
+ mode='lines',
436
+ name='Predicted Data',
437
+ line=dict(color='yellow')
438
+ ))
439
+
440
+ fig.add_trace(go.Scatter(
441
+ x=future_dates,
442
+ y=predictions['yhat_upper'].tail(days_to_predict),
443
+ mode='lines',
444
+ line=dict(width=0),
445
+ showlegend=False
446
+ ))
447
+
448
+ fig.add_trace(go.Scatter(
449
+ x=future_dates,
450
+ y=predictions['yhat_lower'].tail(days_to_predict),
451
+ mode='lines',
452
+ line=dict(width=0),
453
+ fillcolor='rgba(255, 255, 0, 0.3)',
454
+ fill='tonexty',
455
+ name='Prediction Interval'
456
+ ))
457
+
458
+ fig.update_layout(
459
+ title=f'{company_name} Stock Price Prediction',
460
+ xaxis_title='Date',
461
+ yaxis_title='Close Price',
462
+ template='plotly_dark',
463
+ hovermode='x unified',
464
+ xaxis_rangeslider_visible=True,
465
+ legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
466
+ )
467
+
468
+ st.plotly_chart(fig, use_container_width=True)
469
+
470
+ # Create forecast components plot using Plotly
471
+ components = predictor.model.predict_components(predictions)
472
+ n_components = len(components.columns)
473
+
474
+ fig_components = make_subplots(rows=n_components, cols=1,
475
+ subplot_titles=components.columns)
476
+
477
+ for i, component in enumerate(components.columns, start=1):
478
+ fig_components.add_trace(
479
+ go.Scatter(x=predictions['ds'], y=components[component],
480
+ mode='lines', name=component),
481
+ row=i, col=1
482
+ )
483
+
484
+ fig_components.update_layout(height=300*n_components,
485
+ title_text="Forecast Components",
486
+ showlegend=False)
487
+
488
+ st.plotly_chart(fig_components, use_container_width=True)
489
 
490
  st.subheader("Predicted Prices")
491
  pred_df = predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_to_predict)
492
  pred_df.columns = ['Date', 'Predicted Price', 'Lower Bound', 'Upper Bound']
493
  st.dataframe(pred_df)
494
 
495
+ # Provide download link for predictions
496
+ st.markdown(get_table_download_link(pred_df, f"{ticker}_predictions.csv", "Download predictions CSV"), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
497
 
498
  news = fetch_news(company_name)
499
  st.subheader("Latest News")
 
503
  st.error("Failed to generate predictions. The predicted data is None.")
504
  else:
505
  st.error("Failed to train the Prophet model. Please try a different dataset.")
506
+ else:
507
+ st.error("Failed to fetch stock data. Please try again.")
508
 
509
  def explore_data():
510
  st.header("Explore Stock Data")