mircanboncea commited on
Commit
a473b60
·
1 Parent(s): 6f31b18

implement

Browse files
README.md CHANGED
@@ -1,13 +1,10 @@
1
  ---
2
  title: StockOracle
3
- emoji: 🐨
4
- colorFrom: yellow
5
- colorTo: pink
6
  sdk: streamlit
7
  sdk_version: 1.26.0
8
  app_file: app.py
9
  pinned: false
10
- license: gpl-2.0
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: StockOracle
3
+ emoji: 💻
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: streamlit
7
  sdk_version: 1.26.0
8
  app_file: app.py
9
  pinned: false
10
+ ---
 
 
 
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import pandas_datareader as data
3
+ import numpy as np
4
+ import plotly.graph_objects as go
5
+ import streamlit as st
6
+ import yfinance as yf
7
+ import datetime as dt
8
+
9
+ from pandas_datareader import data as pdr
10
+
11
+ from keras.models import load_model
12
+ from sklearn.preprocessing import MinMaxScaler
13
+ from tensorflow.python import tf2
14
+
15
+ from datetime import timedelta
16
+
17
+ default_ticker = 'NVDA'
18
+
19
+ yf.pdr_override()
20
+
21
+ st.set_page_config(
22
+ page_title="Future Stock Price Prediction",
23
+ initial_sidebar_state="auto",
24
+ page_icon=":computer:",
25
+ layout="wide",
26
+ )
27
+
28
+ today = dt.date.today()
29
+
30
+ def create_dataset(df, days):
31
+ x = []
32
+ y = []
33
+ for i in range(days, df.shape[0]):
34
+ x.append(df[i-days:i, 0])
35
+ y.append(df[i, 0])
36
+ x = np.array(x)
37
+ y = np.array(y)
38
+ return x,y
39
+
40
+ def predict(model_file, x_data, y_data):
41
+ model = load_model(model_file)
42
+ predictions = model.predict(x_data)
43
+ predictions = scaler.inverse_transform(predictions)
44
+ y_data_scaled = scaler.inverse_transform(y_data.reshape(-1, 1))
45
+
46
+ df_y_data_scaled = pd.DataFrame(y_data_scaled, columns = ['Close'])
47
+ df_predictions = pd.DataFrame(predictions, columns = ['Close'])
48
+
49
+ return df_y_data_scaled, df_predictions
50
+
51
+
52
+ def prediction_chart(model_file, x_data, original_y_data, predicted_y_data):
53
+ chart = go.Figure()
54
+ chart.add_trace(go.Scatter(x = x_data, y = original_y_data.Close, name='Price',
55
+ mode='lines', marker_color='black'))
56
+
57
+ chart.add_trace(go.Scatter(x = x_data, y = predicted_y_data.Close, name='Prediction',
58
+ mode='lines', marker_color='red'))
59
+
60
+ chart.update_layout(title='Stock Price vs Predicted Price with loaded model: ' + model_file,
61
+ xaxis_title='Date',
62
+ yaxis_title='Price')
63
+
64
+ chart.show()
65
+ st.plotly_chart(chart, use_container_width=True)
66
+
67
+ def show_prediction(model_file, x_data, dataset_test, days):
68
+ #Creating dataset
69
+ x_test, y_test = create_dataset(dataset_test, days)
70
+ x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
71
+
72
+ #Load model and predict
73
+ original_y_data, predicted_y_data = predict(model_file, x_test, y_test)
74
+ prediction_chart(model_file, x_data[days:], original_y_data, predicted_y_data)
75
+
76
+ with st.sidebar:
77
+ user_input = st.text_input("Ticker", default_ticker)
78
+ user_date = st.date_input("Prediction start date", dt.date(2021, 1, 1))
79
+
80
+ ticker=True
81
+ try:
82
+ df = pdr.get_data_yahoo(user_input, start=user_date, end=today).reset_index()
83
+ current_price = df['Close'].tail(1)
84
+ except:
85
+ ticker=False
86
+ if ticker==True:
87
+ st.header(current_price.iloc[0].round(2))
88
+ else:
89
+ st.write("Wrong ticker. Select again")
90
+ st.markdown("""---""")
91
+
92
+ st.title("S&P FUTURES")
93
+ spval=True
94
+ try:
95
+ sp = pdr.get_data_yahoo('ES=F', start=today - timedelta(7), end=today)['Close'].tail(1)
96
+ except:
97
+ spval=False
98
+ if spval==True:
99
+ st.header(sp.iloc[0].round(2))
100
+ else:
101
+ st.write("Can't load right now")
102
+ st.markdown("""---""")
103
+
104
+ st.title("NASDAQ")
105
+ nasval=True
106
+ try:
107
+ nas = pdr.get_data_yahoo('NQ=F', start=today - timedelta(7), end=today)['Close'].tail(1)
108
+ except:
109
+ nasval=False
110
+ if nasval==True:
111
+ st.header(nas.iloc[0].round(2))
112
+ else:
113
+ st.write("Can't load right now")
114
+ st.markdown("""---""")
115
+
116
+ st.title("DOW")
117
+ dowval=True
118
+ try:
119
+ dow = pdr.get_data_yahoo('YM=F', start=today - timedelta(7), end=today)['Close'].tail(1)
120
+ except:
121
+ dowval=False
122
+ if dowval==True:
123
+ st.header(dow.iloc[0].round(2))
124
+ else:
125
+ st.write("Can't load right now")
126
+ st.markdown("""---""")
127
+
128
+ st.title("GOLD")
129
+ goldval=True
130
+ try:
131
+ gold = pdr.get_data_yahoo('GC=F', start=today - timedelta(7), end=today)['Close'].tail(1)
132
+ except:
133
+ goldval=False
134
+ if goldval==True:
135
+ st.header(gold.iloc[0].round(2))
136
+ else:
137
+ st.write("Can't load right now")
138
+ st.markdown("""---""")
139
+
140
+ st.title("CRUDE OIL")
141
+ oilval=True
142
+ try:
143
+ oil = pdr.get_data_yahoo('CL=F', start=today - timedelta(7), end=today)['Close'].tail(1)
144
+ except:
145
+ oilval=False
146
+ if oilval==True:
147
+ st.header(oil.iloc[0].round(2))
148
+ else:
149
+ st.write("Can't load right now")
150
+ st.markdown("""---""")
151
+
152
+ if ticker==True:
153
+
154
+ date = df.Date
155
+ close = df.Close.fillna(method='ffill')
156
+
157
+ fig = go.Figure()
158
+ fig.add_trace(go.Scatter(x = date, y = close, name='Price',
159
+ mode='lines', marker_color='black'))
160
+
161
+ ma1 = close.ewm(span=100, adjust=False).mean()
162
+ fig.add_trace(go.Scatter(x = date, y = ma1, name='MA 100',
163
+ mode='lines', marker_color='red'))
164
+
165
+ ma2 = close.ewm(span=365, adjust=False).mean()
166
+ fig.add_trace(go.Scatter(x = date, y = ma2, name='MA 365',
167
+ mode='lines', marker_color='blue'))
168
+
169
+ fig.update_layout(title='Stock Price vs Moving averages',
170
+ xaxis_title='Date',
171
+ yaxis_title='Price')
172
+ fig.show()
173
+ st.plotly_chart(fig, use_container_width=True)
174
+
175
+ #Start prediction
176
+
177
+ data_training = pd.DataFrame(close[0:int(len(close)*0.7)])
178
+ data_testing = pd.DataFrame(close[int(len(close)*0.7):int(len(close))])
179
+ x_data = date[int(len(date)*0.7):int(len(date))].reset_index(drop=True)
180
+
181
+ #normalising data
182
+ scaler = MinMaxScaler(feature_range=(0,1))
183
+
184
+ dataset_train = scaler.fit_transform(data_training)
185
+ dataset_test = scaler.transform(data_testing)
186
+
187
+ show_prediction('stock_prediction.h5', x_data, dataset_test, 50)
188
+ show_prediction('stock_prediction_test.h5', x_data, dataset_test, 7)
189
+ show_prediction('stock_prediction_longer_train.h5', x_data, dataset_test, 7)
190
+
191
+
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy==1.24.3
2
+ pandas==2.0.2
3
+ pandas-datareader==0.10.0
4
+ seaborn==0.12.2
5
+ yfinance==0.2.24
6
+ scikit-learn==1.2.2
7
+ keras==2.13.1
8
+ streamlit==1.22.0
9
+ tensorflow
10
+ plotly==5.9.0
stock_prediction.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:230bec19c7ac3b71d78e29f788fa7155b55e6c5836872c26c1c809de4c14f463
3
+ size 3195144
stock_prediction_longer_train.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2d805c5ef443a6a065d072d6fca5f00925081e5faa24241b5547eee7ebd52ecd
3
+ size 1302600
stock_prediction_test.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ca16b6c20f59ed16dff39dc332af7351ba4638ece8632c34fa0e02185965335
3
+ size 1302584