Spaces:
Runtime error
Runtime error
File size: 4,063 Bytes
80726db 7f93530 80726db 5bfe1bf 80726db 3073aa2 2c64097 5bfe1bf 699f5e3 bfc574b 699f5e3 bfc574b b9a5bdf c1bdfd2 80726db 30d86e6 80726db 30d86e6 80726db 5bfe1bf 80726db 30d86e6 80726db 5bfe1bf 80726db 99bde0d 92e0d15 99bde0d 5bfe1bf 80726db 5bfe1bf 80726db 30d86e6 80726db 30d86e6 61a2d96 30d86e6 2f897d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import pandas as pd
import joblib
from huggingface_hub import HfApi
import pickle
import yfinance as yf
import keras
from datetime import datetime, timedelta
from forex_python.converter import get_rate
import pandas as pd
import numpy as np
import cpi
from sklearn.preprocessing import MinMaxScaler
from huggingface_hub import hf_hub_download
import gradio as gr
from huggingface_hub import notebook_login
notebook_login()
import hopsworks
from datetime import date
import matplotlib.pyplot as plt
import streamlit as st
st.write("""
# Stock Price Prediction
Shown is the stock prediction of the next working day taking into account the last 10 working days
""")
model = keras.models.load_model('model_stock_prices.h5')
working_days = st.sidebar.slider("Show the historical data of the following last working days", min_value = 10, max_value=20)
working_days = int(working_days)
# downloading the last 10 days to make the prediction
today = date.today()
days_ago = today - timedelta(days=20)
# we get the last 20 days and keep just the last 10 working days, which have prices
nasdaq = yf.Ticker("^IXIC")
hist = nasdaq.history(start=days_ago, end=today)
hist = hist.drop(columns=['Dividends', 'Stock Splits'])
# keeping the last 10 data points
hist = hist[-10:]
inflation = []
for t in hist.index:
inflation.append(get_rate("USD", "EUR", t))
cpi_items_df = cpi.series.get(seasonally_adjusted=False).to_dataframe()
cpi_items_df = cpi_items_df[cpi_items_df['period_type']=='monthly']
cpi_items_df['date'] = pd.to_datetime(cpi_items_df['date'])
cpi_items_df = cpi_items_df.set_index('date')
cpi_df = cpi_items_df['value'].loc['2022':'2023']
cpi_col = []
for x in hist.index:
# ts = datetime(x.year, x.month, 1)
# just adding the latest inflation rate
cpi_col.append(cpi_df[-1])
hist['Inflation'] = inflation
hist['CPI'] = cpi_col
hist['Quarter_end'] = np.where(hist.index.month%3==0,1,0)
s = hf_hub_download(repo_id="marvmk/scalable_project", filename="scaler.save", repo_type='dataset')
scaler = joblib.load(s)
inp = scaler.transform(hist.to_numpy())
df = inp
temp_df = pd.DataFrame(inp, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
ds = []
ds.append(temp_df[0:10])
ds = np.array(ds)
predictions = model.predict(ds)
p = predictions[0][0][0]
p = float(p)
a = np.array([0,0,0,p,0,0,0,0])
a = scaler.inverse_transform(a.reshape(1,-1))
final_prediction = a[-1][3]
prediction = []
#prediction.append(final_prediction)
close = hist['Close'].to_list()
print(close)
for c in close:
prediction.append(c)
prediction.append(final_prediction)
print(prediction)
plt.figure(figsize = (20,10))
plt.plot(prediction, label="Prediction")
plt.plot(hist['Close'].to_list()[-10:], label="Previous")
plt.ylabel('Price US$', fontsize = 15 )
plt.xlabel('Working Days', fontsize = 15 )
plt.title("NASDAQ Stock Prediction", fontsize = 20)
plt.legend()
plt.grid()
st.pyplot(plt)
st.write("""
# Historical prices data
Shown is the historical data of the prices (can be adapted with the values from the sidebar)
""")
today = date.today()
days_ago = today - timedelta(days=25)
# we get the last 30 days and keep just the last working days, which have prices
nasdaq = yf.Ticker("^IXIC")
hist = nasdaq.history(start=days_ago, end=today)
hist = hist.drop(columns=['Dividends', 'Stock Splits'])
# keeping the last working days data points
hist = hist[-working_days:]
inflation = []
for t in hist.index:
inflation.append(get_rate("USD", "EUR", t))
cpi_items_df = cpi.series.get(seasonally_adjusted=False).to_dataframe()
cpi_items_df = cpi_items_df[cpi_items_df['period_type']=='monthly']
cpi_items_df['date'] = pd.to_datetime(cpi_items_df['date'])
cpi_items_df = cpi_items_df.set_index('date')
cpi_df = cpi_items_df['value'].loc['2022':'2023']
cpi_col = []
for x in hist.index:
# ts = datetime(x.year, x.month, 1)
# just adding the latest inflation rate
cpi_col.append(cpi_df[-1])
hist['Inflation'] = inflation
hist['CPI'] = cpi_col
hist['Quarter_end'] = np.where(hist.index.month%3==0,1,0)
hist
|