Can AI/ML help technical analyzers?

Mohammad Mahdi Hosseini
7 min readJul 20, 2021

In this article, I want to share my experience of developing an ML model that can predict stock prices!

But before showing you how my ML models performed in predicting stock prices, I have put a basic explanation of technical analysis for you to read, so you will be more familiar with the task that I expect my model to do.

Technical Analysis:

A good way of analyzing prices and financial markets is technical analysis. This method is popular among traders and investors and is usually used along with fundamental analysis (Involves the analysis of companies’ financial statements, and following up with recent events and news). Technical analysis involves working with charts to predict the future behavior of the market based on the previous prices.

This kind of analysis is most effective on a medium scale, and technical analyzers do not evaluate the exact value of stocks or cryptos, they want to determine their trading strategy and eventually decide for starting their future trades using charts and previous prices.

Technical analysis has three main principles:

Price shows everything about the market: Based on the effective-market hypothesis, the price of a stock or cryptocurrency reflects all of the parameters that affect the market (or can affect the market). These parameters include fundamental factors as well which we neglect in technical analysis.

History will repeat itself: There are many technical strategies that are based on this assumption: the price is more willing to continue its previous trend rather than changing the trend. Many people believe that all of the systems in technical analysis use the same philosophy, and predict the future prices based on repetitive patterns.

Type of the market doesn’t make any difference: Technical analysis can be used for all markets from currency exchange and commodity markets to stock and cryptocurrency markets.

AI is one of the emerging technologies that can help technical analyzers make more reliable predictions, so I decided to develop a model for this purpose. Linear charts are the simplest charts in technical analysis and only demonstrate the close prices in a time frame. Therefore I am going to use linear charts for my model.

In the next few parts of this article, I am going to talk about my stock prediction model.

Decision Tree Regression Model:

Before the emergence of deep learning, decision trees were the golden standard for accuracy and interpretability (here you can read more about them), therefore I decided to make a decision tree model to predict the stock price. I tried Using Sklearn DecisionTreeRegressor to predict different companies’ stock prices using Yahoo datasets. To measure my model error I used RMSE ( Root Mean Square Error) which is as follows :

And this is why I used RMSE:

The most important factors to determine how good an estimator is, are the Variance and Bias of that estimator (These two factors should be minimized).

And we have another factor called the Mean Square Error(MSE) which is equal to:

(where θ is the actual value and θ_hat is the estimator )

We have:

So:

Since θ is a constant we have:

Also, we have:

And finally, we can conclude:

Now that we have:

We can minimize Variance and Bias of our estimator by minimizing the MSE, and RMSE is just the second root of MSE:

So minimizing RMSE gives us better prediction and decreases the error.

The model has an acceptable and relatively low RMSE (around 2.56 for Apple stock), but the chart of predicted values was not very close to reality:

The result of linear regression was even more disappointing😔 . So I started searching for other machine learning methods, and I found Artificial Neural Net as the best method for stock prediction. Now let’s see how my ANN model works.

ANN Model:

After some research, I found out that recurrent neural network (RNN) is a quite good choice for stock price prediction since it is good in predicting the sequences (That’s why it is being used for voice recognition and natural language processing since voice and words are sequences). But there is a small issue with RNNs:

As you can see its short-term memory prevents it from taking the previous data into consideration for making the final decision. In the case of stock price prediction previous data are very important and should always be considered.

LSTM solves this problem for us!

In an LSTM model, we have a forget gate in which the model only forgets some information after changes in the context of the data, and replaces it with new and relevant data. In this way, the useful previous data will be kept and used in the decision-making process.

Finding the best layer orientation:

My next challenge was finding a good orientation of layers for my RNN model. At first, I created a simple model with two LSTM models of 50 neurons and two dense layers with 25 and 1 neurons, and the result was mind-blowing 🤯 .

Here is the orientation of this simple model:

With only 3 epochs (the training dataset had 1765 data points)The model’s RMSE for Apple stock was only 5 dollars, and for Tesla stock, it was around 13 dollars (Tesla stock is really hard to predict since its price skyrocketed in a very short period), which is way better than what I expected.

Here is the result of this model on Apple and Tesla Stock:

RMSE: 13.03422456629136

RMSE: 2.2491807376637176

If we train this model on different company’s stocks and save it, we can see an improvement in the accuracy of the model

Results on Walmart’s stock price before and after being trained on Apple dataset before Walmart dataset:

Before:

RMSE: 0.7956288281609031

After:

RMSE: 0.6992931365966797

Although this model had good accuracy, It still can be improved. Therefore I started making a more complex model with more neurons. I used this popular rule “ The number of hidden neurons should be between the size of the input layer and the size of the output layer. The number of hidden neurons should be 2/3 the size of the input layer, plus the size of the output layer. The number of hidden neurons should be less than twice the size of the input layer.” After trying various neuron orientations (different layers and neurons numbers) using KerasClassifier from keras.wrappers.scikit_learn and GridSearchCV from sklearn.model_selection, I obtained this orientation :

This model has a better performance in predicting stock prices which have less obvious patterns and are harder to predict.

Applications:

Stock price prediction has three main applications:

Investors: The predicted values of companies’ stock prices are quite useful for investors since it expedites the process of decision making for them. Investors can compare their profits of investments in different companies faster and more accurately.

Companies and Firms: Company owners can use the predicted values of their stock price and compare them to their competitors. This data is quite helpful in determinig best strategies for the future. Also, companies can use their competitors’ strategies which are going to experience a considerable increase in their stock prices.

Governments: Stock price prediction can help governments control products’ prices by managing the supply and demand. In addition, using similar sequence prediction models makes them able to predict the currencies exchange rates which are very precious.

Final thoughts:

Although most stock price prediction models are not quite accurate in predicting the exact values of stock prices, they have shown good performance in predicting the trend of prices, which is great! (Although these models can be quite helpful, they should not be considered as the only source for making decisions since models don not consider every factor in their predictions)

As many different factors have direct and indirect effects on stock prices, using previous data alone will not give us a reliable prediction. One possible improvement can be adding a news classifier algorithm into the equation, so our main model can adjust the results based on real-time news analysis (Using Technical analysis along with Fundamental analysis). Overall, there is still a lot of room for improvement of these models, and making a reliable, and accurate price prediction model can be super beneficial for us.

Key takeaways:

· What is technical analysis

· Decision Tree Regression Model (How it works)

· ANN Model (How it works)

· Applications of stock price prediction

· Final thoughts on current price prediction models

--

--