Full-stack engineer's Forex algorithmic trading journey


Explore forex trading strategies with EBC's guide by Rogelio, an experienced engineer. Optimize for better returns through algorithmic trading.

Many traders want to suggest their trading strategies in the foreign exchange market, but the international financial situation is turbulent, and the foreign exchange world can sometimes be confusing. EBC hopes that this article provides some ideas on how to start your own foreign exchange trading strategy for you who are currently trading.

The sharer of this article is Rogelio, a proactive full stack engineer with over 13 years of work experience in multiple languages, frameworks, and platforms.

At the beginning, you may know that the foreign exchange market is a trade between currency pairs. But you may not realize that it is the most liquid market in the world. A few years ago, driven by my curiosity, I created a simulation account and created it on MetaSimulated trading on the Trader 4 trading platform is my first step in the world of foreign exchange algorithmic trading.

After a week of simulated trading, my money almost doubled. Under the motivation of my own small achievements in trading, I conducted more in-depth research and also registered for some foreign exchange forums. I spend hours every day studying algorithmic trading systems (rule sets that determine whether you should buy or sell), user-defined indicators, market sentiment, and so on.

My first client

Coincidentally, when I was studying algorithmic trading, I heard that someone wanted to find a software developer to automate a simple trading system. I was learning Java at the timeConcurrent programming in (threads, semaphores, and all garbage collection). I don't think this automation system will be much more complex than the advanced data science course, so I took this job next.

The customer wants to use the algorithmic trading software built by MQL4, which is MetaTrader 4The functional programming language used by the platform to perform transaction related operations. MQL5 has also been released so far. As you might expect, it solves some of the problems with MQL4 and comes with more built-in functions, making trading easier.

Trading platform (MetaTrader in this example)4) The function of is to provide a connection with foreign exchange brokers. Then, the broker provides a platform to provide real-time information about the market and execute buy/sell orders. For beginners in foreign exchange trading, the following is the basic trading interface information:

With MetaTrader 4, you can use internal functions to access all of this data, and access various time ranges: every minute (M1), every five minutes (M5), M15, M30, and every hour(H1), H4, D1, W1, MN.

The current price change is called a ticket. In other words, a tick change is a change in the buying or selling price of a currency. In an active market, there may be many ticks per second. In a stable market, there may be a few minutes without a single tick.

When you place an order through such a platform, you buy or sell a certain amount of a certain currency. You can also set stop loss and stop profit limits. The stop loss limit refers to the maximum number of points (price changes) that you can withstand before closing a transaction. Profit limit refers to the number of points you will accumulate for profit before cashing in.

The customer's algorithmic trading specification is simple: they want a foreign exchange robot based on two indicators. These two indicators need to be very useful when trying to define market conditions and make trading decisions, as they are based on past data (such as the highest price in the past n days). MetaTrader4 has many built-in metrics, but the metrics that my client is interested in come from a custom trading system.

They hope to trade every time two of these custom metrics intersect, and only at a certain angle.

Getting Started

When I started working on it, I learned that the MQL4 program has the following structure:

Preprocessor Instructions

External parameters

Global Variables

Initialization Function

Deinitialize Function

Start function

Customization Function

The start function is for each MQL4The core of the program is that it will be executed every time the market changes (therefore, this function will be executed once every time the price changes). No matter what time range you use, it's always the same. For example, you can create aOperate within the H1 (one hour) time range, but the start function will execute thousands of times within each time range.

To solve this problem, I force the function to execute once per cycle unit:

Obtain the value of the indicator:

Decision logic, including the intersection of indicators and their perspectives:

Send order:


Once I have established my algorithmic trading system, I want to know: 1) whether its execution is appropriate; 2) whether its foreign exchange trading strategy is useful

Backtesting is the process of testing a specific (automated or non automated) system under past events. In other words, you use past data instead of the present to test your system.

MT4Comes with a tool for testing foreign exchange trading strategies (nowadays, there are more professional tools providing more powerful functionality). Firstly, you set a schedule and run your program in a simulated environment; This tool will simulate each minute, knowing that for each unit, it should open at a specific price, close at a specific price, and reach a specified high and low point.

After comparing the operation of the program with historical prices, you will have a good understanding of whether it was executed correctly.

