From 46d413fb62d8d5d681157728c9544304e78142b5 Mon Sep 17 00:00:00 2001 From: nongkarn2003 <158234555+nongkarn2003@users.noreply.github.com> Date: Thu, 9 May 2024 09:48:26 +0000 Subject: [PATCH 1/2] Master --- streamlit_app.py | 101 +++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/streamlit_app.py b/streamlit_app.py index 7a0ed1272052..97ebaff80ef5 100644 --- a/streamlit_app.py +++ b/streamlit_app.py @@ -1,40 +1,65 @@ -import altair as alt -import numpy as np -import pandas as pd import streamlit as st +from datetime import date +import yfinance as yf +from prophet import Prophet +from prophet.plot import plot_plotly +from plotly import graph_objects as go -""" -# Welcome to Streamlit! - -Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:. -If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community -forums](https://discuss.streamlit.io). - -In the meantime, below is an example of what you can do with just a few lines of code: -""" - -num_points = st.slider("Number of points in spiral", 1, 10000, 1100) -num_turns = st.slider("Number of turns in spiral", 1, 300, 31) - -indices = np.linspace(0, 1, num_points) -theta = 2 * np.pi * num_turns * indices -radius = indices - -x = radius * np.cos(theta) -y = radius * np.sin(theta) - -df = pd.DataFrame({ - "x": x, - "y": y, - "idx": indices, - "rand": np.random.randn(num_points), -}) - -st.altair_chart(alt.Chart(df, height=700, width=700) - .mark_point(filled=True) - .encode( - x=alt.X("x", axis=None), - y=alt.Y("y", axis=None), - color=alt.Color("idx", legend=None, scale=alt.Scale()), - size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), - )) + +# เปลี่ยนแปลงพอร์ตเซิร์ฟเวอร์เมื่อมีการรัน Streamlit app +port = 8502 +START = "2019-01-01" +TODAY = date.today().strftime("%Y-%m-%d") + +st.title("Stock Prediction App") + +stocks = ("ADVANC.bk", "AOT.bk", "AWC.bk", "BANPU.bk", "BBL.bk", "BDMS.bk", "BEM.bk", "BGRIM.bk", "BH.bk", "BTS.bk", + "CBG.bk", "CENTEL.bk", "COM7.bk", "CPALL.bk", "CPF.bk", "CPN.bk", "CRC.bk", "DELTA.bk", "EA.bk", "EGCO.bk", + "GLOBAL.bk", "GPSC.bk", "GULF.bk", "HMPRO.bk", "INTUCH.bk", "IVL.bk", "KBANK.bk", "KCE.bk", "KTB.bk", "KTC.bk", + "LH.bk", "MINT.bk", "MTC.bk", "OR.bk", "OSP.bk", "PTT.bk", "PTTEP.bk", "PTTGC.bk", "RATCH.bk", "SAWAD.bk", + "SCB.bk", "SCC.bk", "SCGP.bk", "TISCO.bk", "TOP.bk", "TTB.bk", "TU.bk", "WHA.bk") + +selected_stocks = st.selectbox("Select Symbol for prediction",stocks) + +n_years = st.slider("Year of Prediction",1,4) +period = n_years * 365 + +@st.cache_data +def load_data(ticker): + data = yf.download(ticker,START,TODAY) + data.reset_index(inplace=True) + return data + +data_load_state = st.text("Load data....") +data = load_data(selected_stocks) +data_load_state.text("Loading data...done") + + +def plot_raw_data(): + fig = go.Figure() + fig.add_trace(go.Scatter(x=data['Date'],y=data['Open'],name='stock_open')) + fig.add_trace(go.Scatter(x=data['Date'],y=data['Close'],name='stock_close')) + fig.layout.update(title_text="Time Series Data", xaxis_rangeslider_visible=True) + st.plotly_chart(fig) + +plot_raw_data() + +#Forecasting +df_train = data[['Date','Close']] +df_train = df_train.rename(columns={"Date": "ds", "Close": "y"}) + +m = Prophet() +m.fit(df_train) +future = m.make_future_dataframe(periods=period) +forecast = m.predict(future) + +st.subheader('Forecast data') +st.write(forecast.tail()) + +st.write('forecast data') +fig1 = plot_plotly(m,forecast) +st.plotly_chart(fig1) + +st.write('forecast components') +fig2 = m.plot_components(forecast) +st.write(fig2) \ No newline at end of file From afcf36e36e3c0eb366d3532ea433b7b5e9bcce1a Mon Sep 17 00:00:00 2001 From: nongkarn2003 <158234555+nongkarn2003@users.noreply.github.com> Date: Thu, 9 May 2024 22:04:03 +0700 Subject: [PATCH 2/2] Update requirements.txt --- requirements.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 502d7d1a0d19..3b4a45a1838b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ -altair pandas -streamlit +yfinance +prophet +plotly