<jupyter_start><jupyter_text>Import libraries<jupyter_code>import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import RobustScaler
import matplotlib.pyplot as plt
import seaborn as sns
import datetime, pytz
plt.style.use('fivethirtyeight')<jupyter_output><empty_output><jupyter_text>Import Google Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output><empty_output><jupyter_text>Load data from Google Drive<jupyter_code>#define a conversion function for the native timestamps in the csv file
def dateparse (time_in_secs):    
    return datetime.datetime.fromtimestamp(float(time_in_secs))

# load the dataset
dataset = pd.read_csv('/content/drive/My Drive/BTC-2010-2021.csv', delimiter=',', parse_dates=['time'], date_parser=dateparse)
df.head()<jupyter_output><empty_output><jupyter_text>Preparing our data for feeding to model<jupyter_code>cols = list(dataset)[1:5]
df_for_training = dataset[cols].astype(float)
scaler = RobustScaler()
robust_df = scaler.fit(df_for_training)
df_for_training_scaled = scaler.transform(df_for_training)
standardScaler = StandardScaler()
standardScaler = standardScaler.fit(df_for_training)
df_for_training_scaled = scaler.transform(df_for_training)<jupyter_output><empty_output><jupyter_text>Preparing data for network<jupyter_code>#As required for LSTM networks, we require to reshape an input data into n_samples x timesteps x n_features. 
#In this example, the n_features is 2. We will make timesteps = 3. 
#With this, the resultant n_samples is 5 (as the input data has 9 rows).
trainX = []
trainY = []
n_future = 1   # Number of days we want to predict into the future
n_past = 100     # Number of past days we want to use to predict the future

for i in range(n_past, len(df_for_training_scaled) - n_future +1):
    trainX.append(df_for_training_scaled[i - n_past:i, 0:df_for_training.shape[1] + 2])
    trainY.append(df_for_training_scaled[i + n_future - 1:i + n_future, 0:4])

trainX, trainY = np.array(trainX), np.array(trainY)

print('trainX shape == {}.'.format(trainX.shape))
print('trainY shape == {}.'.format(trainY.shape))<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>regressor = Sequential()

#=============================================================

regressor.add(LSTM(units = 100, return_sequences = True, input_shape = (trainX.shape[1], trainX.shape[2])))
regressor.add(Dropout(0.2))

#=============================================================

regressor.add(LSTM(units = 150, return_sequences = True))
regressor.add(Dropout(0.2))
# , activation=tf.nn.relu
#=============================================================

regressor.add(LSTM(units = 160, return_sequences = True))
regressor.add(Dropout(0.2))

#=============================================================

regressor.add(LSTM(units = 140, return_sequences = True))
regressor.add(Dropout(0.2))

#=============================================================

regressor.add(LSTM(units = 120, return_sequences = True))
regressor.add(Dropout(0.2))

#=============================================================

regressor.add(LSTM(units = 60, return_sequences = False))
regressor.add(Dropout(0.2))

#=============================================================

regressor.add(Dense(units = trainY.shape[2]))

regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

history = regressor.fit(trainX, trainY, epochs = 110, batch_size = 128, validation_split=0.1, verbose=1)<jupyter_output><empty_output><jupyter_text>Model History<jupyter_code>plt.plot(history.history['loss'], label='Training loss')
plt.plot(history.history['val_loss'], label='Validation loss')
plt.legend()<jupyter_output><empty_output><jupyter_text>Predict for 30 fay<jupyter_code>predict_n_future = 30
predicted_dataset = []
for i in range(0, n_past):
  predicted_dataset.append(trainX[trainX.shape[0] - 1][i])

predicted_dataset.append(regressor.predict(np.array(predicted_dataset).reshape((1, n_past, trainX.shape[2])))[0])

for i in range(1, predict_n_future):
  predicted_dataset.append(regressor.predict(np.array(predicted_dataset[len(predicted_dataset) - n_past : ]).reshape(1, n_past, trainX.shape[2]))[0])
y_pred_future = scaler.inverse_transform(predicted_dataset)<jupyter_output><empty_output><jupyter_text>Ploting<jupyter_code>datelist_future = pd.date_range(dataset.iloc[-1]['time'], periods=predict_n_future, freq='1d').tolist()
forecast_dates = []
for time_i in datelist_future:
    forecast_dates.append(time_i.date())
    
df_forecast = pd.DataFrame({'time':np.array(forecast_dates), 'open':y_pred_future[n_past:, 0]})
df_forecast['time'] = pd.to_datetime(df_forecast['time'])
original = dataset[['time', 'open']]
original['time'] = pd.to_datetime(original['time'])
# original['time'] = y_pred_train
original = original.loc[original['time'] >= '2020-8-1']
forecast_period_dates1 = pd.date_range(dataset.iloc[n_past + 6]['time'], periods=y_pred_train.shape[0], freq='1d').tolist()
forecast_dates1 = []
for time_i in forecast_period_dates1:
    forecast_dates1.append(time_i.date())
    
df_forecast1 = pd.DataFrame({'time':np.array(forecast_dates1), 'open':y_pred_train})
df_forecast1['time'] = pd.to_datetime(df_forecast1['time'])
df_forecast1 = df_forecast1.loc[df_forecast1['time'] >= '2020-8-1']
sns.set(rc={'figure.figsize':(25,15)})
sns.lineplot(original['time'], original['open'], label = 'original')
sns.lineplot(df_forecast['time'], df_forecast['open'], label = 'future')
sns.lineplot(df_forecast1['time'], df_forecast1["open"], label = 'train')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1999659342531085, 'fraction_numerical': 0.019587804462612844, 'mean_word_length': 8.261829652996845, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '436777', 'license': 'MIT', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1967, 'n_words': 357, 'path': 'Untitled11.ipynb', 'repo_name': 'MohammadMobasher/Binance-Prediction-LSTM', 'size': '5871'}