-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARIMA.py
52 lines (43 loc) · 1.89 KB
/
ARIMA.py
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
from pmdarima.arima import auto_arima
import pandas as pd
import pickle
import joblib
import numpy as np
import os
#selection and fitting of ARIMA/SARIMA models on the selected futures based on XGBR regression analysis
#generate in-sample predictions for evaluation of training set
def fitARIMA(futures):
futures_List = futures
for i in futures_List:
df = pd.read_csv("tickerData/{}.txt".format(i))
df['DATE']= pd.to_datetime(df['DATE'],format='%Y%m%d')
#filter data to use only training data to fit model
df = df.loc[(df['DATE'] > '19900101') & (df['DATE'] <= '20201231')]
try:
df = df[' CLOSE']
except:
df = df['CLOSE']
daily_return = np.log(df)
#set period as 5 to represent 5 working days per period
#set a range of 0 to 5 for all paramaters
model = auto_arima(daily_return, start_p=0, start_q=0, max_p=5, max_q=5,d = 1, max_d = 5,start_P = 0,D=1,start_Q=0,max_P=5,max_Q=5,max_D=5,m=5, seasonal=True, trace=True,suppress_warnings=True, error_action='warn', stepwise=True,n_fits=50)
# save model
with open('ARIMA/Models/{}.pkl'.format(i), 'wb') as pkl:
pickle.dump(model, pkl)
#saving in-sample prediction for evaluation of fitted model on training data
for i in futures_List:
df = pd.read_csv("tickerData/{}.txt".format(i))
df['DATE']= pd.to_datetime(df['DATE'],format='%Y%m%d')
#filter data to use only training data to fit model
df = df.loc[(df['DATE'] > '19900101') & (df['DATE'] <= '20201231')]
pred = None
with open('ARIMA/Models/{}.pkl'.format(i), 'rb') as pkl:
model = pickle.load(pkl)
pred = model.predict_in_sample(1)
date = df["DATE"].tolist()
date = [int(x.strftime("%Y%m%d")) for x in date]
df_in_sample = pd.DataFrame({"predictions":pred, "date": date})
df_in_sample.to_csv('ARIMA/In_sample_predictions/{}.csv'.format(i), index = False)
if __name__ == '__main__':
futures_List = ["F_ED", "F_ZQ", "F_TU", "F_SS", "F_EB"]
fitARIMA(futures_List)