Maisarah Nurain commited on
Commit
48e43cb
1 Parent(s): eb8f9f1

Add application file

Browse files
Files changed (2) hide show
  1. app.py +64 -6
  2. myapp.py +0 -83
app.py CHANGED
@@ -3,6 +3,14 @@ import streamlit as st
3
  import pandas as pd
4
  import datetime
5
 
 
 
 
 
 
 
 
 
6
  st.write("""
7
  # Simple Stock Price App
8
 
@@ -10,16 +18,66 @@ Shown are the stock **closing price** and **volume**.
10
  """)
11
 
12
  def user_input_features() :
13
- stock_symbol = st.sidebar.selectbox('Symbol',('ANTM.JK','ARNA.JK', 'DUTI.JK', 'ELSA.JK',
14
- 'MFMI.JK'))
15
  date_start = st.sidebar.date_input("Start Date", datetime.date(2015, 5, 31))
16
  date_end = st.sidebar.date_input("End Date", datetime.date.today())
17
 
18
- tickerData = yf.Ticker(stock_symbol)
19
  tickerDf = tickerData.history(period='1d', start=date_start, end=date_end)
20
- return tickerDf
21
 
22
- input_df = user_input_features()
23
 
24
  st.line_chart(input_df.Close)
25
- st.line_chart(input_df.Volume)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import pandas as pd
4
  import datetime
5
 
6
+ import numpy as np
7
+ import matplotlib.pyplot as plt
8
+ from keras.models import Sequential
9
+ from keras.layers import LSTM
10
+ from keras.layers import Dense
11
+ from keras.layers import Bidirectional
12
+
13
+
14
  st.write("""
15
  # Simple Stock Price App
16
 
 
18
  """)
19
 
20
  def user_input_features() :
21
+ stock_symbol = st.sidebar.selectbox('Symbol',('ANTM', 'ARNA', 'DUTI', 'ELSA', 'MFMI'))
 
22
  date_start = st.sidebar.date_input("Start Date", datetime.date(2015, 5, 31))
23
  date_end = st.sidebar.date_input("End Date", datetime.date.today())
24
 
25
+ tickerData = yf.Ticker(stock_symbol+'.JK')
26
  tickerDf = tickerData.history(period='1d', start=date_start, end=date_end)
27
+ return tickerDf, stock_symbol
28
 
29
+ input_df, stock_symbol = user_input_features()
30
 
31
  st.line_chart(input_df.Close)
32
+ st.line_chart(input_df.Volume)
33
+
34
+ st.write("""
35
+ # Stock Price Prediction
36
+
37
+ Shown are the stock prediction for next 20 days.
38
+ """)
39
+
40
+ n_steps = 100
41
+ n_features = 1
42
+
43
+ model = Sequential()
44
+ model.add(Bidirectional(LSTM(300, activation='relu'), input_shape=(n_steps, n_features)))
45
+ model.add(Dense(1))
46
+ model.compile(optimizer='adam', loss='mse')
47
+
48
+ model.load_weights(stock_symbol + ".h5")
49
+ df = input_df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
50
+ df = df[df.Volume > 0]
51
+
52
+ close = df['Close'][-n_steps:].to_list()
53
+ min_in = min(close)
54
+ max_in = max(close)
55
+ in_seq = []
56
+ for i in close :
57
+ in_seq.append((i - min_in) / (max_in - min_in))
58
+
59
+ for i in range(20) :
60
+ x_input = np.array(in_seq[-100:])
61
+ x_input = x_input.reshape((1, n_steps, n_features))
62
+ yhat = model.predict(x_input, verbose=0)
63
+ in_seq.append(yhat[0][0])
64
+
65
+ norm_res = in_seq[-20:]
66
+ res = []
67
+ for i in norm_res :
68
+ res.append(i * (max_in - min_in) + min_in)
69
+
70
+ closepred = close[-80:]
71
+ for x in res :
72
+ closepred.append(x)
73
+
74
+ plt.figure(figsize = (20,10))
75
+ plt.plot(closepred, label="Prediction")
76
+ plt.plot(close[-80:], label="Previous")
77
+ plt.ylabel('Price (Rp)', fontsize = 15 )
78
+ plt.xlabel('Days', fontsize = 15 )
79
+ plt.title(stock_symbol + " Stock Prediction", fontsize = 20)
80
+ plt.legend()
81
+ plt.grid()
82
+
83
+ st.pyplot(plt)
myapp.py DELETED
@@ -1,83 +0,0 @@
1
- import yfinance as yf
2
- import streamlit as st
3
- import pandas as pd
4
- import datetime
5
-
6
- import numpy as np
7
- import matplotlib.pyplot as plt
8
- from keras.models import Sequential
9
- from keras.layers import LSTM
10
- from keras.layers import Dense
11
- from keras.layers import Bidirectional
12
-
13
-
14
- st.write("""
15
- # Simple Stock Price App
16
-
17
- Shown are the stock **closing price** and **volume**.
18
- """)
19
-
20
- def user_input_features() :
21
- stock_symbol = st.sidebar.selectbox('Symbol',('ANTM', 'ARNA', 'DUTI', 'ELSA', 'MFMI'))
22
- date_start = st.sidebar.date_input("Start Date", datetime.date(2015, 5, 31))
23
- date_end = st.sidebar.date_input("End Date", datetime.date.today())
24
-
25
- tickerData = yf.Ticker(stock_symbol+'.JK')
26
- tickerDf = tickerData.history(period='1d', start=date_start, end=date_end)
27
- return tickerDf, stock_symbol
28
-
29
- input_df, stock_symbol = user_input_features()
30
-
31
- st.line_chart(input_df.Close)
32
- st.line_chart(input_df.Volume)
33
-
34
- st.write("""
35
- # Stock Price Prediction
36
-
37
- Shown are the stock prediction for next 20 days.
38
- """)
39
-
40
- n_steps = 100
41
- n_features = 1
42
-
43
- model = Sequential()
44
- model.add(Bidirectional(LSTM(300, activation='relu'), input_shape=(n_steps, n_features)))
45
- model.add(Dense(1))
46
- model.compile(optimizer='adam', loss='mse')
47
-
48
- model.load_weights(stock_symbol + ".h5")
49
- df = input_df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
50
- df = df[df.Volume > 0]
51
-
52
- close = df['Close'][-n_steps:].to_list()
53
- min_in = min(close)
54
- max_in = max(close)
55
- in_seq = []
56
- for i in close :
57
- in_seq.append((i - min_in) / (max_in - min_in))
58
-
59
- for i in range(20) :
60
- x_input = np.array(in_seq[-100:])
61
- x_input = x_input.reshape((1, n_steps, n_features))
62
- yhat = model.predict(x_input, verbose=0)
63
- in_seq.append(yhat[0][0])
64
-
65
- norm_res = in_seq[-20:]
66
- res = []
67
- for i in norm_res :
68
- res.append(i * (max_in - min_in) + min_in)
69
-
70
- closepred = close[-80:]
71
- for x in res :
72
- closepred.append(x)
73
-
74
- plt.figure(figsize = (20,10))
75
- plt.plot(closepred, label="Prediction")
76
- plt.plot(close[-80:], label="Previous")
77
- plt.ylabel('Price (Rp)', fontsize = 15 )
78
- plt.xlabel('Days', fontsize = 15 )
79
- plt.title(stock_symbol + " Stock Prediction", fontsize = 20)
80
- plt.legend()
81
- plt.grid()
82
-
83
- st.pyplot(plt)