Note - 8th May 2023

I created this blog in 2020 to document the software development projects I'd worked on in university. This was both for my personal record and also to demonstrate the skills & knowledge obtained. The projects were: Percy AI, RNN Trading Bot, Language Translation Headset. I'd like to expand the scope of this blog and write articles on different topics in the future when I have time.


Building A Recurrent Neural Network Trading Bot And The Infrastructure Behind It To Make Live Trades On The Stock And Forex Market (Python, Tensorflow, Keras, Interactive Brokers, Metatrader)

Towards the end of 2019, I developed a keen interest in building machine learning models. I was especially intrigued by Google’s work on Alpha Zero, which employs reinforcement learning to train a highly intelligent model quickly. During my first and second years at university, I had learned about financial markets, optimal investment portfolios, and short-term trading strategies. After winning my university’s trading competition with a 12.01% return in 90 days, I started exploring quantitative finance on platforms like Quantopian and CloudQuant. However, I realized that generating alpha from news sentiment and historical stock price movements required advanced technologies, and these platforms didn’t support minute data or machine learning libraries. So, I decided to build my own trading system using a random forest classifier, an efficient machine learning model for this application.

Jupyter Notebooks on Google Cloud

I enjoyed using Jupyter notebooks on Google Cloud for machine learning tasks since it provided the computational power required for complex tasks and large datasets. Additionally, it allowed me access to the Windows operating system, which proved necessary later on. For my stock choices, I focused on relatively small, fast-growing NASDAQ stocks, using data from a software company called Digital Turbine (APPS) to test my model.

The Model

_config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml _config.yml My model had a 63.04% accuracy for predicting buying opportunities in unseen future stock prices. With a 2% gain for each correct prediction and a 0.5% loss for each incorrect one, I achieved a 1,500% return within a 30-day period. However, I hadn’t accounted for the 0.50% buying/selling fee charged by brokers for small, fast-growing stocks, which rendered the system unprofitable. Despite this, I continued building the infrastructure for live trading using this model, as I thought it was an interesting project.

The Infrastructure

Unfortunately, I didn’t take any screenshots of the infrastructure in use on my Google Cloud Windows server, so I can’t share any images.

Interactive Brokers Trader Workstation

I wanted to trade stocks that had been rising for at least three months and had large percentage moves during the day. These were more likely to keep rising and provide more earning potential after deducting broker spreads. I found that Interactive Brokers allowed users to trade stocks using an API through their Trader Workstation (TWS) API. The TWS API runs a server that sends and receives messages to the Trader Workstation client.

_config.yml _config.yml

I integrated the random forest classifier model into the TWS API by importing the pickle file and feeding it price data. The model would then output a ‘1’ to buy or a ‘0’ not to buy. If the stock price rose by 2%, the system would automatically sell, and if it dropped by 0.5% below the purchase price, it would also sell automatically.

When I created my Interactive Brokers account to test my system on live data, I discovered that stock price feeds required a minimum of $2,000 in the trading account. This was too large an amount just for testing purposes, so I took a break from the project.

MetaTrader

Later, I learned that the forex market does not have restrictions on subscribing to live data feeds as it’s a decentralized market. So, I started looking for a way to use my machine learning model with MetaTrader.

I found an open-source wrapper library on GitHub called ‘dwx-zeromq-connector’ that connected Python 3 with MetaTrader 4. I wrote a Python script that imported my random forest classifier machine learning model as a pickle file and used it to make predictions, leading to opening trades on the MetaTrader software.

_config.yml

In the end, I had a setup that looked similar to the one shown in the images below. While these images are not mine, they are taken from the ‘dwx-zeromq-connector’ GitHub repository and included here to demonstrate how my system appeared in action.

_config.yml _config.yml

With the project complete, I had successfully built a machine learning model for trading and integrated it with the MetaTrader platform. Although the initial model was not profitable due to broker fees, it was an engaging and educational experience that allowed me to explore the intersection of machine learning and trading. The skills and knowledge gained from this project could potentially be applied to future trading strategies or refined to create more profitable models.

Written on January 2, 2020