Download MetaTrader 5

How to Make a Trading Robot in No Time

19 July 2012, 09:54
MetaQuotes Software Corp.
2
31 743

To Make a Trading Robot, You Need a Trading System

Trading on financial markets involves many risks including the most critical one - the risk of making a wrong trading decision. The dream of every trader is to find a trading robot, which is always in good shape and not subject to human weaknesses - fear, greed and impatience.

How to make a trading robot in no time

Each newcomer wants to get or create a clear and strict trading system that can be presented in the form of algorithms and completely get rid of routine operations. Is it possible?

A trading system is a necessary condition for entering the market and that system should be profitable, of course. When newcomers come to the market, they are usually overwhelmed by the great mass of information difficult to grasp. Books and trader forums can provide some help in that case.

Unfortunately, not all authors are successful traders and not all successful traders write books. Many special web resources are created only to earn profit for their owners, as it is much more difficult to trade your own money than to issue forecasts and teach trading systems.

Each trader should independently pass all stages of a trading system creation. There is a popular saying that it does not matter what system you use for trading, the main thing is that you should really trade according to that system. Otherwise, trading on the market turns into a gamble with a predictable result.


Trading Robots and Forex

Forex market is believed to have a great liquidity. Also, it allows trading 24 hours a day, unlike many other markets. Therefore, many traders try to make trading robots specially for Forex market, as it offers a large number of trading instruments.

However, skeptics claim that all currency pairs are strongly correlated with each other providing very low volatility in the market. But their opponents respond that each currency pair has its own features and low volatility is compensated by a big leverage.

In any case, Forex instruments are attractive for making trading robots and most supporters of the automated trading hone their skills on currency pairs.

MetaTrader 4 and MetaTrader 5 trading terminals are specially designed to easily develop automated trading systems but at the same time their interface is also convenient for manual trading.


How to Start Making a Trading Robot?

There are many approaches to building an automated trading system. We will describe only a few major ones.

The first approach rests on math. A developer tries to create a sort of an equation that can consider many factors. This approach is based on the firm belief that price movements are managed by a model that can be found using available historical data.

In most cases, the followers of such an approach know too much math but know nothing about/are not interested in the market. The market is a pure abstraction, a type of an intellectual game for them. This approach usually leads to many years of study and development, while a definite result in the form of a working automated trading system is not so important.

The second approach is based on studying the market laws. No attempts are made to understand why the price goes up or down when various technical analysis figures appear on a chart. The advantage of this approach is that it requires no special knowledge of mathematics and makes no assumptions about the market driving force.

It is most clear and convenient when studying trading. It is most popular among traders who received universal recognition. The disadvantage of the approach is the necessity to constantly track all necessary symbols.

Sooner or later, a trader starts considering automation of trading processes and the most considerable issue appears at that stage – complexity of formalizing trading rules when trying to express them in the form of algorithms. In some cases traders who try to order a trading robot cannot describe trading rules and find common ground with programmers.

The third approach is based on the attempt to create a «black box» based on neural networks with the use of the ready-made tools widely available in special software and math packages. Creation of an automated trading system with the elements of the artificial intelligence is an exciting and challenging task even for newcomers, as it requires neither deep mathematical background, nor programming experience - everything is done using visual aids.

A trader should know the basics of technical indicators, possess an ability to prepare necessary price data and experience in some definite package for working with neural networks. The main drawback of this approach is that a trading robot obtained using such specialized tools for working with neural networks is actually a "black box". Traders do not know its working principles and, generally, it is impossible to predict what market phase will be the most problematic for the robot.

Programmers often choose the fourth approach – they start making a trading robot from the very beginning without spending time for manual trading. Why trade manually? You can make a robot spending a few months and reap the benefits of your efforts then.

But «no pains, no gains». In most cases, programmers start creating all necessary infrastructure using a familiar programming language rather than just making a trading robot – getting and processing price data, visual representation of charts and indicators, custom means of testing strategies on historical data and so on.

