-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode
115 lines (92 loc) · 3.82 KB
/
code
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
Created on Feb 24 2024
@author Fadhaa Ali
import urllib3
import gc
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
urllib3.disable_warnings()
import matplotlib.pyplot as plt
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error
class Pred:
def __init__(self):
return
def getData(self, fi1):
htt = urllib3.PoolManager()
ml = htt.request('GET', fi1)
ml = ml.data
da = str(ml)
return da
def reformat(self, da):
i = 0
FD = []
lis = []
pc = []
dt = []
no_days_price = da.count('test="relative-most-active-last">')
no_date = da.count('<time dateTime=')
ix2 = 2
for i in range(no_days_price):
ix1 = da.index('test="relative-most-active-last">', ix2, len(da)) + 33
ix2 = ix1 + 6
pc.append(da[ix1:ix2])
ix2 = 2
for i in range(no_date):
ix1 = da.index('<time dateTime=', ix2, len(da)) + 16
ix2 = ix1 + 12
dt.append(da[ix1:ix2])
k = 0
for i in range(1, no_date):
FD.append([dt[i], pc[k], pc[k+1], pc[k+2]])
k += 3
##FD.reverse()
Reformated_data = {
"Date": [FD[i][0] for i in range(len(FD))],
"Open": [float(FD[i][1]) for i in range(len(FD))],
"High": [float(FD[i][2]) for i in range(len(FD))],
"Low": [float(FD[i][3]) for i in range(len(FD))]
}
PriceData = pd.DataFrame(Reformated_data)
return PriceData
def estim(self, PriceData):
model = ARIMA(PriceData['Low'], order=(1, 1, 1))
model_fit = model.fit()
forecast = model_fit.get_forecast(steps=30)
train_size = int(len(PriceData) * 0.8)
train, test = PriceData[0:train_size], PriceData[train_size:len(PriceData)]
model_train = ARIMA(train['Low'], order=(1, 1, 1))
model_train_fit = model_train.fit()
test_forecast = model_train_fit.get_forecast(steps=len(test))
test_forecast_series = pd.Series(test_forecast.predicted_mean, index=test.index)
mse = mean_squared_error(test['Low'], test_forecast_series)
rmse = mse ** 0.5
model2 = ARIMA(PriceData['High'], order=(1, 1, 1))
model_fit2 = model2.fit()
forecast2 = model_fit2.get_forecast(steps=30)
train_size2 = int(len(PriceData) * 0.8)
train2, test2 = PriceData[0:train_size2], PriceData[train_size2:len(PriceData)]
model_train2 = ARIMA(train2['High'], order=(1, 1, 1))
model_train_fit2 = model_train2.fit()
test_forecast2 = model_train_fit2.get_forecast(steps=len(test2))
test_forecast_series2 = pd.Series(test_forecast2.predicted_mean, index=test2.index)
mse2 = mean_squared_error(test2['High'], test_forecast_series2)
rmse2 = mse2 ** 0.5
plt.figure(figsize=(14, 7))
plt.plot(train['Low'], label='Training Low', color='red')
plt.plot(test['Low'], label='Actual Low', color='red')
plt.plot(test_forecast_series, label='Forecasted Low', color='yellow')
plt.plot(train2['High'], label='Training High', color='blue')
plt.plot(test2['High'], label='Actual High', color='blue')
plt.plot(test_forecast_series2, label='Forecasted High', color='green')
plt.fill_between(test.index, test_forecast.conf_int().iloc[:, 0],
test_forecast.conf_int().iloc[:, 1], color='k', alpha=.15)
plt.fill_between(test2.index, test_forecast2.conf_int().iloc[:, 0],
test_forecast2.conf_int().iloc[:, 1], color='k', alpha=.15)
plt.title('ARIMA Model Evaluation')
plt.xlabel('Day')
plt.ylabel('Price')
plt.legend()
plt.show()
return [rmse, rmse2]