Skip to content
Search
Generic filters
Exact matches only

Stock Market Prediction through Machine Learning

 

Stock market prediction in machine learning

Introduction

lets discuss stock market prediction through ML. Wall street walk is as rewarding and intense as imaginations would be. You will notice lots of smoked cigarette and sullen faces as well. It the amidst of all craziness anyone could expect from the world’s financial center. Actually, main goal underlying is pretty simple for everyone there. By taking this risk of simplifying things, I will explain you how financing is simply utilizing money (either owned or borrowed) to earn more money. Financing industry does not create value in real, somewhat it uses other factors to get return on your investment.

Stock market is on top of list industries to make potential money. Rule is simple, you only have to crack pattern of stock prices and invest on right time in right place. So, if someone has right prediction at right time, he will be earning Hugh from lowest cash flow available.

Here comes the real challenge, predicting stock prices are not so simple. It involves lots of factors to consider, for example companies’ previous history, current market position, international currency rates, world economy, country economy, relative stock prices in closing index and many more. Its hard for humans to remember all of these factors at once. To handle this issue, it is often practiced that a whole team of analyst jointly works on stock pattern and do insight research to figure out right trends. But stats show that it’s not satisfying even after combining teams of geniuses to identify future stock situations.

At this point you will be thinking why are we discussing stock and its operation in this article? Answer is simple, by adding machine learning with stock previous data, its possible to get stock prices prediction better than humans. But wait! Is it so in real? Let’s Find out…

Agenda of this article will be:

  1. Python library Stocker Module
  2. Moving Averages
  3. Simple Linear Regression
  4. K-Nearest Neighbors/Multilayer Perceptron Results
  5. Conclusions

And we will be following this Github repository to implement some of algorithms using python.

Stock Module

Stock module is a python language library which includes bunch of stock market related functions useful for predictions. A simple implementation of those functions are so satisfying but after tuning its parameters, it produces a lot better results.

Before starting its code implementation, first clone its git hub repository.
!git clone https://github.com/WillKoehrsen/Data-Analysis.git .

Now importing few libraries including Stocker.

stocker module function called plot_stock() to plot Google stock history.

As you look into graph shown above, dates for stock rates are not up to dated. It ends up plotting trend in 2018. Taking closer look to code, its observed that it captures Quandl’s WIKI exchange data. Although this data is updated to date but still it will work for us.
However, we can use stocker to apply technical analysis on stock, but our focus for this article will be on being mediums. Stocker relies on package named as prophet created by Facebook used for additive modeling.

Now divide data into test and training sets. 2014-2016 data will be used for training and for test set 2017 data will be used. After splitting of data lets check accuracy of model.

As you can in result graph, its not well so we have to tune up some parameters to get better results.
Graph shown above states clearly differences between before and after result

To effectively adjust hyperparameters, validation on changepoints is an effective way to get better results on prediction of stock algorithm. Now we will evaluate results of refined model to check if there is some improvements in estimate predictions.

Above graph shows slightly better results than previous model

Now let’s do the ultimate test, time to try luck in stock market.

Finally output graph shows that buying and holding will return more profits or at least positive profits

Data Preparation for Machine Learning

Now moving forward and using machine learning instead of using built in module. We will use google stock data by using function called make_df provided by stocker to contract data for machine learning model

Moving Averages

In short description, moving averages is commonly used technical analysis technique. It is lagging indicator, which means it predicts future rates using past stock prices. It smooths out any kind of fluctuation in short term and over all trend finding. Similarly, we will use it to observe if it helps us in predicting future prices.

Now measure accuracy of model by applying RMS(Root Mean Squared Error):

Lets plot prediction curve:

To find out general trend of the stock by given data, moving average works very well, but it is not useful when we want to see future prediction of prices. Due to that limitation it will not help us in real world stock trading.

Simple Linear Regression

Now we will try to overcome prediction issues generated by moving averages if possible by implementing linear regression algorithm.

firstly create new dataset and don’t mess up with original one.

Now add some extra features to the dataset to make it more robust. And use fastai module to add that random data.

Let’s split train and test data.

Once again, its observed that prediction algorithm is capable of figuring out general trend very efficiently but it fails to predict real prices.

Similarly implementing K-nearest neighbor and Multi-layer perceptron graph (Shown Below) is not satisfying.

K-nearest neighbor
Multi – layered perceptron

Conclusion

So, what does experiment results taught us today? Answer is simple: if you are not like Ray Dalio or Warren Buffet or some other great investor, then there is a Hugh risk and eventually not profitable to beat stock market. Conferring to some sources, most of hedge funds cannot even perform better than S&P 500! Consequently, if you want best return on your investment, you should implement buy and hold strategy.