- How does automated trading software work?
- My First Client
- Broker and Market Data Adapters
- What is automated trading software?
- Hands On
- Make your success your future
- Parameter Optimization, and its Lies
- How I Built A Profitable Forex Trading Algo
- Forex Trading Software
- Further Reading
- Markets and Instruments
- Understanding the basics
- What is Forex trading all about?
- How does Forex make money?
- What is backtesting a trading strategy?
- What is algorithmic trading?
How does automated trading software work?
As you may know, the Foreign Exchange (Forex, or FX) market is used for trading between currency pairs. But you might not be aware that it’s the most liquid market in the world.
A few years ago, driven by my curiosity, I took my first steps into the world of Forex algorithmic trading by creating a demo account and playing out simulations (with fake money) on the Meta Trader 4 trading platform.
After a week of ‘trading’, I’d almost doubled my money.
Spurred on by my own successful algorithmic trading, I dug deeper and eventually signed up for a number of FX forums. Soon, I was spending hours reading about algorithmic trading systems (rule sets that determine whether you should buy or sell), custom indicators, market moods, and more.
My First Client
Around this time, coincidentally, I heard that someone was trying to find a software developer to automate a simple trading system.
This was back in my college days when I was learning about concurrent programming in Java (threads, semaphores, and all that junk).
I thought that this automated system this couldn’t be much more complicated than my advanced data science course work, so I inquired about the job and came on-board.
The client wanted algorithmic trading software built with MQL4, a functional programming language used by the Meta Trader 4 platform for performing stock-related actions.
MQL5 has since been released.
As you might expect, it addresses some of MQL4's issues and comes with more built-in functions, which makes life easier.
The role of the trading platform (Meta Trader 4, in this case) is to provide a connection to a Forex broker. The broker then provides a platform with real-time information about the market and executes your buy/sell orders. For readers unfamiliar with Forex trading, here’s the information that is provided by the data feed:
Through Meta Trader 4, you can access all this data with internal functions, accessible in various timeframes: every minute (M1), every five minutes (M5), M15, M30, every hour (H1), H4, D1, W1, MN.
The movement of the Current Price is called a tick.
In other words, a tick is a change in the Bid or Ask price for a currency pair. During active markets, there may be numerous ticks per second. During slow markets, there can be minutes without a tick.
The tick is the heartbeat of a currency market robot.
When you place an order through such a platform, you buy or sell a certain volume of a certain currency. You also set stop-loss and take-profit limits.
Broker and Market Data Adapters
The stop-loss limit is the maximum amount of pips (price variations) that you can afford to lose before giving up on a trade. The take-profit limit is the amount of pips that you’ll accumulate in your favor before cashing out.
If you want to learn more about the basics of trading (e.g., pips, order types, spread, slippage, market orders, and more), see here.
The client’s algorithmic trading specifications were simple: they wanted a Forex robot based on two indicators.
For background, indicators are very helpful when trying to define a market state and make trading decisions, as they’re based on past data (e.g., highest price value in the last n days).
What is automated trading software?
Many come built-in to Meta Trader 4. However, the indicators that my client was interested in came from a custom trading system.
They wanted to trade every time two of these custom indicators intersected, and only at a certain angle.
As I got my hands dirty, I learned that MQL4 programs have the following structure:
- [Preprocessor Directives]
- [External Parameters]
- [Global Variables]
- [Init Function]
- [Deinit Function]
- [Start Function]
- [Custom Functions]
The start function is the heart of every MQL4 program since it is executed every time the market moves (ergo, this function will execute once per tick).
This is the case regardless of the timeframe you’re using. For example, you could be operating on the H1 (one hour) timeframe, yet the start function would execute many thousands of times per timeframe.
To work around this, I forced the function to execute once per period unit:
Getting the values of the indicators:
The decision logic, including intersection of the indicators and their angles:
Sending the orders:
If you’re interested, you can find the complete, runnable code on GitHub.
Once I built my algorithmic trading system, I wanted to know: 1) if it was behaving appropriately, and 2) if the Forex trading strategy it used was any good.
Backtesting (sometimes written “back-testing”) is the process of testing a particular (automated or not) system under the events of the past. In other words, you test your system using the past as a proxy for the present.
MT4 comes with an acceptable tool for backtesting a Forex trading strategy (nowadays, there are more professional tools that offer greater functionality).
To start, you setup your timeframes and run your program under a simulation; the tool will simulate each tick knowing that for each unit it should open at certain price, close at a certain price and, reach specified highs and lows.
After comparing the actions of the program against historic prices, you’ll have a good sense for whether or not it’s executing correctly.
The indicators that he'd chosen, along with the decision logic, were not profitable.
From backtesting, I’d checked out the FX robot’s return ratio for some random time intervals; needless to say, I knew that my client wasn’t going to get rich with it—the indicators that he’d chosen, along with the decision logic, were not profitable.
Make your success your future
As a sample, here are the results of running the program over the M15 window for 164 operations:
Note that our balance (the blue line) finishes below its starting point.
One caveat: saying that a system is "profitable" or "unprofitable" isn't always genuine.
Often, systems are (un)profitable for periods of time based on the market's "mood," which can follow a number of chart patterns:
Parameter Optimization, and its Lies
Although backtesting had made me wary of this FX robot’s usefulness, I was intrigued when I started playing around with its external parameters and noticed big differences in the overall Return Ratio. This particular science is known as Parameter Optimization.
I did some rough testing to try and infer the significance of the external parameters on the Return Ratio and came up with something like this:
Or, cleaned up:
You may think (as I did) that you should use the Parameter A.
How I Built A Profitable Forex Trading Algo
But the decision isn’t as straightforward as it may appear. Specifically, note the unpredictability of Parameter A: for small error values, its return changes dramatically.
In other words, Parameter A is very likely to over-predict future results since any uncertainty, any shift at all will result in worse performance.
But indeed, the future is uncertain! And so the return of Parameter A is also uncertain. The best choice, in fact, is to rely on unpredictability. Often, a parameter with a lower maximum return but superior predictability (less fluctuation) will be preferable to a parameter with high return but poor predictability.
The only thing you can be sure is that you don’t know the future of the market, and thinking you know how the market is going to perform based on past data is a mistake. In turn, you must acknowledge this unpredictability in your Forex predictions.
Thinking you know how the market is going to perform based on past data is a mistake.
Forex Trading Software
This does not necessarily mean we should use Parameter B, because even the lower returns of Parameter A performs better than Parameter B; this is just to show you that Optimizing Parameters can result in tests that overstate likely future results, and such thinking is not obvious.
Since that first algorithmic Forex trading experience, I’ve built several automated trading systems for clients, and I can tell you that there’s always room to explore and further Forex analysis to be done.
For example, I recently built a system based on finding so-called “Big Fish” movements; that is, huge pips variations in tiny, tiny units of time. This is a subject that fascinates me.
Building your own FX simulation system is an excellent option to learn more about Forex market trading, and the possibilities are endless.
For example, you could try to decipher the probability distribution of the price variations as a function of volatility in one market (EUR/USD for example), and maybe make a Monte Carlo simulation model using the distribution per volatility state, using whatever degree of accuracy you want.
I’ll leave this as an exercise for the eager reader.
The Forex world can be overwhelming at times, but I hope that this write-up has given you some points on how to start on your own Forex trading strategy.
Nowadays, there is a vast pool of tools to build, test, and improve Trading System Automations: Trading Blox for testing, NinjaTrader for trading, OCaml for programming, to name a few.
I’ve read extensively about the mysterious world that is the currency market.
Markets and Instruments
Here are a few write-ups that I recommend for programmers and enthusiastic readers:
- BabyPips: This is the starting point if you don’t know squat about Forex trading.
- The Way of the Turtle, by Curtis Faith: This one, in my opinion, is the Forex Bible. Read it once you have some experience trading and know some Forex strategies.
- Technical Analysis for the Trading Professional — Strategies and Techniques for Today’s Turbulent Global Financial Markets, by Constance M.
- Expert Advisor Programming – Creating Automated Trading Systems in MQL for Meta Trader 4, by Andrew R. Young
- Trading Systems – A New Approach to System Development and Portfolio Optimisation, by Urban Jeckle and Emilio Tomasini: Very technical, very focused on FX testing.
- A Step-By-Step Implementation of a Multi-Agent Currency Trading System, by Rui Pedro Barbosa and Orlando Belo: This one is very professional, describing how you might create a trading system and testing platform.
Understanding the basics
What is Forex trading all about?
Forex (or FX) trading is buying and selling via currency pairs (e.g.
USD vs EUR) on the foreign exchange market.
How does Forex make money?
Forex brokers make money through commissions and fees. Forex traders make (or lose) money based on their timing: If they're able to sell high enough compared to when they bought, they can turn a profit.
What is backtesting a trading strategy?
Backtesting is the process of testing a particular strategy or system using the events of the past.
What is algorithmic trading?
Algorithmic trading is when a robot/program uses a set of rules that tell it when to buy or sell.