Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
altair
pandas
streamlit
yfinance
prophet
plotly
101 changes: 63 additions & 38 deletions streamlit_app.py
Original file line number Diff line number Diff line change
@@ -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)