Update app.py
Browse files416 unmodified lines4174184194204214224234244254264274284294304314324334344354364374384394404414424433 unmodified lines447448449450451452416 unmodified linespredictions = predictor.predict(days=days_to_predict)if predictions is not None:plot = create_prediction_plot(data, predictions, company_name)st.plotly_chart(plot, use_container_width=True)candlestick_plot = create_candlestick_plot(data, company_name)st.plotly_chart(candlestick_plot, use_container_width=True)st.subheader("Predicted Prices")pred_df = predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_to_predict)pred_df.columns = ['Date', 'Predicted Price', 'Lower Bound', 'Upper Bound']st.dataframe(pred_df)# Component-wise forecastst.subheader("Forecast Components")fig = predictor.model.plot_components(predictions)st.pyplot(fig)# Changepointsst.subheader("Detected Changepoints")fig = predictor.model.plot(predictions)a = add_changepoints_to_plot(fig.gca(), predictor.model, predictions)st.pyplot(fig)news = fetch_news(company_name)st.subheader("Latest News")3 unmodified linesst.error("Failed to generate predictions. The predicted data is None.")else:st.error("Failed to train the Prophet model. Please try a different dataset.")def explore_data():st.header("Explore Stock Data")416 unmodified lines4174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984993 unmodified lines503504505506507508509510416 unmodified linespredictions = predictor.predict(days=days_to_predict)if predictions is not None:# Create prediction plotfig = go.Figure()fig.add_trace(go.Scatter(x=data.index,y=data['Close'],mode='lines',name='Historical Data',line=dict(color='cyan')))future_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=days_to_predict)fig.add_trace(go.Scatter(x=future_dates,y=predictions['yhat'].tail(days_to_predict),mode='lines',name='Predicted Data',line=dict(color='yellow')))fig.add_trace(go.Scatter(x=future_dates,y=predictions['yhat_upper'].tail(days_to_predict),mode='lines',line=dict(width=0),showlegend=False))fig.add_trace(go.Scatter(x=future_dates,y=predictions['yhat_lower'].tail(days_to_predict),mode='lines',line=dict(width=0),fillcolor='rgba(255, 255, 0, 0.3)',fill='tonexty',name='Prediction Interval'))fig.update_layout(title=f'{company_name} Stock Price Prediction',xaxis_title='Date',yaxis_title='Close Price',template='plotly_dark',hovermode='x unified',xaxis_rangeslider_visible=True,legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1))st.plotly_chart(fig, use_container_width=True)# Create forecast components plot using Plotlycomponents = predictor.model.predict_components(predictions)n_components = len(components.columns)fig_components = make_subplots(rows=n_components, cols=1,subplot_titles=components.columns)for i, component in enumerate(components.columns, start=1):fig_components.add_trace(go.Scatter(x=predictions['ds'], y=components[component],mode='lines', name=component),row=i, col=1)fig_components.update_layout(height=300*n_components,title_text="Forecast Components",showlegend=False)st.plotly_chart(fig_components, use_container_width=True)st.subheader("Predicted Prices")pred_df = predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(days_to_predict)pred_df.columns = ['Date', 'Predicted Price', 'Lower Bound', 'Upper Bound']st.dataframe(pred_df)# Provide download link for predictionsst.markdown(get_table_download_link(pred_df, f"{ticker}_predictions.csv", "Download predictions CSV"), unsafe_allow_html=True)news = fetch_news(company_name)st.subheader("Latest News")3 unmodified linesst.error("Failed to generate predictions. The predicted data is None.")else:st.error("Failed to train the Prophet model. Please try a different dataset.")else:st.error("Failed to fetch stock data. Please try again.")def explore_data():st.header("Explore Stock Data")
app.py+73-15