How machine learning can help avoiding human errors in trading strategies

Smart solutions for a real-world strategy

Giovanni Prisco
Apple Developer Academy | Federico II
6 min readApr 5, 2020

--

Giovanni and Vincenzo are two adventurous Computer Engineering students who, after seeing the famous film The Wolf of Wall Street, engaged by the rise of the protagonist Jordan Belfort, as a stock broker, decide to put their love for money in code and start deepen market strategies, with the aim of raising some money with stocks 💸.

Algorithmic Trading

Algorithmic trading is widespread and can be based on different strategies. The one that we chose is the 50–200 strategy. It’s based on the Simple-Moving-Average (SMA) of the previous 50 and 200 days. The investment will be placed in the period where the stock price is lower than the 50 days SMA which has to be lower than the 200 days SMA.

But, how can they get the stock at the lowest price possible, thus maximizing the profit, between the favorable days? How can they model their algorithm for mid-long term investments?

Having discovered this, they decide to build a machine learning model, to find the most convenient solution between the different favorable purchase periods (downtrends), to obtain the greatest gains and, why not, to become rich like Jordan 🤩, without however falling into his catastrophic collapse!

Data? Yes, please.

Machine learning needs data to work. For this project, Alpha Vantage free APIs can be the perfect solution, we will get the historical data for the stock we choose to analyze. The first step is to explore this data, with just some simple operation we can achieve this with python.

Code Output (Data from Alpha Vantage) for TSLA.

This will be the output of a first data visualization, we got from them the SMA and saved them in a .csv for a later use.

At this point, Giovanni and Vincenzo start arguing on which algorithm will better do what they expect, Giovanni says that a regressor would be more accurate than a classifier, Vincenzo disagrees and thinks that a classifier would be more user-friendly. Who will be right? Let’s discuss both of the algorithms.

Photo by Helloquence on Unsplash

The Classifier

Taking a closer look at the chart, we find out that there is a precise pattern: the period of a good investment is characterized by the green line above the orange one which is above the blue one (SMA-200 above the SMA-50 above the close price).

The goal is to label each day with EXCELLENT, VERY GOOD, GOOD or BAD. How can we do that?

This is actually very easy, as said above, in the “good investment” period we get the minimum close price, assign a percentage of 100% to it and calculate percentages to the other prices based on this. This is done for each period, so that every period has a day with a 💯 percentage. Finally we assign the label to percentage intervals (e.g. an EXCELLENT would be 91–100%).

The Regressor

The dataset construction will be much easier here, we will just add a column called “prediction” and for each row assign to it the close price of the next day 🗓. An important note is that the last day (which is today) will have NaN as prediction value, as a matter of fact, we’re not including it in the training part.

The features for this algorithm are pretty much the same as the classifier, the difference is the target 🎯 which is now the predicted price.

The code

Since we are not brokers like Jordan, our only weapon is code! We applied some Data Science to explore data, we cleaned it, and used it for our models. Here is how we actually achieved this.

The only thing to point out is the regressor in action, as you can see it predicted the last 5 days results with a root mean square error of just 0.53. Awesome (for a first test)!

Regressor tests output

We can clearly see the predicted results vs the real ones.

Wrapping up

Finally, we can discover who was right, Giovanni or Vincenzo.

CreateML Classifier Details

On one hand, the classifier we have a 77% confidence in the testing step on 30 classifications (as you can see in the image top, the metrics section). For a first implementation, this is awesome, we have something real to “play” with.

CreateML Regressor Details for TWTR

On the other hand, the regressor, predicted the stock close prices with a root mean square error of just 1.2 on 235 predictions (see image top).

As we can see, the regressor is very useful for short-term investments, as it tells you the predicted price for the next day, while the classifier tells you if it’s a good period or not to buy some stocks and hold them waiting for an uptrend.

Finally, we can say that Giovanni and Vincenzo were both right but under different points of view.

These two solutions allow you to use algorithmic trading in a smarter way with a real-world strategy 🚀. The main advantage of using such an algorithm is that you won’t face the biggest error that you can make in the stock market, which is the human-emotion.

A different approach

After these good results, we wanted to play and to learn other ML algorithms, so we decided to train an object detection model to accomplish the same task but changing our point of view.

We used IBM’s annotation system to mark all the downtrends in charts 📉, built a dataset of 30 images with annotations and given them to CreateML and tested it out.

Taking a look at the results, as we can see, the model recognizes all the periods where the investment is good very quickly.

Results of a test in CreateML

Unfortunately, this approach is useless for our use case, it just detects downtrends on a big chart.

Where to go now?

Let’s clarify that the algorithm is NOT ready-to-go, but it’s a good starting point to make other tests and validations. It’s not valid yet to actually trade in the real world and we are not advising to use this to place investments in the stock market (for now).

The models can be improved to recognize also the best periods in which you should sell 📈; specifically for the classifier it would be the case in which the price is above the 50-days SMA which is above the 200-days SMA.

The regressor could be implemented within a bot 🤖 which buys when it sees that the price will rise of an X% on the next closing.

Conclusion

Giovanni and Vincenzo are very happy of what they achieved 🍸, they learned a lot about stock market and how to use Machine Learning to dive deeper into it, their dream of becoming rich 💰 will have to wait a bit… but it’s surely nearer than they think!

--

--