They gain much experience in the process. But in most cases, that experience does not bring them closer to the final goal – creation of an automated trading system. And even if a trading robot is created, there is no guarantee that it will be profitable. And what if a programmer wants to write another trading system? Deep restructuring and new programming errors are inevitable.

There is also the fifth approachbuying a ready-made trading system in the form of a trading robot. In this case, a trader acts as an operator or a tuner. This approach saves much time (no need to learn many new things) and allows traders to quickly enter the world of the automated trading.

The main drawback of this approach stems from its advantages – you don't know the operation principles of your trading robot and its structure. And even if a seller has provided you with a detailed description of the implemented trading system, you will never be completely sure in it.

However, neither of the mentioned approaches can give you absolute guarantee except a bank deposit. But that is not a very suitable solution for people interested in market trading and ways to increase their private assets.


What is the Best Approach to the Automated Trading for a Trader?

Each of the five described approaches has its advantages and corresponds to some definite type of trader. It is unlikely that you will choose the first approach (market analytical description) without good mathematical background. It is equally unlikely that you will start from making trading robots based on neural networks. However, both of these approaches are very exciting and provide good intellectual exercise.

Below we will discuss only the second approach, which is already considered to be the classical one. That is the approach usually chosen by new followers of the automated trading, as the technical analysis remains the key knowledge area when learning trading basics.

Another advantage of the second approach is that after you spend some time for manual trading and obtain the sense of the market, you will already have a good understanding of technical analysis tools. Besides, you will be able to program trading strategies or create neural networks on a higher level.


The First Steps in Making a Trading Robot

To make an automated trading system, you need programming skills and knowledge of all intricacies of trade requests processing. But first you can start from the ready-made Expert Advisors – trading robots from the free Code Base library.

Download any Expert Advisor (trading robot) and launch it in the Strategy Tester of MetaTrader 4 or MetaTrader 5 client terminals. Select a history interval showing a strong trend and an interval with a flat. Perform optimization of an Expert Advisor input parameters and examine their differences at these two intervals.

Launch an Expert Advisor with the optimal parameters for a flat on a trend interval and with the optimal parameters for a trend on a flat interval. Examine the differences in trading results, deals distributions and other statistical parameters. As a result, you will know how much the behavior of your trading system can vary when the market situation changes.

It would be better to try several standard trading strategies using this method on different parts of history and various symbols. Such a trial run prevents from fitting a trading system for some definite history interval and provides better understanding of trend and countertrend systems.

The next step would be to create more complex trading systems based on the combination of already existing simple signals from MQL5 Wizard set. You can test and develop your trading intuition sorting out bad signals of one system using a filter based on another system without programming means.

The main thing here is not to overachieve. The more input parameters a trading system has, the easier it is to be fitted. There have been a lot of discussions about the differences between optimization and fitting. There are no widely accepted solutions here. But visualization of test/optimization results and your own common sense can help you.

Learn to identify the most critical input parameters affecting your trading system from the whole set of input data. Do not pay much attention to secondary parameters that take time during optimization but do not affect the very logic of the system. Remember that a good trading system always demonstrates a small free motion of secondary parameters but it does not display dramatic volatility in case of inconsiderable market changes.

You can spend as much time at this stage, as you wish, until you are sure that you can understand any trading strategy examining test and optimization results. The knowledge of strengths and weaknesses of standard systems will allow you to be better prepared when creating your own trading robot.


Programming a Trading Robot

Suppose that you have learned/are learning MQL4 or MQL5 programming language and now you are ready to write your first Expert Advisor for MetaTrader client terminal. Several cases are possible here.

First, you can examine several ready-made trading robots described in the articles to better understand programming intricacies.

Second, you can ask questions on MQL4.community or MQL5.community, if you have any unresolved issues. Experienced community participants usually help the newcomers showing sincere interest in the subject.

Third, you can order imrpovement or development of an Expert Advisor or an indicator in Jobs service, if you are not able to write a necessary program on your own. But even if you make an order via the freelance service, you should have some idea about strategy testing to find a common language with a developer.

Besides, basic knowledge of a programming language allows you to implement minor fixes and changes into the code after the work has been already completed. After all, it would not be too convenient to call a programmer to fix every small issue you encounter. It would be much more easier and faster to fix it yourself.


