Stock Market Prediction: Train a neural network to predict stock prices


During the month of December last year, I started researching on the stock market and I stumbled upon helpful information I thought I would share. I learned about the analysis needed in order to excel as the 1% of stock traders. I asked myself, “But what if I combined some machine learning to predict stock prices?” Now, we are ready to cram years worth of work in weeks! Strap on and let’s start the journey.

History and Background of The Stock Markets

A stock is simply a theoretical percentage of ownership of a company’s value. The concept of trading commodities and debts started out in the 16th Century. Even so, the modern concept of the stock markets was started by the Dutch later on. In those days, large amounts of capital was needed to start large companies. This amount was impossible to be raised by a single individual . Thus, investors pulled funds together and divided the company’s value among themselves. The percentages were in order of their contributions, of course.

Stock market quotes
Stock Market Price Quotes at the London Stock Exchange

The Modern Stock Markets

Thereafter, the concept of shares became widely adopted and thus a centralized marketplace was needed. A group of stock traders met at a London coffee shop to do the trades. Soon in 1773, they bought the coffee shop and converted it into the London Stock Exchange we know of today. The idea of a stock market spread to other countries and it has now become global.

Basically, in order for your investment to be profitable it means that the price of the overall shares must be higher than the invested amount. However, it is very difficult to know when the price will go up. The price of stocks is controlled by a number of factors that makes it complex to predict. That is why I named the title ‘predict stock prices’ and not ‘make money from the stock market.’ Either way, you will see how you can predict stock prices with minutes of training.

Analysis of the Stock Market

In stock market analysis, there is three key things that you need to follow:

Fundamental analysis: It entails assessing the monetary value of the organization. It keeps in mind the economic conditions of where the company is. It is also dependent on the financial conditions and the overall performance of the company. A thorough investigator would most definitely look into the reports, budgetary proportions and other information that could be utilized to anticipate the eventual fate of an organization. The technique utilizes incomes, profit, future development, return on value, overall revenues and other information to decide an organization’s fundamental esteem and potential for future development.

Technical Analysis: In contrast to fundamental analysis, the expert basically examines the pattern and trends in the shares’ prices. The basic presumption is that market prices are a function of the supply and demand for the stock, which thus mirrors the value of the organization. This strategy additionally trusts that past patterns are a sign of future trends.

Sentimental Analysis: Market sentiment is the general speculation towards a specific security or stock available in the stock market. The attitude of the investors influence this highly and it could be triggered by the news or just a report released by the company.

Training the Neural Network

Artificial Intelligence has progressed over the years. Typical AI’s can do years worth of work in weeks depending on the computational power provided. In this article, we will be focusing on technical analysis. To see sentimental analysis in use check out this article.

We will use a subset of Machine Learning called Deep Learning which is based on learning data representations. We will then try to make educated guesses by using probability calculations and training our model to spot the patterns in our dataset. Our deep learning model will be supervised in that we will give it inputs and set the parameters. We will also provide it with an expected output to train on.

We will obtain our dataset from the Investing website which has data ranging from economic calendars to past opening and closing prices in the stock market. After scraping the stock market closing prices, we will train an LSTM Network to find long-term patterns in our dataset. The dataset should be a continuous column of closing prices. Do not add any heading. Add zeros to blank data (normalization). If by any chance you are not aware of web scraping, follow this article.

The Code:

The link to the LSTM Network I used can be found here. Otherwise, let’s jump in to the actual code used to predict the stock market quotes.

#Importing the dependencies needed for the project.
from keras.layers.core import Dense, Activation, Dropout
from keras.models import Sequential
from keras.layers.recurrent import LSTM
import lstm, time #helper libraries

#Loading the data. You may need to convert the XLS to CSV
X_train, y_train, X_test, y_test = lstm.load_data('eur/usd.csv', 50, True)
#Building the model
model = Sequential()




start = time.time()
model.compile(loss='mse', optimizer='rmsprop')
print 'compilation time : ', time.time() - start

#Step 3 Train the model

#Step 4 - Plot the predictions!
predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
lstm.plot_results_multiple(predictions, y_test, 50)

The prediction

As you can see, the model worked with some degree of success. It predicted the big leaps successfully. However, it had problems with the small leaps. The training data was at least seven years long and the test data was two years long. For better results, you could either train the model longer or use more test data. Either way, I will repeat by saying that the stock market is highly volatile and is driven by a lot of factors.

In the next article, we will be combining sentiment analysis and technical analysis. The sentiment analysis will include designing a bot which scrapes news data from Investopedia and The data obtained will be fed through a model that uses NLP (Natural Language Processing) to discern alerting news that can influence prices in the stock market.

Hope this article has helped you in one way or another. Feel free to comment if you get stuck.

More Resources on stock market predictions:

Stock Price Prediction | AI in Finance | Siraj Raval

Reference: Siraj Raval – How to train a neural network to predict stock prices #7

How to train a neural network to predict stock prices | Mark Gacoka
Article Name
How to train a neural network to predict stock prices | Mark Gacoka
Artificial Intelligence has progressed over the years. Typical AI's can do years worth of work in weeks depending on the computational power provided. In this article, we will be focusing on technical analysis.
Publisher Name
Mark Gacoka Website
Publisher Logo

Leave a Reply