Through backtesting, I checked the return rate of foreign exchange robots over some random time intervals; Undoubtedly, I know that my client will not make money - the metrics and decision-making logic he chooses are not profitable. As an example, the following is an example ofThe result of 164 operations by running the program on the M15 window:

Please note that our balance (blue line) is considered closed below its starting point.

Reminder: It is not always correct to say that a system is "profitable" or "unprofitable". Usually, the system will be profitable/unprofitable for a period of time based on the "sentiment" of the market, which can follow many different chart patterns:

parameter optimization

Although the backtesting has made me aware of this FXI was skeptical about the practicality of robots, but when I started trying to use their external parameters and noticed a huge difference in overall returns, I became curious. This special science is called parameter optimization.

I conducted some rough tests to infer the importance of external parameters on the return rate and came to the following conclusion:

Alternatively, after cleaning up the data:

You may think (as I did) that you should use parameter A. But this decision is not as simple as it seems. Specifically, please note the parametersThe unpredictability of A: For small error values, the return value will undergo significant changes. In other words, parameter A is likely to overestimate future results, as any uncertainty or change will lead to worse performance.

But indeed, the future is uncontrollable, so the return of parameter A is also uncertain. In fact, the best choice is to rely on unpredictability. Usually, parameters with lower maximum returns but better predictability (less volatility) are preferable to parameters with higher returns but poorer predictability.

The only certainty is that you do not know the future of the market and based on past data, it is incorrect to assume that you know the performance of the market. On the contrary, you must acknowledge this unpredictability in foreign exchange trading.

This does not necessarily mean that we should use parameter B, as even if parameter A has a lower return, it is still better than parameter BPerform better; This is just to show you that optimizing parameters may lead to testing exaggerating possible future results.

Precautions for foreign exchange algorithmic trading

Since my first experience in algorithmic foreign exchange trading, I have built several automated trading systems for clients. I can tell you that there is always room for exploration and further optimization analysis to be done. For example, I recently established a system based on searching for so-called 'Big'Fish "motion system; That is to say, in a small unit of time, there is a huge change in the number of points. This is a research that fascinates me.

Building your own foreign exchange simulation system is the best choice to learn more about foreign exchange market transactions, and the possibilities are endless. For example, you can try to interpret the probability distribution of price changes as a function of volatility in a market (such as euros/dollars), and may create a Monte Carlo simulation model using the distribution of each volatility state, with any degree of accuracy you want.

Further reading

Nowadays, there are a large number of tools available for building, testing, and improving trading system automation: Trading for testingBlox, NinjaTrader for trading, OCaml for programming, to name just a few.

Here are some articles I recommend to programmers and enthusiastic traders:

BabyTips: If you are not familiar with foreign exchange trading, this is the starting point.

The Way of the Turtle, written by Curtis Faith: In my opinion, this is the Foreign Exchange Bible. Once you have some trading experience and understand some foreign exchange strategies, you can read it.

technical analysis for the Trading Professional Strategies and TechniquesFor Today's Turbulent Global Financial Markets by Constance M. Brown

Intelligent Trading Programming System - Creating an Automated Trading System for MetaTrader 4 in MQL by Andrew R. Young

Trading Systems – A New Approach to System DevelopmentAnd Portfolio Optimization

Author Urban Jeckle and Emilio Tomasini: Highly technical and focused on foreign exchange testing.

A Step By Step Implementation of a Multi Agent Currency Trading System by RuiPedro Barbosa and Orlando Belo: This is very professional and describes how you can create trading systems and testing platforms.

Overview of Walmart and Its Investment Valuation

Overview of Walmart and Its Investment Valuation

Walmart, a top global retailer with vast reach and a strong supply chain, shows robust sales and financials, making its stock a long-term investment.

Copper futures trading basis and market analysis

Copper futures trading basis and market analysis

Copper futures rules include contract specs, trading hours, and margins. Tracking global trends and indicators helps create effective strategies.

Shanghai index trend analysis and coping strategy

Shanghai index trend analysis and coping strategy

The Shanghai Composite Index reflects the A-share market. Investors should focus on sentiment, indicators, macro-factors, and timing actions wisely.