<jupyter_start><jupyter_text>Bi directional LSTM Model---  1. Import required libs<jupyter_code>import pandas as pd
import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, Bidirectional
from keras.callbacks import ModelCheckpoint
from keras.models import load_model

from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>---  2. Load and visualise Data  2.1. Load dataset<jupyter_code>ticker = "MSFT"
month = "1"
path = f"../TDL_Project_Data/{ticker}_year1_month{month}.csv"
stock = pd.read_csv(path, parse_dates=['time'])
stock.head()<jupyter_output><empty_output><jupyter_text>2.2. Close price of stock along with bollinger bands and volume<jupyter_code>fig, ax1 = plt.subplots(figsize=(20, 10))
ax2 = ax1.twinx()  # set up the 2nd axis
    
ax1.set_xlabel("Time")
ax1.set_ylabel("Price")
ax1.set_title(f'Stocks price and volume')
    
ax1.plot(stock['time'][:100], stock['close'][:100], label='Close price', marker = 'o', color='b')
ax1.plot(stock['time'][:100], stock['SMA'][:100], label='Simple moving Average', color='g')

ax1.plot(stock['time'][:100], stock['up_band_2'][:100], label='', color='r')
ax1.plot(stock['time'][:100], stock['low_band_2'][:100], label='positive sentiment percentage', color='r')
    
ax2.set_xlabel("Time")
ax2.set_ylabel("Volume")
ax2.set_title(f'Stocks price and volume')
# ax1.set_ylim(0, 100)
ax2.bar(stock['time'][:100], stock['volume'][:100], width=0.0001, alpha=0.2)
ax2.grid(b=False)

ax2.legend()
plt.title("")<jupyter_output>No handles with labels found to put in legend.<jupyter_text>2.3. RSI, MACD, MACD Signal<jupyter_code>fig = plt.figure(figsize=(20, 5))
    
ax1 = plt.subplot2grid((1,2),(0,0))
labels = ['', '']
plt.plot(stock['time'][:100], stock['RSI'][:100])
plt.plot(stock['time'][:100], [20 for i in range(100)], color='r')
plt.plot(stock['time'][:100], [80 for i in range(100)], color='r')
plt.ylim(0, 100)
plt.title(f'RSI')
    
ax2 = plt.subplot2grid((1,2), (0, 1))
plt.plot(stock['time'][:100], stock['MACD'][:100])
plt.plot(stock['time'][:100], stock['MACD_Signal'][:100])
# plt.plot(stock['time'][:100], [20 for i in range(100)])
# plt.plot(stock['time'][:100], [80 for i in range(100)])
# plt.ylim(0, 100)
plt.title(f'MACD and MACD Signal')<jupyter_output><empty_output><jupyter_text>---  3. BI LSTM Model  3.1. Scale and Reshape data<jupyter_code>scaler = StandardScaler()
lstm_data = stock.copy()
lstm_data.head()
X = lstm_data.copy()
X.drop(
    ['time', 'price_1_min_ahead', 'price_2_min_ahead', 'price_3_min_ahead', 'price_5_min_ahead', 'price_10_min_ahead', 'company', 'change', 'gain', 'loss', 'avg_gain', 'avg_loss', 'rs'],
    axis='columns',
    inplace=True
)
X.head()
X_matrix = X.values
X_matrix.shape
X_matrix = scaler.fit_transform(X_matrix)
print("Before reshaping: ", X_matrix.shape)
X_matrix = X_matrix.reshape(X_matrix.shape[0], X_matrix.shape[1], 1)
print("After reshaping: ", X_matrix.shape)
y = np.array(lstm_data['price_1_min_ahead'])
print("Before reshaping: ", y.shape)
y = y.reshape(y.shape[0], 1)
print("After reshaping: ", y.shape)
y = scaler.fit_transform(y)<jupyter_output><empty_output><jupyter_text>3.2. Split to train and test set<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X_matrix, y, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>3.3. Define and train model<jupyter_code>model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(16, 1)))
model.add(Bidirectional(LSTM(64, activation='relu', return_sequences=True)))
model.add(LSTM(64, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.summary()
checkpoint = ModelCheckpoint("./SavedModels/BI_LSTM_best_model_1.h5", monitor='loss', verbose=1,
    save_best_only=True, mode='auto', period=1)

model.fit(X_train, y_train, epochs=5, batch_size=1, callbacks=[checkpoint])<jupyter_output>WARNING:tensorflow:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of batches seen.
Epoch 1/5
 197/6974 [..............................] - ETA: 1:15 - loss: 0.4364<jupyter_text>3.4. Make and test predictions<jupyter_code># Upload a model if required
model = load_model('./SavedModels/BI_LSTM_best_model_1.h5')
model.summary()
len(X_test)
len(y_test)
predictions = model.predict(X_test)
predictions
y_test
print('FOR NORMALIZED PRICES')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, predictions))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, predictions))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
y_prices = scaler.inverse_transform(y_test)
y_prices
predictions_prices = scaler.inverse_transform(predictions)
predictions_prices
print('ERROR VALUES FOR ACTUAL PRICES')
print('Mean Absolute Error:', metrics.mean_absolute_error(y_prices, predictions_prices))
print('Mean Squared Error:', metrics.mean_squared_error(y_prices, predictions_prices))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_prices, predictions_prices)))<jupyter_output>ERROR VALUES FOR ACTUAL PRICES
Mean Absolute Error: 0.3539209723516337
Mean Squared Error: 0.17454453082404636
Root Mean Squared Error: 0.4177852687973169<jupyter_text>3.5. Performance in binary trading<jupyter_code>X_pred = model.predict(X_matrix)
X_pred
X_pred_prices = scaler.inverse_transform(X_pred)
X_pred_prices
y
X_target_prices = scaler.inverse_transform(y)
X_target_prices
len(X_pred_prices)
len(X_target_prices)
len(stock['close'])
def get_trades(actual_prices, target_prices):

    pred_labels = []
    for price in actual_prices - target_prices:
        # print(price)
        if price < 0:
            pred_labels.append(1)
        else:
            pred_labels.append(0)
    
    return pred_labels

X_pred_labels = get_trades(stock['close'], X_pred_prices.reshape(X_pred_prices.shape[0]))
X_target_labels = get_trades(stock['close'], X_target_prices.reshape(X_target_prices.shape[0]))
metrics.accuracy_score(X_pred_labels, X_target_labels)
metrics.precision_score(X_pred_labels, X_target_labels, average=None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15717684377478192, 'fraction_numerical': 0.04250594766058684, 'mean_word_length': 6.952080706179067, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 34, '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': '268065', 'license': 'MIT', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2338, 'n_words': 445, 'path': 'Models/BiLSTM.ipynb', 'repo_name': 'Varun487/BinaryOptions_DeepLearning', 'size': '6305'}