<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
#for deep learning model
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout<jupyter_output><empty_output><jupyter_text>Strategy1* Here only the nextprice is predicted<jupyter_code># load the dataframe
df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/lstm_stocks/excel_data.txt', delimiter  = '\t')
df = df.set_index('Date Time')
df.columns = ['open', 'high', 'low', 'last', 'volume', 'num_of_trades']
df = df.drop(labels = 'num_of_trades', axis = 1)

# reverse such that latest dates are in the last
df = df[::-1]

# split to training and testing data
train_set = df[:int(len(df)*0.98)].values
test_set = df[int(len(df)*0.98):].values
print("Train size: ",train_set.shape)
print("Test size:",test_set.shape)

# scaling the data
scaler = MinMaxScaler()
train_set = scaler.fit_transform(train_set)

# arrange data for X_train and y_train
# number_of_values is the number of time frame used as X_train for y_train
number_of_values = 12
X_train, y_train = [], []
column = 0
for i in range(number_of_values, len(train_set)):
  X_train.append(train_set[i-number_of_values:i])
  #here we will predict the open price, hence column = 0
  y_train.append(train_set[i, column])
X_train, y_train = np.array(X_train), np.array(y_train)
y_train = np.reshape(y_train, (-1,1))
print("X_train size: ",X_train.shape)
print("y_train size:",y_train.shape)

# neural LSTM network

lstm_model = Sequential()

# first layer
lstm_model.add(LSTM(units = 50, 
                    activation = 'relu', 
                    return_sequences = True, 
                    input_shape = (X_train.shape[1], X_train.shape[2])))
lstm_model.add(Dropout(rate = 0.2))

# second layer
lstm_model.add(LSTM(units = 60, 
                    activation = 'relu', 
                    return_sequences = True))
lstm_model.add(Dropout(rate = 0.3))

# third layer
lstm_model.add(LSTM(units = 80, 
                    activation = 'relu', 
                    return_sequences = True))
lstm_model.add(Dropout(rate = 0.4))

# final layer
lstm_model.add(LSTM(units = 120, 
                    activation = 'relu'))
lstm_model.add(Dropout(rate = 0.5))

# output layer
lstm_model.add(Dense(units = 1))

# compile the model
lstm_model.compile(optimizer = 'adam', loss = 'mean_squared_error')
# fit the model
lstm_model.fit(X_train, y_train, epochs = 30, batch_size = 640)

# test dataset reshaping
past_days = train_set[-12:]
test_set_transform = scaler.transform(test_set)
test_set_days = np.concatenate([past_days, test_set_transform], axis = 0)
X_test, y_test = [], []
column = 0
for i in range(number_of_values, len(test_set_days)):
  X_test.append(test_set_days[i-number_of_values:i])
  #here we will predict the open price, hence column = 0
  y_test.append(test_set_days[i, column])
X_test, y_test = np.array(X_test), np.array(y_test)
y_test = np.reshape(y_test, (-1,1))
print("X_test size: ",X_test.shape)
print("y_test size:",y_test.shape)

# inverse transform of scaled data
scale = MinMaxScaler()
scale.min_, scale.scale_ = scaler.min_[0], scaler.scale_[0]
y_pred = lstm_model.predict(X_test)
y_pred = scale.inverse_transform(y_pred)
y_test = scale.inverse_transform(y_test)

# Plots
plt.figure(figsize = (15,10))
plt.plot(df.index[int(len(df)*0.98):], y_test, color = 'b', label = 'Original price')
plt.plot(df.index[int(len(df)*0.98):], y_pred, color = 'r', label = 'Predicted price')
plt.xlabel('Dates')
plt.ylabel('Price in dollars')
plt.xticks(rotation = 90)
plt.legend()
plt.show()
# load the dataframe
df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/lstm_stocks/excel_data.txt', delimiter  = '\t')
df = df.set_index('Date Time')
df.columns = ['open', 'high', 'low', 'last', 'volume', 'num_of_trades']
df = df.drop(labels = 'num_of_trades', axis = 1)

# reverse such that latest dates are in the last
df = df[::-1]

# split to training and testing data
train_set = df[:int(len(df)*0.98)].values
test_set = df[int(len(df)*0.98):].values
print("Train size: ",train_set.shape)
print("Test size:",test_set.shape)
df.describe().T
# import plotly.graph_objects as go

