Inspiration

Warren Buffet once said, "Market fluctuations are your friend, not your enemy." But not everyone is as good at investing as Warren Buffet, so it is no simple task to be comfortable with market volatility. But what if we didn't have to be agitated or tense about making quality investments? What if there was a way to know general stock price movements? While there are indicators, they aren't extremely reliable and most apply to short-term trading. But most people are too busy at work to watch the markets only to make small gains. This was the issue that inspired me to implement neural networks for predicting market trends. Why use AI? AI has been growing at a rapid rate and there is no doubt that it is a key technological advancement for society. However, there are a lot of missed opportunities in its application to the real world. One of which is in the realm of investing in the stock market. Not everyone can do the complex math to figure out whether it will be a bull market or a bear market but with AI, it can be drastically simplified to just code. This makes it accessible to anyone allowing people to make accurate investing decisions.

What it does

In its essence, it will predict the direction of the price in a market. It doesn't predict the exact value or price as no one can possibly predict such a minute detail - not even a computer. But as buy-and-hold investors, it isn't really necessary to know the exact price we need to sell. Knowing the general trend and approximately how long each market (bear/bull) will last is enough to make profitable investments.

How I built it

I integrated the model using an LSTM network. LSTM stands for Long-Short-Term-Memory. It is a type of recurrent neural network (RNN) designed to address the limitation of traditional RNNs when dealing with long-term dependencies and large amounts of sequential data. LSTMs are extremely popular and useful for natural-language processing (NLP), speech recognition, and time-series analysis which is where stock predictions would fall into. The LSTM architecture itself includes memory cells, input gates, forget gates, and output gates. The key to LSTMs is the memory cell, a unit that can store information for long durations. The memory cell acts as a conveyor belt, allowing information to flow through while maintaining and updating its content. This architecture makes it extremely effective for time-series data. The data comes from Yahoo Finance which can be accessed using the yfinance library in Python. The stock I picked was Nvidia from 2012 to 2022 (Ticker: NVDA) but it doesn't affect its training in any way. But before the data can be given to the model, it needs be to normalized or simplified so the model can analyze it better. This is done by using a MinMaxScaler which comes from the sklearn library and setting the range from 0 to 1. Then, the data is split into training data + labels and testing data + labels. This can be done manually or through the use of for-loops as it is pretty intuitive. Next, we build the neural network. For this project, we used 2 LSTM layers and 2 Dense layers. When compiling the model, the optimizer was "adam" and the loss function was "mean_squared_error". The data was trained only for 1 epoch since the data itself is quite large as we are using the daily closing prices. After training is done, the model will make predictions which will be shown in a graph at the end. This graph would model the actual historical stock data, the validation data, and the model's prediction. After running the code a couple of times, I noticed that each time, the model was pretty accurate in making trend predictions. It would be offset by a bit once in a while but as mentioned before, it doesn't matter a whole lot.

Challenges I ran into

There were 2 main challenges. The first one was picking the proper architectures for the neural network. Choosing an LSTM model is what makes this price predictor so accurate and it is unlikely similar results would occur had a traditional RNN been used. To solve this issue, I coded a few different models before picking the most accurate one. The second challenge was normalizing data. While the code is pretty simple, the reasoning behind why the closing prices are changed to a 0 or a 1 is a little more confusing. This problem was addressed by reading research papers that analyze the MinMaxScaler at a high level. Overall, coding the parts that all machine learning projects have in common wasn't the most difficult part. Rather, it was the specific sections of code that were best optimized for LSTM networks that were tricky.

Accomplishments that I'm proud of

As an individual working on this project, I am most proud of learning and implementing all the new knowledge of LSTMs and neural networks into something I am truly passionate about. Connecting two of my favorite hobbies made this project the most fun one yet.

What I learned

I learned a lot about time-series forecasting and RNNs. I also learned how to read research papers more effectively when it came time to go more in-depth about LSTMs. Overall, I gained a lot of interesting knowledge and I hope to continue to apply it for future projects and hackathons.

What's next for LSTM Neural Network for Predicting Stock Market Trends

One major improvement I would like to add in the future is to save the model and use APIs to constantly give the most recent stock prices, making the entire project applicable to dynamic markets. It would also be really cool to post this on a website where a user can pick a stock and how far into the future they want the model to predict and output a graph of the predictions.

Built With

Share this project:

Updates