Back to Feed
ProjectsFinance

Time series defence stock prediction

Check out the code for this project here

Predicting Stock Prices with LSTM Neural Networks

After building my simple technical analysis pivot point strategy in python I wanted to further develop my skills in algorithmic trading while also developing my skills in AI and python. So I decided to try and predict stock prices with an LSTM neural network. This type of neural network can be used to predict time series of data which is perfect for stock price prediction. This method has the same flaws as regular technical analysis in that it assumes past performance is indicative of future results which is not technically true. However, we can still gain some useful insights from something like this and it is a good exercise to learn more about AI.

How an LSTM works

A Long Short-term memory (LSTM) neural network is a certain type of recurrent neural network (RNN). These work on the prinical of the neral net feeding its own ouputs back into iself. This allows it to predict time series well into the future. However, an issue with regular RNNs is that they suffer from an issue called the "vanising graident". This is where the nueral netw forgers older information quickly.

LSTMs were designed specifically to solve this problem. A cell of an LSTM contains an internal state and three distinct "gates":

  • Forget Gate: Decides what information from the past is no longer relevant and should be thrown away.

  • Input Gate: Decides what new information from the current time step should be added to the memory.

  • Output Gate: Determines what the next hidden state should be.

How my program is structured

To program an LSTM for stock price prediction I used Keras and Tensor flow. I used a 128 unit LSTM layer followed by two 64 unit LSTM layers and then followed by two regular Dense neural net layers (25 and 1 units respectively) to output a final continuous price prediction.

I trained the model on historical data and used the approach of having a "sliding window". Here instead of trying to predict the price based on the price of a single day the data is structured so that the features XX consist of a sequence of previous days and the target variable yy is the price at t+1t+1:

yt+1=f(Xt,Xt1,,Xtn)y_{t+1} = f(X_t, X_{t-1}, \dots, X_{t-n})

2024-04-12-time-series-stock-prediction Not my results (credit)

In this particular program I was actually pulling data from usaspending.gov on defence spending in order to try and predict the price of various defense stocks. Because of this I also had to include a normaliser using MinMaxScaler as the government spending data was much larger than the stock price.

How it performed

While it was able to achieve a very low Mean Squared Error (MSE) on the test set I gave it stock prices are very noisy and even with the extra data from the government spending it was not able to predict real prices very well. So I would likely have to improve it a lot before I gave it any real money to play around with.

What's Next?

If I was to actually go and use this I would likely add and change the following:

  • Predicting Returns vs. Prices: It would be better for my to tray and predict percentage returns instead of trying to predict prices. This often leads to much less overfitting and would give better results.

  • Hyperparameter Tuning: Trying to refine my hyperparameters like my lookback window and batch size dynamically would be beneficial. Currently they are just fixed values. I would likely do this with something like a grid search.

  • Sentiment Analysis Integration: A cool idea would be to mix this program with an LLM to give integrate relevant data about the news relating to defence stocks, i.e. what new wars have started. I would likely use the API of one of the major LLM providers to do this as their LLMs would by far outperform anything I could make with my limited resources.

This was a very fun and insightful exercise that taught me a lot more about AI, quantitative finance and also helped to refine my python programming tools. However, I would not use this program to do any real trading without significant improvement.