No Need to Reinvent the Wheel

How to find your own trading strategy, or at least in what direction should you focus your search? All traders protect their own trading systems, if they have one. All newcomers want to create a profitable system or get a ready-made one. At the same time, any obtained solution seems to be too simple compared to newcomers' ideas about a genuine trading system.

How to find your own trading strategy, or at least in what direction should you focus your search?

Army men all over the world are prone to excessive levels of secrecy. There are many jokes about that including the following one: "The military secret is not in what you are studying, - an officer says to military school students, - but in the fact that exactly you are studying it". The situation with trading systems is similar enough: most traders use simple and well-known trading ideas with minor modifications, for example, adding Trailing Stop or confirmations from trend indicators.

There are plenty of trader forums with limited access where participants join their efforts to develop or improve some secret trading systems. Most interesting thing is that such systems do not contain anything special at all. Usually a well-known idea (like "trade with the trend") is used as a basis. Then it is perfected with some new indicators unknown to general public.

Therefore, you can easily take available trading robot source codes and try to use them correctly with various symbols and timeframes. Another popular saying can be mentioned here: "You don't like cats? You just don't know how to cook them!" It is hard to believe but the probability that you will develop something really new is very small. The main thing here is to create a system using available ingredients. Do not think that some geniuses have access to some secret systems from NASA laboratories. That is the secret of the Grail.


Only a Few Will Make It Through

So, why does no one use trading ideas, if they are literally within arm's reach? The answer probably lies in human psychology. The staff of many banks and large investment funds includes traders performing deals according to strict rules and within limited volumes. But for some reasons, only a few institutional traders leave their companies and start trading using their own money.

It turns out that you need not only a trading strategy but also the iron discipline to follow it. Many traders found out with regret that they also have the same psychological problems described in books. After realizing that the worst enemy of traders are themselves, a newcomer starts thinking about making a trading robot to eliminate a psychological burden.

Though I slightly deviate from the topic, I should mention the legendary Turtles traders who successfully traded on multiple markets in the late 20th century. Read "Way of the Turtle" and you will see that the most important thing for a trader is a self-discipline and not some top secret system. Alas, most newcomers will not be able to follow a profitable strategy, even if they get it for free.

