Deep Learning

Using a TensorFlow Deep Learning Model for Forex Trading

Building an algorithmic bot, in a commercial platform, to trade based on a model’s prediction

Adam Tibi
Adam Tibi
Oct 11 · 8 min read
Image for post
Image for post

Recap

In the previous story, we have trained and tested a model and saved the resulting model as a directory and the scaler used for the data as a file.

Image for post
Image for post
Showing the directory of the model and scaler file in Visual Studio Code

Trading

Trading in this story refers to Algorithmic Trading, also known as Quantitative Trading. Algo trading is when a trading strategy expressed in code, assesses whether a trade could be profitable and executes this trade automatically with minimal human intervention.

Architecture

When we want to expose a software system A to be used by another software system B, we use the term “we are exposing an Application Programming Interface (API) from A”. We call the consumer, in our case the trading platform, as client and we call the producer, in our case the packaged model, as server.

Image for post
Image for post
Image by author, all product names, logos, and brands are property of their respective owners

Server

To expose our model via RESTful API, we need to host it (wrap it) with a web server. Since we are using Python for the model, one popular non-production web server for Python is Flask.

conda install flask
or
pip install flask
@app.route('/predict/<string:ticker>/<int:batch_size>/<int:window_size>/<int:ma_periods>/<float:abs_pips>/<int:pred_size>/<string:instance>/<string:series>', methods=['GET'])def predict(ticker, batch_size, window_size, ma_periods, abs_pips, pred_size, instance, series):
http://localhost:5000/predict/gbpusd/32/256/14/0.0008/4/20200824000100/1.30936,1.309315,1.30932,...,1.30912
length = window_size + moving_average_periods = 256 + 14 = 270
python ./LSTM-FX-Prediction-Server/main.py

Client

We have a prediction server. To use this prediction server, a client needs to supply a URL in the previous format and then gets a prediction.

Results

I ran this bot in backtesting between 24/08/2020 and 30/08/2020 with £1000 capital, having the client server setup above and it made a small profit of £49.

Image for post
Image for post
Backtesting between 24/08/2020 and 30/08/2020. Captured from cTrader

Disclaimer

These stories are meant as research on the capabilities of deep learning and are not meant to provide any financial or trading advice. Do not use this research and/or code with real money.

Extensions and Limitations

REST vs gRPC

REST is the greatest common denominator for trading platforms and for modern systems in general. However, it is not the fastest. A faster and popular API type is gRPC and just to be clear, faster in this sense means a fraction of a second.

JSON

JSON is a data structure that is often used with REST. I have not used JSON on purpose to comply with the vast majority of clients.

Multithreading

Having the model hosted in a web server makes it easy to request multiple predictions at the same time, e.g. within 0.2 seconds gap. This might be the same client requesting multiple predictions or multiple clients requesting multiple predictions at the same time.

Encrypted Predictions

If you are hosting your server remotely, you might consider setting your web server to HTTPS, that is installing an SSL certificate, if you want to have a secure connection between your client and your server. This will mean that if someone can intercept the communication between the client and the server, they will not be able to decipher what is being predicted.

Authorisation and Authentication

If you are hosting your server remotely, it is accessible to the public. Although, it is hard to know what are the expected parameters for it to be useful. You don’t want to leave it to chance, protect it via a RESTful API security protocol.

Hosting your Model

There are professional ways to host your model: The Cloud. You can rent a cloud server virtual machine or go for a serverless option by using an ML hosting platform such as Azure ML.

Conclusion

I hope this clarified the idea of an end-to-end process and using an ML system built with Python from another system built by another programming language and not necessarily located on the same network.

About Me

My background is 20 years in software engineering with specialisation in finance. I work as a software architect in the City of London and my favourite languages are C# and Python. I have a love relationship with practical mathematics and an affair with machine learning.

Towards AI

The Best of Tech, Science, and Engineering.

Sign up for Towards AI Newsletter

By Towards AI

Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive our newsletter right on your inbox. For sponsorship opportunities, please email us at pub@towardsai.net Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Adam Tibi

Written by

Adam Tibi

Software Architect from London with a certificate in Quant Finance and a background in Software Engineering. Passionate about machine learning, C# and Python

Towards AI

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Adam Tibi

Written by

Adam Tibi

Software Architect from London with a certificate in Quant Finance and a background in Software Engineering. Passionate about machine learning, C# and Python

Towards AI

Towards AI is a world’s leading multidisciplinary science publication. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store