# fig = go.Figure(data = [go.Candlestick(
#                                     x = df.index,
#                                     open = df['open'],
#                                     close = df['last'],
#                                     high = df['high'],
#                                     low = df['low'])] )
# fig.show()
# arrange data for X_train and y_train
# number_of_values is the number of time frame used as X_train for y_train
number_of_values = 30
# future_time_predict = 6
X_train, y_train = [], []
column = 0
train_set_series = train_set[:,0]
for i in range(number_of_values, len(train_set_series) - number_of_values):
  X_train.append(train_set_series[i-number_of_values:i])
  #here we will predict the open price, hence column = 0
  y_train.append(train_set_series[i:i+int(number_of_values * 0.2)])
X_train, y_train = np.array(X_train), np.array(y_train)
# y_train = np.reshape(y_train, (-1,1))
print("X_train size: ",X_train.shape)
print("y_train size:",y_train.shape)

# normalizing the dataset
x_scaler = MinMaxScaler()
X_train = x_scaler.fit_transform(X_train)
y_scaler = MinMaxScaler()
y_train = y_scaler.fit_transform(y_train)

# reshape the inputdata
X_train = np.reshape(X_train, newshape = (X_train.shape[0], X_train.shape[1], 1))
print("after transformation, X_train size: ",X_train.shape)

# multistep prediction model

# neural LSTM network

lstm_model_multi_step = Sequential()

# first layer
lstm_model_multi_step.add(LSTM(units = 64, 
                    activation = 'selu', 
                    return_sequences = True, 
                    input_shape = (X_train.shape[1], X_train.shape[2])))
lstm_model_multi_step.add(Dropout(rate = 0.2))

# second layer
lstm_model_multi_step.add(LSTM(units = 32, 
                    activation = 'selu', 
                    return_sequences = True))
lstm_model_multi_step.add(Dropout(rate = 0.3))

# third layer
lstm_model_multi_step.add(LSTM(units = 32, 
                    activation = 'selu', 
                    return_sequences = True))
lstm_model_multi_step.add(Dropout(rate = 0.4))

# final layer
lstm_model_multi_step.add(LSTM(units = 32, 
                    activation = 'relu'))
lstm_model_multi_step.add(Dropout(rate = 0.5))

# output layer
lstm_model_multi_step.add(Dense(units = (number_of_values * 0.2)))

# compile the model
lstm_model_multi_step.compile(optimizer = 'adam', loss = 'mean_squared_error')
# fit the model
lstm_model_multi_step.fit(X_train, y_train, epochs = 50, batch_size = 640)

# arrange data for X_test and y_test
# number_of_values is the number of time frame used as X_test for y_test
number_of_values = 30
X_test, y_test = [], []
test_set_series = test_set[:,0]
past_days = train_set[-number_of_values:,0]
test_set_days = np.concatenate([past_days, test_set_series], axis = 0)
for i in range(number_of_values, len(test_set_days) - number_of_values):
  X_test.append(test_set_days[i-number_of_values:i])
  #here we will predict the open price, hence column = 0
  y_test.append(test_set_days[i:i+int(number_of_values * 0.2)])
X_test, y_test = np.array(X_test), np.array(y_test)
# y_train = np.reshape(y_train, (-1,1))
print("X_test size: ",X_test.shape)
print("y_test size:",y_test.shape)

# normalizing the dataset
x_scaler = MinMaxScaler()
X_test = x_scaler.fit_transform(X_test)
y_scaler = MinMaxScaler()
y_test = y_scaler.fit_transform(y_test)

# reshape the inputdata
X_test = np.reshape(X_test, newshape = (X_test.shape[0], X_test.shape[1], 1))
print("after transformation, X_test size: ",X_test.shape)

# inverse transform of scaled data
y_pred = lstm_model_multi_step.predict(X_test)
y_pred = y_scaler.inverse_transform(y_pred)
y_test = y_scaler.inverse_transform(y_test)

# Plots
for i in range(10):
  plt.figure(figsize = (10,5))
  plt.plot(y_test[i], color = 'b', label = 'Original price')
  plt.plot(y_pred[i], color = 'r', label = 'Predicted price')
  plt.xlabel('Dates')
  plt.ylabel('Price in dollars')
  plt.xticks(rotation = 90)
  plt.legend()
  plt.show()
for i in range(10):
  print(y_pred[i])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1316322880648623, 'fraction_numerical': 0.015380946703231192, 'mean_word_length': 4.142857142857143, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '467524', 'license': 'MIT', 'n_tokens_mistral': 3184, 'n_tokens_neox': 3026, 'n_words': 660, 'path': 'lstm_futures_forecasting.ipynb', 'repo_name': 'arjunjanamatti/finance_and_trading', 'size': '8387'}