Beginning Forex Algorithmic Trading With Python
By: Paulo Carvalho
Tags: Beginners Guide, Forex, Python, Forex Trading, Algorithmic Trading

I finally got around to implementing my first Forex trading robot!

Photo by Clemens Van Lay on Unsplash

Introduction

For years, I have fantasized about building a Forex trading robot that would slowly and consistently increase its account balance. However, I was never really sure where to start. What company to use? How do I access the APIs? Is it safe?

Recently, I blocked off some time to learn what would be needed to develop such a system and it culminated in a very simple to implement python script. In the remainder of this article, I will detail how it works.

Disclosure: This article is not investment, tax, or legal advice, nor is it a solicitation for investment. Furthermore, I am not a financial expert and offer this only as a way to share my experiences.

Methods

Step 1 — Get a Forex Account

The first step is to open an account with a broker. After a bit of research, I decided to go with forex.com. Specifically their standard online account here. They have been around since 2006 and are part of the GAIN Global Markets Inc group which has been around since 1999. Their user base and trading volume seemed to make them a trustworthy platform. However, I have never used another broker so have no basis for comparison.

Creating an account on their platform can be done by following the instructions on their website and will work much the same way as opening an account with any other investment platform or bank.

Once you have created your account, email them at apisupport@gaincapital.com to request access to your API credentials. You will require a $5k balance if you want to trade on your main account or you can opt to have an account created with a virtual balance (not real money) of $50k. I chose the latter.

Step 2 — Write Trader Script

I started reading over the provided API documentation but it left much to be desired. In the hope of streamlining the process, I started searching forex.com’s forums and came across this open source Python client!

Important: Whenever dealing with sensitive information such as financial data it is important to vet any code you use to ensure it is not going to improperly expose your data. Don’t take my vetting of this library for granted. At the time of this writing, most of the library’s relevant code is in this short file. I recommend inspecting the code.

The open source python library abstracts the APIs into simple functions that allows us to retrieve price information, buy/sell currency pairs, check our open orders and more. So how should our trading robot work?

There are many ways that a trading robot can work. Several of which may be better than the one I will present. However, I chose the following approach since it is easy to understand and debug.

Our script will consist of three parts:

Two helper functions check_sell_criteria and check_buy_criteria where our actual algorithm will reside. A configuration step that will be run once when the script is started and will authenticate, get account information and select the currency pair we will be trading. A periodic loop that will retrieve price information, and invoke the helper functions of part 1 to determine based on the most recent price data if it should place a buy/sell order or just continue to next loop. Note: For simplicity we skip partial/no trading days (Fridays, Saturdays and Sundays).

The full script can be found below:

Step 3 — Trading Algorithm

This is the part that I am currently working on and that will require the most time and effort to hone in.

Developing a trading algorithm that consistently makes a profit is no small task. Some guidelines I believe are important for a good trading algorithm:

Profitable: We want an algorithm that nets positive when run over extended periods of time. No tripling the account in a day and then loosing everything in an hour. Predictable: It performs in a manner that is expected given a certain set of conditions. This is useful since it allows proper debugging to ensure it won’t behave unexpectedly under new market conditions.

Since I have not tested my algorithms for long enough, I will not be placing them here and risk having someone lose money. However, a search on Google Scholar (not main Google) shows hundreds of possible algorithms and spending some time looking over trading charts and existing indicators may give you some ideas on how to begin!

Starting with simple algorithms such as checking that current price is below a moving average in order to buy may be a good starting point to learn the script’s behavior.

Conclusion and Future Work

The simple Python trading script shown above is able to trade a currency pair using the forex.com platform.

However, as with most things worth doing: There is still much to explore. Including: