An automated stock trading system using deep learning
Several articles are available on predicting stock prices, but this article offers two things the reader cannot find in other articles:
- To determine stop-loss and take-profit levels in stock trading, confidence intervals are used
- Stock traders use alpacas to test trading strategies and track profits
Machine learning trading algorithms can benefit from both of these tools.
Here’s the concept:
Three main components will make up the program:
- Setting up the data
The YFinance library will provide data on opening, high, low, and closing prices of assets in daily intervals. We will normalize the data and then reshape it to fit the neural network
- Neural networks
An LSTM convolutional neural network will have the ability to extract the features and access the temporal features. Due to the fact that some of the complex patterns in the data are not only convolutional, but time-based as well, this network fits the data well.
- The creation of orders
Daily opening and closing prices will be predicted by the Neural Network. The network short sells stocks if their opening prices are higher than their closing prices. A network will buy a stock if its closing price is higher than its opening price.
The stop loss and take profit values will be determined after the network has been trained by computing the loss and using this value as a confidence interval. In order to place orders, I will use requests to access the Alpaca API.
Let’s move on to the code now that the key concept has been established.
Here is the code:
The first step involves the following prerequisites:
In order to prevent importing the whole library and taking up space, the prerequisities of the program are spread out in this manner. Clear_output is only available for Jupyter notebooks when imported from IPython.display. Scripts do not need to be imported if you are using them.
Data Access: Step 2:
Yfinance data is split into its respective sections with this function. The program also reshapes data into the following form:
The Convolutional LSTM network can be fitted with a four-dimensional array.
Preparation of data: Step 3:
The challenge does not end with accessing the data. After that, the data needs to be converted into the correct format, and the training and testing datasets need to be separated.
In this function, the sequence is split into chunks of size n_steps in order to create a time series data.
The function converts all data into a value between 0 and 1. There have been many skyrocketing and nosedived stocks. Datapoints with higher values would not be normalized, so the neural network would learn from them. Predictions could be affected by this blind spot. In order to normalize, follow these steps:
The minimum and maximum values of a feature are minimum and maximum, respectively.
In this function, data is shuffled and divided into training and testing datasets using the train_test_split function in Sklearn.
Creating a neural network is the fourth step:
Convolutional LSTM networks have the following basic architecture. This network works best with Adam, the optimizer I found.
Train the neural network in Step 5:
The best weights of the model were saved using the criminally underused Model Checkpoint callback. The dirx variable should be changed to the location where you want to store your model.
Prediction and evaluation at Step 6:
To ensure that you are using the best weights, load the model again after saving the best weights. On the basis of data that it has never seen before, the program evaluates itself. Following that, a set of variables is printed to provide a comprehensive overview of the network’s training.
This is the function that makes the program’s predictions. In order to obtain the USD value, we must reverse the normalization function.
In Step 7 of the process, you will create the order:
These parameters and endpoints can be used to place orders for alpacas. The API key and secret key can be obtained here.
The idea of take profit and stop loss is applied in this function:
Profit from the fluctuation of the close price and prevent loss.
The loss value is the distance between the red and blue borders. As the program predicts the price will fluctuate within the borders, the stop loss and take profit values act as the stop loss and take profit values.
In conclusion:
The prediction of complex patterns is at the heart of both machine learning and stock trading.
As algorithmic trading becomes more popular, I hope more people use the Alpaca API and confidence intervals.a