File size: 2,695 Bytes
bb19c72
 
 
 
 
 
 
 
 
 
 
 
 
8af2aca
 
bb19c72
 
 
 
 
8af2aca
bb19c72
6fdf06c
 
 
 
 
bb19c72
 
8af2aca
bb19c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import joblib
from huggingface_hub import HfApi
import pickle
import yfinance as yf
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
import matplotlib.pyplot as plt
import streamlit as st
from huggingface_hub import hf_hub_download
from datetime import date
#cpi.update()

# from huggingface_hub import notebook_login
# notebook_login()


m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl")
# model = pickle.load(open(m, 'rb'))

with open(m, "rb") as f:
	model  = pickle.load(f)


# 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)
predictions
p = predictions[0][0]
print(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)