Predicting Stock Prices With Deep Neural Networks
So in this project, I will walk you through the end to end data science lifecycle of developing a predictive model for stock price movements with Alpha Vantage APIs, and a very powerful ML called LSTM
#@title Load Python libraries
! pip install alpha_vantage -q
# pip install numpy
import numpy as np
# pip install torch
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
# pip install matplotlib
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
# pip install alpha_vantage
from alpha_vantage.timeseries import TimeSeries
print("All libraries loaded")
This python program focuses on importing necessary libraries and modules for our project. At the beginning of the code, it tries to install the alpha_vantage package using the pip package manager. This package is going to provide a way to access financial data via the API.
After that the subsequent lines import various scientific computing and machine leanring libraries.
numpy is imported for numerical operations on array, this is a foundational in processing and manipulating data.
pytorch library is imported with it’s core modules torch, torch.nn, torch.nn.functional and torch.optim. These will be used for building and training neural networks. Utilities for dealing with dataset in Pytorch dataset and DataLoader are also imported, allowing efficient batching and shuffling of data for ML taks.
For visualisation purposes, matplotlib.pyplot is imported, and a specific function figure is imported to modify plots. The alpha_vantage.timeseries import indicates that the code is designed to interact with time series data from Alpha Vantage.
config = {
"alpha_vantage": {
"key": "YOUR_API_KEY", # Claim your free API key here: https://www.alphavantage.co/support/#api-key
"symbol": "IBM",
"outputsize": "full",
"key_adjusted_close": "5. adjusted close",
},
"data": {
"window_size": 20,
"train_split_size": 0.80,
},
"plots": {
"show_plots": True,
"xticks_interval": 90,
"color_actual": "#001f3f",
"color_train": "#3D9970",
"color_val": "#0074D9",
"color_pred_train": "#3D9970",
"color_pred_val": "#0074D9",
"color_pred_test": "#FF4136",
},
"model": {
"input_size": 1, # since we are only using 1 feature, close price
"num_lstm_layers": 2,
"lstm_size": 32,
"dropout": 0.2,
},
"training": {
"device": "cpu", # "cuda" or "cpu"
"batch_size": 64,
"num_epoch": 100,
"learning_rate": 0.01,
"scheduler_step_size": 40,
}
}
This python code is for configuration of dictionary. This code is organised into various sections with settings for API access, data preprocessing, plotting, LSTM model parameters, and training parameters.
The alpha_vantage part of this code holds the API key and details required to fetch the stock data, in our case it is going to be IBM. This will help us pull out the historical stock price information, including the adjusted close prices with account for dividends and stock splits.
Now the data section sets the window size for rolling analysis and the portion of data to be used for training the model. The remainder will be used for validation or testing purposes. In the plots section, multiple setting define the visual parts of how the plots should be displayed, with different colors assigned to actual data, training data, validation, and test predictions, as well as the user interface feature like tick intervals and the x-axis.
Then the model section describes the architecture of the LSTM, specifying the number of layers, the number of units in each layer, and the dropout rate to prevent overfitting.
At the end, the training section includes settings for how the model should be trained, such as the device to use CPU or GPU, batch size, number of epochs iterations through the dataset, learning rate, and learning rate scheduler parameters.
The main purpose of this code snippet is to provide the baseline configuration for an automated system that can ingest historical stock data, learn patterns using a recurrent neural network, and then we make predictions about future stock prices.