<jupyter_start><jupyter_text>RNN stock price predictorUsing Recurrent neural network (RNN) and Long short-term memory (LSTM) algorithm, one of deep learning technique, we'll predict selected ticker's future price. Note : This is just a basic code, hence it's not suitable to apply in real life market, and any investment should be your own decision also risk you take.  Recurrent Neural Network (RNN)Recurrent neural networks (RNN) are the state of the art algorithm for sequential data and are used by Apple's Siri and and Google's voice search. In RNN, neuron is called cell, information of previously trained cell is used for next cell, hence it is suitable to process sequential data.[image : RNN]  Long short-term memoryRNN can store short-term memory, to find correlations of data, it needs long term memory, LSTM is used to support this.A common LSTM unit is composed of a cell, an input gate, an output gate and a forget gate. The cell remembers values over arbitrary time intervals and the three gates regulate the flow of information into and out of the cell.  Stock price predictor data cleansing<jupyter_code>import pandas as pd
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt
# To prevent TF complier error
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Model
import tensorflow as tf
from keras.layers import Dense, SimpleRNN, LSTM, Dropout, Bidirectional, Activation
from keras.models import Sequential


    
def get_tickers(ticker):
    data = yf.Ticker(ticker)
    x = data.history(period='5y', auto_adjust=True)
#     x = pdr.get_data_yahoo(tickers, start=date_from)
    df = pd.DataFrame(x[['Open', 'High', 'Low', 'Volume', 'Close']])
    df.fillna(method='bfill', inplace=True)
    df.fillna(method='ffill', inplace=True)
    df.rename(columns={'Close' : 'Close'}, inplace=True)
    return df

def Normalizer(data):
    numerator = data - np.min(data,0)
    denominator = np.max(data, 0) - np.min(data,0)
    return numerator / (denominator + 1e-7)
tickers = 'SPY'
df = get_tickers(tickers)
df
dfx = Normalizer(df)
dfy = dfx[['Close']]


x = dfx.values.tolist()
y = dfy.values.tolist()
dfx.info()
dfy.info()
data_x = []
data_y = []
window_size = 10
for i in range(len(y) - window_size):
    _x = x[i : i + window_size] # not including next day close (i+window_size)
    _y = y[i + window_size] # Next day close
    data_x.append(_x)
    data_y.append(_y)<jupyter_output><empty_output><jupyter_text>Run `print(_x, "->", _y)` to see _x values added to data_x and _y<jupyter_code>print(_x, "->", _y)<jupyter_output>[[0.9843724299632776, 0.9852073706323444, 0.9827284053971, 0.08769871921198157, 0.984239326091595], [0.9876348725716173, 0.987177006001898, 0.9840972322225352, 0.08775706027707432, 0.9782791141465766], [0.9923070414355161, 0.9874986542292876, 0.9824868549493121, 0.11791992863570357, 0.9808791992321277], [0.981875263810349, 0.9778914068699235, 0.9736698950133215, 0.09371241941472348, 0.9749189872871094], [0.9867085758533425, 0.9800218663531719, 0.9664231358518627, 0.1492528445302025, 0.9612783981808417], [0.947156345147413, 0.9613300806721657, 0.9303903454019347, 0.34337077467524585, 0.9357973446066246], [0.945102169091706, 0.9653900615011856, 0.9419046596261941, 0.2133029995641895, 0.9601583558943526], [0.971926792806235, 0.9745550732161028, 0.9667854615235446, 0.11951691447280509, 0.9741189047644228], [0.9815932939759909, 0.9786551680689046, 0.9775751244044402, 0.074226234820905, 0.9777590727143757], [0.9927903480565692, 0.9970655422191586, 0.9900557632837618, 0.11684855009010599, 0.9[...]<jupyter_text>Past 10 days, given data, latest close is 0.8726370803053053, next day close is 0.8677396475977315  Prepare train / test dataset<jupyter_code># train dataset
train_size = int(len(data_y) * 0.7)
train_x = np.array(data_x[0 : train_size])
train_y = np.array(data_y[0 : train_size])

# test dataset
test_size = len(data_y) - train_size
test_x = np.array(data_x[train_size : len(data_x)])
test_y = np.array(data_y[train_size : len(data_y)])<jupyter_output><empty_output><jupyter_text>Build model with SimpleRNN<jupyter_code>model_rnn = Sequential()

model_rnn.add(SimpleRNN(units = 20, activation='relu',
                   return_sequences=True, input_shape=(window_size, 5)))
model_rnn.add(Dropout(0.1))
model_rnn.add(SimpleRNN(units=40, activation='relu'))
model_rnn.add(Dropout(0.1))
model_rnn.add(Dense(units=1))
model_rnn.summary()<jupyter_output>Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
simple_rnn_4 (SimpleRNN)     (None, 10, 20)            520       
_________________________________________________________________
dropout_4 (Dropout)          (None, 10, 20)            0         
_________________________________________________________________
simple_rnn_5 (SimpleRNN)     (None, 40)                2440      
_________________________________________________________________
dropout_5 (Dropout)          (None, 40)                0         
_________________________________________________________________
dense_6 (Dense)              (None, 1)                 41        
=================================================================
Total params: 3,001
Trainable params: 3,001
Non-trainable params: 0
____________________________________________________[...]<jupyter_text>Model fit<jupyter_code>model_rnn.compile(optimizer='adam', loss='mean_squared_error', metrics = ['accuracy'])
history = model_rnn.fit(train_x, train_y, epochs=30, batch_size=30)
pred_y = model_rnn.predict(test_x)<jupyter_output>Epoch 1/30
30/30 [==============================] - 1s 2ms/step - loss: 0.0620 - accuracy: 1.4938e-04
Epoch 2/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0041 - accuracy: 9.9696e-04
Epoch 3/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0028 - accuracy: 7.6576e-04
Epoch 4/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0022 - accuracy: 9.1425e-04
Epoch 5/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0019 - accuracy: 3.6513e-04
Epoch 6/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0020 - accuracy: 0.0027
Epoch 7/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0021 - accuracy: 6.9855e-04
Epoch 8/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0018 - accuracy: 9.9696e-04
Epoch 9/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0016 - accuracy: 0.0043
Epoch 10/30
30/30 [==============================] - 0s 1ms/step - loss: 0.0014 - accuracy[...]<jupyter_text>Compare prediction to real<jupyter_code>plt.figure(figsize=(16,8))
plt.plot(test_y, c='r', label='real stock price')
plt.plot(pred_y, c='blue', label='predicted stock price')
plt.title(f'{tickers} Stock price prediction')
plt.xlabel('time')
plt.ylabel('stock price')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Build model with LSTM<jupyter_code># model = keras.Sequential()
# model.add(layers.LSTM(units=10, activation='relu',
#                       return_sequences=True, input_shape=(window_size, 5)))
# model.add(layers.Dropout(0.1))
# model.add(layers.LSTM(units=10, activation='relu'))
# model.add(layers.Dropout(0.1))
# model.add(layers.Dense(units=1))
# model.summary()

model_lstm = Sequential()
model_lstm.add(LSTM(units=64, activation='relu',
                   return_sequences=True, input_shape=(window_size, 5)))
model_lstm.add(LSTM(units=64, activation='relu',
                   return_sequences=False))
model_lstm.add(Dense(units=32))
model_lstm.add(Dense(units=1))
model_lstm.summary()
model_lstm.compile(optimizer='adam',loss='mean_squared_error', metrics=['accuracy'])
history2 = model_lstm.fit(train_x, train_y, epochs=30, batch_size=30)
pred_y2 = model_lstm.predict(test_x)<jupyter_output>Epoch 1/30
30/30 [==============================] - 1s 4ms/step - loss: 0.0233 - accuracy: 5.7556e-04
Epoch 2/30
30/30 [==============================] - 0s 4ms/step - loss: 7.6926e-04 - accuracy: 0.0043
Epoch 3/30
30/30 [==============================] - 0s 4ms/step - loss: 2.6525e-04 - accuracy: 1.8921e-04
Epoch 4/30
30/30 [==============================] - 0s 4ms/step - loss: 2.5577e-04 - accuracy: 2.7357e-04
Epoch 5/30
30/30 [==============================] - 0s 4ms/step - loss: 2.7694e-04 - accuracy: 1.8921e-04
Epoch 6/30
30/30 [==============================] - 0s 4ms/step - loss: 2.5886e-04 - accuracy: 0.0021
Epoch 7/30
30/30 [==============================] - 0s 4ms/step - loss: 2.2179e-04 - accuracy: 0.0017
Epoch 8/30
30/30 [==============================] - 0s 4ms/step - loss: 2.1918e-04 - accuracy: 0.0021
Epoch 9/30
30/30 [==============================] - 0s 4ms/step - loss: 1.9699e-04 - accuracy: 0.0014
Epoch 10/30
30/30 [==============================] - 0s 4ms/step - los[...]<jupyter_text>Compare prediction to real<jupyter_code>plt.figure(figsize=(16,8))
plt.plot(test_y, c='r', label='real stock price')
plt.plot(pred_y2, c='blue', label='pridected stock price')
plt.title(f'{tickers} Stock price prediction')
plt.xlabel('time')
plt.ylabel('stock price')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluating Model<jupyter_code># Losses
history.history['loss'];
# plot loss vs epochs
plt.figure(figsize=(12,8))
plt.plot(history.history['loss'])
plt.xlabel('Epochs')
plt.ylabel('Losses')
plt.title('Simple RNN model, Loss vs Epoch')
plt.show()
history2.history['loss'];
# plot loss vs epochs
plt.figure(figsize=(12,8))
plt.plot(history2.history['loss'])
plt.xlabel('Epochs')
plt.ylabel('Losses')
plt.title('Simple LSTM model, Loss vs Epoch')
plt.show()<jupyter_output><empty_output><jupyter_text>Predict with Prophet<jupyter_code>df2 = df.reset_index()
pr_df = df2.rename(columns={'Date':'ds', 'Close':'y'})
pr_df.head()
from prophet import Prophet
m = Prophet(daily_seasonality=False)
m.fit(pr_df)
future = m.make_future_dataframe(1, freq='D')
forecast = m.predict(future)
forecast.tail()
fig1 = m.plot(forecast)<jupyter_output><empty_output><jupyter_text>Predict tomorrw's close price<jupyter_code>print(f'RNN predicted {tickers} tomorrow price : {df.Close[-1]*pred_y[-1]/dfy.Close[-1]}')
print(f'LSTM predicted {tickers} tomorrow price : {df.Close[-1]*pred_y2[-1]/dfy.Close[-1]}')
print(f'Prophet predicted {tickers} tomorrow price : [{forecast.loc[forecast.index[-1], "yhat"]}]')<jupyter_output>Prophet predicted SPY tomorrow price : [438.4864717182654]
{'fraction_non_alphanumeric': 0.19518209408194234, 'fraction_numerical': 0.13334597875569043, 'mean_word_length': 5.269322235434007, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '304945', 'license': 'MIT', 'n_tokens_mistral': 4664, 'n_tokens_neox': 3700, 'n_words': 806, 'path': 'portfolio maker/Stock price predictor with RNN, LSTM and Prophet.ipynb', 'repo_name': 'Rsych/Finance-Portfolio-Maker', 'size': '10544'}