The problem is that most trading strategies that are perfectly fitted for manual trading can hardly be formalized and transcribed to a programming language. The strategies that can be easily formalized (for example, those involving two moving averages' intersection) are too simple and require a lot of refinements and improvements, so they can be used in practice. Thus, a simple idea is gradually complicated by a plenty of external parameters preventing a trading robot from false entries and errors clearly visible for a developer. A trading robot optimization issue emerges. This process should not turn into an overoptimization and fitting for a particular history interval.

To address this issue, the forward testing using the obtained system parameters has been implemented in MetaTrader 5 terminal. If the forward testing results do not significantly differ from the ones obtained in the optimization section, there is a probability that a trading robot will be stable enough for some time after its launch on a trading account. A length of an interval for parameters optimization and an actual value of that "some time" depend on a certain trading system.

Thus, the optimization of a trading robot before launching it on a trading account reminds of unwinding a sling - the more carefully we have unwound and flung a projectile from the sling, the farther it will fly and the more accurate its trajectory will be. A thoroughly developed trading robot will keep a positive result on a trading account for a longer time than a trading robot obtained as a result of a fitting. We can say that the Grail is a working idea and correct adjustment of parameters performed from time to time at the moments of market conditions changes.

This can be illustrated by the results of the Automated Trading Championship that is held for many years already. Submitted Expert Advisors of all the participants pass through automatic tests on the time interval from January to the end of July. The main requirement for passing the automatic test is a profit earned for eight months of testing. But less than a half of trading robots admitted for the Championship remain profitable after thee months of autonomous work.

You can also try your skills in making and adjusting your trading robot to take part in the Championship and get the forward testing results of your Expert Advisor. Besides, the participation is free and the awards are impressive. We hope to see you there!


Conclusion

Professional intraday traders spend many hours sitting at their computers and waiting for the right moment to perform a deal. Of course, they cannot be in good shape all the time.

Most traders come to the conclusion that their actions violate their own trading rules. Not all trading systems can be completely formalized but even such systems can in most cases adopt additional tools, such as indicators, analytical systems and false signals filters.

We do not make any special recommendations here concerning MQL4 or MQL5 languages learning, as there are a lot of other useful articles concerning that subject. The purpose of this article was to provide some initial idea about how to start making your trading robot for MetaTrader 4 and MetaTrader 5 terminals.

We hope that this article will save time for newcomers and show the right direction in the difficult task of developing an automated trading system.

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/443

Last comments | Go to discussion (2)
Sergey Golubev
Sergey Golubev | 15 Feb 2014 at 07:04

Forum on trading, automated trading systems and testing trading strategies

Press review

newdigital, 2014.02.15 06:58

Trader Styles and Flavors (based on dailyfx article)

Technical vs. Fundamental

Technical analysis is the art of studying past price behavior and attempting to anticipate price moves in the future. These are traders that focus solely on price charts and often times incorporate indicators and tools to assist them. They look at price action, support and resistance levels, and chart patterns to create trading strategies that hopefully will turn a profit.

Fundamental analysis looks at the underlying economic conditions of each currency. Traders will turn to the Economic Calendar and Central Bank Announcements. They attempt to predict where price might be headed based on interest rates, jobless claims, treasury yields and more. This can be done by looking at patterns in past economic news releases or by understanding a country’s economic situation.

Short-Term vs. Medium-Term vs. Long-Term

Deciding what time frame we should use is mostly decided by how much time you have to devote to the market on a day-to-day basis. The more time you have each day to trade, the smaller the time frame you could trade, but the choice is ultimately yours.

Short-Term trading generally means placing trades with the intention of closing out the position within the same day, also referred to as
“Day Trading” or “Scalping” if trades are opened and closed very rapidly. Due to the speed at which trades are opened and closed, short-term traders use small time-frame charts (Hourly, 30min, 15min, 5min, 1min).

Medium-Term trades or “Swing Trades” typically are left open for a few hours up to a few days. Common time frames used for this type of trading are Daily, 4-hour and hourly charts.

Long-Term trading involves keeping trades open for days, weeks, months and possibly years. Weekly and Daily charts are popular choices for long term traders. If you are a part-time trader, it might be suitable to begin by trading long term trades that require less of your time.

Discretionary vs. Automated

Discretionary trading means a trader is opening and closing trades by using their own discretion. They can use any of the trading styles listed above to create a strategy and then implement that strategy by placing each individual trade.
The first challenge is creating a winning strategy to follow, but the second (and possibly more difficult) challenge is diligently following the strategy through thick and thin. The psychology of trading can wreak havoc on an otherwise profitable strategy if you break your own rules during crunch time.

Automated trading or algorithmic trading requires the same time and dedication to create a trading strategy as a discretionary trader, but then the trader automates the actual trading process. In other words, computer software opens and closes the trades on its own without needing the trader’s assistance. This has three main benefits. First, it saves the trader quite a bit of time since they no longer have to monitor the market as closely to input trades. Second, it takes the emotions out of trading by letting a computer open and close trades on your behalf. This means you are following your strategy to the letter and are not able to deviate. And third, automated strategies can trade 24 hours a day, 5 days a week giving your account the ability to take advantage of any opportunity that comes its way no matter the time of day.


Syariful Anwar
Syariful Anwar | 8 Mar 2016 at 11:33

It a MUST READ article for all Freelance customers here before they press the New Job button, to minimize the arbitration procedure. 

Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.

How to create an indicator of non-standard charts for MetaTrader Market How to create an indicator of non-standard charts for MetaTrader Market

Through offline charts, programming in MQL4, and reasonable willingness, you can get a variety of chart types: "Point & Figure", "Renko", "Kagi", "Range bars", equivolume charts, etc. In this article, we will show how this can be achieved without using DLL, and therefore such "two-for-one" indicators can be published and purchased from the Market.