Forecasting Stock Using Deep Learning Along With Indicators
Today I am going to show you how to prediction stock prices using deep learning. Also add indicators like sell and buy on chart.
Steps involved:
Baseline Model
ARIMA
Sentiment Analysis
Feature Selection with XGBoost
Deep Neural Network
Pattern Recognition by Hand
Q-Learning
Import Libraries
import os
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import seaborn as sns
import warnings
import numpy as np
from numpy import array
from importlib import reload # to reload modules if we made changes to them without restarting kernel
from sklearn.naive_bayes import GaussianNB
from xgboost import XGBClassifier # for features importance
warnings.filterwarnings('ignore')
plt.rcParams['figure.dpi'] = 227 # native screen dpi for my computer
First of all we are going to import various libraries such as os, pandas, matplotlib, seaborn, warnings, numpy and other important libraries. It filters out warning messages, set the DPI of matplotlib, for more clearer plots.
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.graphics.tsaplots import plot_pacf, plot_acf
from sklearn.metrics import mean_squared_error, confusion_matrix, f1_score, accuracy_score
from pandas.plotting import autocorrelation_plot
This code imports a bunch of stuff from different libraries to help us predict stock prices using machine learning. The statsmodels library has all sorts of tools for time series analysis and modeling, including ARIMA and SARIMAX models. We can use functions like plot_pacf and plot_acf to plot autocorrelation and partial autocorrelation plots, which can help us understand how the data is correlated with itself over time. The scikit-learn library has functions for evaluating our model’s performance, like mean squared error, confusion matrix, f1 score, and accuracy score. And the pandas.plotting library can help us plot autocorrelation plots for our data.
import tensorflow.keras as keras
from tensorflow.python.keras.optimizer_v2 import rmsprop
from functools import partial
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Flatten, TimeDistributed, LSTM, Dense, Bidirectional, Dropout, ConvLSTM2D, Conv1D, GlobalMaxPooling1D, MaxPooling1D, Convolution1D, BatchNormalization, LeakyReLU
from bayes_opt import BayesianOptimization
from tensorflow.keras.utils import plot_model
This code imports all the libraries and functions we need to predict stock prices using machine learning and Bayesian optimization. We’re using TensorFlow Keras for our machine learning model, and Bayesian optimization to help us tune the model’s hyperparameters.
import functions
import plotting
This code imports two custom modules: functions
and plotting
. These modules were likely written specifically for this stock prediction project.
The functions
module contains functions that do all sorts of things related to stock prediction, like cleaning the data, creating new features, training models, and evaluating models.
The plotting
module contains functions that make it easy to visualize the results of the stock prediction, like plotting the actual stock prices vs. the predicted values, or showing how different models performed.
By importing these modules, the code can access all the functionality they provide and use it for its stock prediction task.
np.random.seed(66)
This line of code tells the numpy library to use the number 66 as its random seed. This means that every time the program is run, it will generate the same sequence of random numbers.
This is useful for machine learning because it allows us to get consistent results each time we run our program. This is important because it allows us to compare different models and algorithms fairly, and to make sure that our results are reliable.