Beginning Forex Algorithmic Trading With Python

Paulo Carvalho
Oct 20 · 4 min read

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

Image for post
Image for post
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:

  1. Two helper functions check_sell_criteria and check_buy_criteria where our actual algorithm will reside.
  2. 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.
  3. 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:

  1. 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.
  2. 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:

  1. Developing a functional trading algorithm. Can you come up with a simple algorithm that consistently makes money?
  2. Extending the script to work on partial trading days to benefit from possible market open/close volatility.
  3. Exploring trading across multiple currency pairs. This will increase the frequency with which entry points are detected.

The Startup

Medium's largest active publication, followed by +732K people. Follow to join our community.

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