This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time as tm | |
import datetime as dt | |
import tensorflow as tf | |
# Data preparation | |
from yahoo_fin import stock_info as yf | |
from sklearn.preprocessing import MinMaxScaler | |
from collections import deque |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install yahoo_fin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SETTINGS | |
# Window size or the sequence length, 7 (1 week) | |
N_STEPS = 7 | |
# Lookup steps, 1 is the next day, 3 = after tomorrow | |
LOOKUP_STEPS = [1, 2, 3] | |
# Stock ticker, GOOGL | |
STOCK = 'GOOGL' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LOAD DATA | |
# from yahoo_fin | |
# for 1104 bars with interval = 1d (one day) | |
init_df = yf.get_data( | |
STOCK, | |
start_date=date_3_years_back, | |
end_date=date_now, | |
interval='1d') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove columns which our neural network will not use | |
init_df = init_df.drop(['open', 'high', 'low', 'adjclose', 'ticker', 'volume'], axis=1) | |
# create the column 'date' based on index column | |
init_df['date'] = init_df.index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Scale data for ML engine | |
scaler = MinMaxScaler() | |
init_df['close'] = scaler.fit_transform(np.expand_dims(init_df['close'].values, axis=1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's preliminary see our data on the graphic | |
plt.style.use(style='ggplot') | |
plt.figure(figsize=(16,10)) | |
plt.plot(init_df['close'][-200:]) | |
plt.xlabel("days") | |
plt.ylabel("price") | |
plt.legend([f'Actual price for {STOCK}']) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def PrepareData(days): | |
df = init_df.copy() | |
df['future'] = df['close'].shift(-days) | |
last_sequence = np.array(df[['close']].tail(days)) | |
df.dropna(inplace=True) | |
sequence_data = [] | |
sequences = deque(maxlen=N_STEPS) | |
for entry, target in zip(df[['close'] + ['date']].values, df['future'].values): | |
sequences.append(entry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GetTrainedModel(x_train, y_train): | |
model = Sequential() | |
model.add(LSTM(60, return_sequences=True, input_shape=(N_STEPS, len(['close'])))) | |
model.add(Dropout(0.3)) | |
model.add(LSTM(120, return_sequences=False)) | |
model.add(Dropout(0.3)) | |
model.add(Dense(20)) | |
model.add(Dense(1)) | |
BATCH_SIZE = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GET PREDICTIONS | |
predictions = [] | |
for step in LOOKUP_STEPS: | |
df, last_sequence, x_train, y_train = PrepareData(step) | |
x_train = x_train[:, :, :len(['close'])].astype(np.float32) | |
model = GetTrainedModel(x_train, y_train) | |
last_sequence = last_sequence[-N_STEPS:] |
OlderNewer