From 3db5d8e1d6eedea393ee2c96a9ce3f2a877ddd87 Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 01:36:37 +0800 Subject: [PATCH 1/6] Added time series analysis --- Analytics/timeseries.py | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Analytics/timeseries.py diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py new file mode 100644 index 0000000..b617fe9 --- /dev/null +++ b/Analytics/timeseries.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- + +# Commented out IPython magic to ensure Python compatibility. +# %pip install pandas_datareader --upgrade + +from datetime import datetime, date +from pandas_datareader import data +import matplotlib.pyplot as plt +import pandas as pd +import os +import warnings +warnings.filterwarnings('ignore') +import numpy as np +from pylab import rcParams +rcParams['figure.figsize'] = 10, 2 +from statsmodels.tsa.arima_model import ARIMA +import math + +spxTicker = '^SPX' +start_date = date(1990, 1, 1) +end_date = date.today() + +panel_data = data.DataReader('^SPX', 'stooq',start_date,end_date) + +panel_data + +close_SPY = pd.DataFrame(index=panel_data.index, data=panel_data['Close']) + +close_SPY.head() + +close = close_SPY['Close'] + +short_rolling_close = close.rolling(window=20).mean() +long_rolling_close = close.rolling(window=100).mean() + +fig, ax = plt.subplots(figsize=(16,9)) + +ax.plot(close.index, close, label='S&P500') +ax.plot(short_rolling_close.index, short_rolling_close, label='20 days rolling') +ax.plot(long_rolling_close.index, long_rolling_close, label='100 days rolling') + +ax.set_xlabel('Date') +ax.set_ylabel('Adjusted closing price ($)') +ax.legend() + + + +df_log = np.log(close) +train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] +model = ARIMA(df_log, order=(3, 2, 1)) +fitted = model.fit(disp=-1) +fc, se, conf = fitted.forecast(len(test_data), alpha=0.05) +future_index = pd.date_range(start = date.today(),periods= len(test_data)) +fc_series = pd.Series(fc, index=future_index) +lower_series = pd.Series(conf[:, 0], index=future_index) +upper_series = pd.Series(conf[:, 1], index=future_index) +# Plot +plt.figure(figsize=(16,9), dpi=72) +plt.plot(np.exp(df_log), label='training') +plt.plot(np.exp(fc_series), color = 'orange',label='Future Predicted Stock Price') +plt.fill_between(lower_series.index, np.exp(lower_series), np.exp(upper_series), + color='k', alpha=.10) +plt.title('S & P 500 Index Price Prediction') +plt.xlabel('Time') +plt.ylabel('Price') +plt.legend(loc='upper left', fontsize=8) +plt.legend(loc='upper left', fontsize=8) From 6e9c0485c3291c4da42f95d4521723a8f957c06e Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 07:18:52 +0800 Subject: [PATCH 2/6] Update timeseries.py --- Analytics/timeseries.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py index b617fe9..f0cbf35 100644 --- a/Analytics/timeseries.py +++ b/Analytics/timeseries.py @@ -46,22 +46,25 @@ df_log = np.log(close) -train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] -model = ARIMA(df_log, order=(3, 2, 1)) -fitted = model.fit(disp=-1) -fc, se, conf = fitted.forecast(len(test_data), alpha=0.05) -future_index = pd.date_range(start = date.today(),periods= len(test_data)) -fc_series = pd.Series(fc, index=future_index) -lower_series = pd.Series(conf[:, 0], index=future_index) -upper_series = pd.Series(conf[:, 1], index=future_index) -# Plot -plt.figure(figsize=(16,9), dpi=72) -plt.plot(np.exp(df_log), label='training') -plt.plot(np.exp(fc_series), color = 'orange',label='Future Predicted Stock Price') -plt.fill_between(lower_series.index, np.exp(lower_series), np.exp(upper_series), - color='k', alpha=.10) -plt.title('S & P 500 Index Price Prediction') -plt.xlabel('Time') -plt.ylabel('Price') -plt.legend(loc='upper left', fontsize=8) -plt.legend(loc='upper left', fontsize=8) +def arima_model(p,d,q): + train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] + model = ARIMA(df_log, order=(p, d, q)) + fitted = model.fit(disp=-1) + fc, se, conf = fitted.forecast(len(test_data), alpha=0.05) + future_index = pd.date_range(start = date.today(),periods= len(test_data)) + fc_series = pd.Series(fc, index=future_index) + lower_series = pd.Series(conf[:, 0], index=future_index) + upper_series = pd.Series(conf[:, 1], index=future_index) + # Plot + plt.figure(figsize=(16,9), dpi=72) + plt.plot(np.exp(df_log), label='training') + plt.plot(np.exp(fc_series), color = 'orange',label='Future Predicted Stock Price') + plt.fill_between(lower_series.index, np.exp(lower_series), np.exp(upper_series), + color='k', alpha=.10) + plt.title('S & P 500 Index Price Prediction') + plt.xlabel('Time') + plt.ylabel('Price') + plt.legend(loc='upper left', fontsize=8) + plt.legend(loc='upper left', fontsize=8) + +arima_model(3,2,1) From 9ead7fb719592e8e7661fda05b77078f696f2fbb Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 07:21:34 +0800 Subject: [PATCH 3/6] Update timeseries.py --- Analytics/timeseries.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py index f0cbf35..902fe65 100644 --- a/Analytics/timeseries.py +++ b/Analytics/timeseries.py @@ -47,6 +47,9 @@ df_log = np.log(close) def arima_model(p,d,q): + if (d > 2): + print("d cannot be more than 2!") + return None train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] model = ARIMA(df_log, order=(p, d, q)) fitted = model.fit(disp=-1) From 434453bd92e653fce7561fc75ebba4bde73a31d4 Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 07:24:33 +0800 Subject: [PATCH 4/6] Update timeseries.py --- Analytics/timeseries.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py index 902fe65..4927148 100644 --- a/Analytics/timeseries.py +++ b/Analytics/timeseries.py @@ -50,6 +50,9 @@ def arima_model(p,d,q): if (d > 2): print("d cannot be more than 2!") return None + else if (p == 1 and d == 1 and q == 1): + print("p,d & q cannot be all 1!") + return None train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] model = ARIMA(df_log, order=(p, d, q)) fitted = model.fit(disp=-1) From af556a415cc2176744881a14c5276715e2c33a6a Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 07:27:28 +0800 Subject: [PATCH 5/6] Update timeseries.py --- Analytics/timeseries.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py index 4927148..6a33299 100644 --- a/Analytics/timeseries.py +++ b/Analytics/timeseries.py @@ -72,5 +72,7 @@ def arima_model(p,d,q): plt.ylabel('Price') plt.legend(loc='upper left', fontsize=8) plt.legend(loc='upper left', fontsize=8) + fname = "SNP"+"-"+str(datetime.date.today())+"("+str(p)+","+str(d)+","+str(q)+")"+'-predictions' + plt.savefig(fname) arima_model(3,2,1) From b1200140ff71c3b927ae751671a81accdcb6a35a Mon Sep 17 00:00:00 2001 From: shinkurono <42473070+shinkurono@users.noreply.github.com> Date: Tue, 31 Mar 2020 07:32:56 +0800 Subject: [PATCH 6/6] Add files via upload --- Analytics/timeseries.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Analytics/timeseries.py b/Analytics/timeseries.py index 6a33299..ebcd5a5 100644 --- a/Analytics/timeseries.py +++ b/Analytics/timeseries.py @@ -44,13 +44,12 @@ ax.legend() - df_log = np.log(close) def arima_model(p,d,q): if (d > 2): print("d cannot be more than 2!") return None - else if (p == 1 and d == 1 and q == 1): + elif (p == 1 and d == 1 and q == 1): print("p,d & q cannot be all 1!") return None train_data, test_data = df_log[:int(len(df_log)*0.85)], df_log[int(len(df_log)*0.85):] @@ -72,7 +71,7 @@ def arima_model(p,d,q): plt.ylabel('Price') plt.legend(loc='upper left', fontsize=8) plt.legend(loc='upper left', fontsize=8) - fname = "SNP"+"-"+str(datetime.date.today())+"("+str(p)+","+str(d)+","+str(q)+")"+'-predictions' + fname = "SNP"+"-"+str(date.today())+"-("+str(p)+","+str(d)+","+str(q)+")"+'-predictions' plt.savefig(fname) arima_model(3,2,1)