Building An Ensemble ML Trading Bot And The Infrastructure Behind It To Make Live trades On The Stock and Forex Market [Python, Tensorflow, Keras, Interactive Brokers, Metatrader]

Around the end of 2019 I had gotten very interested in building machine learning models. I was particularly fascinated by Google’s work on Alpha Zero which uses reinforcement learning to train a hyper-intelligent model quickly. In my second and first year of university, I had spent a lot of time learning about the financial markets and how to create an optimal investment portfolio and profits from shorter-term trading. Following my success in my university’s trading competition, where I placed first by returning 12.01% in 90 days, I had started experimenting with quantitative finance on websites such as Quantopian and CloudQuant. When I discovered that generating alpha from news sentiment and historical stock price movement required the latest technologies and these platforms did not support downloading stock-price minute data or machine learning libraries, I set off to build my own system to trade the market using a very effective type of machine learning model for this application called a random forest classifier.

At this point in time I was really enjoying using jupyter notebooks on google cloud for my machine learning work as it gave me large amounts of computation power that are required for complex machine learning tasks and big data sets. By using google cloud servers I could also have access to the windows operating system which ended up being necessary later on.

In terms of stock choice, I chose to trade relatively small, fast rising stocks on the NASDAQ exchange. I had done some research prior to this project where I had discovered a potential opportunity in this market. I used data from the stock of a software company called Digital Turbine (APPS) when testing my model.

The Model

My Jupyter Notebook

The model was right about buying 63.04% of the time on unseen future stock prices. As each correct buying opportunity resulted in a 2% positive price movement and each incorrect buying opportunity resulted in a 0.5% loss, and these profits and losses were building on each other over a minutely timeframe. I don’t have a screenshot of the return variable in the notebook, but I think it was 1500% in a 30 day period. Brilliant right? Except I had not included the broker’s fees in my ‘CalculatePerformance’ function. I later found out that on these relatively small, fast rising stocks brokers charge around 0.50% as a buying/selling fee. That means -1.00% on every trade, positive or negative. When you take these into account, the system is not profitable at all anymore. Despite this I set out to build the infrastructure to carry out live trades using this model because I thought it was a cool project even if I wasn’t going actually use the system to trade with money.

The Infrastructure

Unfortunately, I did not take any screenshots of the infrastructure in use on my google cloud windows server so won’t be able to show any pictures which is a shame because it looked pretty cool.

Interactive Brokers Trader Workstation

I wanted to trade stocks that had been rising for at least the last three months and had large percentage moves during the day. These were more likely to keep rising and there was more earning potential after deducting the cost of the broker spreads. For this reason, I needed a broker that allowed users to trade stocks using an API. After a lot of searching I found out that Interactive Brokers allowed users to do this through their trader workstation API. The TWS API runs a server that receives and sends messages to the trader workstation client. The code for it looks like this:


Adding the random forest classifier model to this was as simple as importing the pickle file and feeding the price data into it, which it would then output a 1 to buy or 0 to not buy. If the stock price rose 2% it would sell automatically and if it dropped 0.5% below the purchase price it would sell automatically.

When I created my interactive brokers account to test my system on live data, I discovered that stock price feeds require a minimum of $2,000 in your trading account. As I just wanted to test my system this was too big of an amount. Slightly disappointed that I didn’t get to see my system in action I took a break from this project.


A short while later after talking to some people at my university’s investment and trading society which I was the vice-president of at the time, I realised that the forex market does not have restrictions on subscribing to live data feeds as it is a decentralised market. So I started looking for way to use my machine learning model with metatrader.

I discovered there was an open-source wrapper library on github called ‘dwx-zeromq-connector’ that connected python3 with MetaTrader4. I wrote a python script that imported my random forest classifier machine learning model as a pickle file and used it to make predictions that lead to opening trades on the metatrader software.

My Python Script

In the end I had something that looked like this.

An Image From The DWX ZeroMQ Connector Github Repository ( To Demonstrate How My System Looked In Action

Project complete.

I hope you’ve enjoyed reading this post. As always if you want to get in touch, send me an email or message me on twitter.