Design your own forex robot

Design your own forex robot

Posted: yms Date of post: 05.06.2017

Do you like the article? Share it with others - post a link to it! Use new possibilities of MetaTrader 5. The world around us is changing rapidly, and we try to keep up with it. We do not have time to learn something new, and this is a normal attitude of a normal human being. Traders are people just like everyone else, they want to get maximum results for the minimum of effort. Specially for traders, MetaEditor 5 offers a wonderful MQL5 Wizard. There are several articles describing how to create an automated trading system using the wizard, including a "light version" MQL5 Wizard for Dummies and a "version from developers " - MQL5 Wizard: It all seems good - a trading robot is created in 5 mouse clicks, you can test it in the Strategy Tester and optimize the parameters of a trading system, you can let the resulting robot trade on your account without the need to do anything else manually.

The trader opens the MQL5 documentation, gets to the Standard Library, and is horrified to see True, the MQL5 Wizard greatly simplifies the creation of Expert Advisors, but first you need to learn what will be used as input for it. To automatically create an Expert Advisor using the MQL5 Wizard, make sure that its components adhere to five basic classes of the section Base Classes of Expert Advisors:. Here is the whole force of the "great and terrible" approach that is called Object-oriented programming OOP.

But don't be afraid, now almost everyone has a cell phone with lots of function, and almost no one knows how it works. We do not need to study all this, we will only discuss some functions of the CExpertSignal class. In this article we will go through the stages of creating a module of trading signals , and you will see how to do this without having to learn OOP or the classes.

Design Your Forex Trading System in 6 Steps - hozenesipew.web.fc2.com

But if you want, you can go a little further then. We will not alter any existing module of trading signals to our needs, because it's the way to get confused.

Right-click on the folder we have created, select "New File" and create a new class for our module of trading signals. Click "Finish" and a draft of our module us ready. It's all east so far. We only need to add the include declaration to the resulting file so that the compiler knows where to find the base class CExpertSignal.

Check the resulting class it must be free of compilation errors and click F7. There are no errors and we can move on. Our class is completely empty, it has no errors and we can test it - let's try to create a new Expert Advisor in the MQL5 Wizard based on it.

How to Code Your Own Algo Trading Robot | Investopedia

We reach the step of selecting a module of trading signals and see And how can it be there? We do not add any indications for the MQL5 Wizard to understand that our class could be something useful. If you look at the modules of the standard package, you'll see that each of them contains a header at the beginning of the file.

This is the handle of the module compiled according to certain rules. And the rules are very simple. Open, for example, the source code of the module of AMA based trading signals see the logic description in Signals of the Adaptive Moving Average. And run the MQL5 Wizard choosing this module. The last block in the handle refers to the module parameters, the first line contains the name of the module to be displayed in the MQL5 Wizard.

As you can see, there is nothing complicated. Thus, the handle of each module contains the following entries:. Now, knowing all this, let's create the handle of our module of trading signals. So, we are writing a module for getting trading signals at the intersection of two moving averages.

We need to set at least four external parameters:. You could also add a shift and the type of prices to calculate each of the moving averages, but it does not change anything fundamentally.

So the current version is as follows: Save the changes and compile. There should not be any errors. Run the MQL5 Wizard to check.

You see, our module is now available for selection, and it shows all of our parameters! Now it is time to work with the external parameters. Let's add four lines equal to the number of parameters to the class declaration. We've already described the parameter in the handle and know the following:. It's all very simple, you only need to declare public methods of the same name in the class, namely, to add four lines to the public section:.

When you generate an Expert Advisor on the basis of this module using the MQL5 Wizard and run it on the chart, these four methods are automatically called when initializing the Expert Advisor. So here is a simple rule: The rule of parameter creation in the module - for each parameter that we have declared in the handle, we should create a private member in the class for storing its value and a public member for setting a value to it. The method name must match the name of the parameter.

Each declared variable or class member must be initialized. This technique allows to avoid many of hard-to-find errors. For automatic initialization, the best suiting one is the class constructor; it is always the first one to be called when creating an object.

For default values, we will use those written in the module handle. Here the class members are initialized using the initialization list. As you can see, we haven't used moving average indicators yet. We found a simple rule - as many parameters are stated in the handle of the module, so many methods and members should be in the class that implements the module.

There is nothing complicated! However, don't forget to set default values of parameters on the constructor. In our case, we must check the periods of moving averages and the type of smoothing for their calculation. For this purpose you should write your own ValidationSettings method in the class. This method is defined in the parent class CExpertBase , and in all its children it is obligatorily redefined.

But if you do not know anything about object-oriented programming, just remember - in our class we should write the ValidationSettings function, which requires no parameters and returns true or false.

First comes the return type, then the class name, then scope resolution operator:: Do not forget that the name and type of parameters must match in the declaration and description of the class method. However, the compiler will warn you of such an error. If you do not add this line, the generated Expert Advisor will not be able to initialize our module of trading signals.

It's time to work with the indicators, since all the preparatory work with the parameters for them have been completed. Each module of trading signals contains the InitIndicators method, which is automatically called when you run the generated Expert Advisor. In this method, we must provide indicators of moving averages for our module.

First, declare the InitIndicators method in the class and paste its draft:. So there is nothing complicated, we declare the method and then simply create the method body, as we have done for the ValidationSettings method.

Above all, do not forget to insert the class name and the operator:: We have a draft, which we can insert into a code to create moving averages. Let's do this properly - for each indicator we create a separate function in the class, which returns true if successful. The function can have any name, but let it reflect its purpose, so let's call the functions CreateFastMA and CreateSlowMA.

That is why a pointer to a variable of type CIndicators is passed as a parameter. The following is written in Documentation about it:. The CIndicators is a class for collecting instances of timeseries and technical indicators classes.

The CIndicators class provides creation of instanced of technical indicator classes, their storage and management data synchronization, handle and memory management. This means that we must create our indicators and place them in this collection.

Since only indicators of the CIndicator form and its children can be stored in the collection, we should use this fact. We will use CiCustom , which is the above mentioned child. For each moving average we declare an object of type CiCustom in the private part of the class: Of course, you can create your own indicator class, which will be derived from CIndicator , and implement all the necessary methods for use with the MQL5 Wizard. But in this case we want to show how you can use any custom indicator in the module of trading signals using CiCustom.

Then declare the MqlParam structure, which is especially designed for storing parameters of custom indicators, and fill it with values. We use Custom Moving Average from the standard terminal delivery pack as the custom MA indicator.

Since Custom Moving Average. If you look at the code for this indicator, you can see all the required data:. After filling the structure, the indicator is initialized by the Create method of all the required parameters: And the last one is specifying the number of indicator buffers using the NumBuffers method. The CreateSlowMA method for creating the slow moving average is simple.

When using custom indicators in the module, do not forget that the Expert Advisor generated by the MQL5 Wizard will also run in the tester. If we use several different indicators, we should add this line for each of them. So, we have added the indicators. For more convenience, let's provide two methods of receiving MA values:.

As you can see, the methods are very simple, they used the GetData method of the SIndicator parent class, which returns a value from the specified indicator buffer at the specified position. If you need classes for working with classical indicators of the standard package, they are available in section Classes for working with indicators.

We are ready to proceed to the final stage. Everything is ready to make our module work and generate trading signals. This functionality is provided by two methods that must be described in each child of CExpertSignal: If the function returns a null value, it means that there is no trading signal.

If there are conditions for the signal, then you can estimate the strength of the signal and return any value not exceeding Evaluation of the signal strength allows you to flexibly build trading systems based on several modules and market models. Read more about this in MQL5 Wizard: Since we are writing a simple module of trading signals, we can agree that the buy and sell signals are valued equally Let's add necessary methods in the class declaration. Also, let's create the description of functions.

This is how the buy signal is checked it's all the same with the sell signal:. Note that we have declare the idx variable, to which the value returned by the StartIndex function of the parent class CExpertBase is assigned.

The StartIndex function returns 0, if the Expert Advisor is designed to work on all ticks, and in this case the analysis starts with the current bar. If the Expert Advisor is designed to work at open prices, StartIndex returns 1 and the analysis starts with the last formed bar.

By default StartIndex returns 1 , which means that the Expert Advisor generated by the MQL5 Wizard will only run at the opening of a new bar and will ignore incoming ticks during formation of the current bar. How to activate this mode and how it can be used will be described later in the finishing stroke.

The module is ready for use, so let's create a trading robot in the MQL5 Wizard based on this module. To test the efficiency of our module, let's generate an Expert Advisor based on it in the MQL5 Wizard and run it on the chart. All other parameters have also been added by the MQL5 Wizard while generating the EA based on the selected money management module and position maintenance module Trailing Stop.

Thus, we only had to write a module of trading signals and received a ready solution. This is the main advantage of using the MQL5 Wizard! Now let's test the trading robot in the MetaTrader 5 Strategy Tester.

design your own forex robot

Let's try to run a quick optimization of key parameters. In these settings of input parameters, more than half a million of passes is required for full optimization. Therefore, we choose fast optimization genetic algorithm and additionally utilize MQL5 Cloud Network to accelerate the optimization.

The optimization has been done in 10 minutes and we have got the results. As you can see, creating a trading robot in MQL5 and optimization of input parameters have taken much less time than would be required for writing the position management servicing logic, debugging and searching for the best algorithms.

You can skip this item or go back to it later when you are completely comfortable with the technique of writing a module of trading signals. Based on this variable, the StartIndex function returns its value. It communicates to the Expert Advisor the mode it should run in.

design your own forex robot

Do this only if you understand how it works. Not all trading systems are designed to work inside the bar. If you have mastered MQL5, then you no longer need to write an Expert Advisor from scratch. Just create a module of trading signals and, based on this module, automatically generate a trading robot with the enabled trailing and trade volume management modules. And even if you are not familiar with OOP or do not want to delve much into the structure of trade classes, you can just go through 6 steps: Each step is simple and requires little skill in MQL5 programming.

You only need to write your module once, following the instructions, and further verification of any trade idea will take no more than an hour, without tiring hours of coding and debugging. Remember that the trading strategy implemented by your trading robot created using the MQL5 Wizard, is as complex as the module of trading signals it uses. But before you start to build a complex trading system based on a set of rules for entry and exit, split it into several simple systems and check each one separately.

Based on simple modules you can create complex trading strategies using the ready-made modules of trading signals, but this is a topic for another article! Translated from Russian by MetaQuotes Software Corp.

Here I tried to make a simple program, but the compiler found only one error: Class with "no type". And no matter what I put in as a Class "type" or "name" there was no change. First off all I would like to thank the author s for this article. I'm new to MetaTrader, MQL5 and Forex trading, so these articles are really useful! When I test this EA, I notice that for closing a long position, it gives a sell signal with a doubled lot size.

Why does it not only close my long position, but directly enters a short at the same signal? I do not have any experience yet with programming in MQL, but I do have some experience in programming in other languages. It is one of the easiest errors to make, and conversely, also one of the harder ones to find. KJG, as far as the closing a long and heading right into a short, it is probably what this style of EA is designed to do.

I have seen many trading styles with just that type of setup, I believe they were designed for a market that has a lot of volatility and wide price swings happening all the time, like something that would likely happen on a minute time frame chart.

Good article as well. I definitely will be making use of this one and many others here I am sure. The article is intended to get its readers acquainted with the Box-Cox transformation.

The issues concerning its usage are addressed and some examples are given allowing to evaluate the transformation efficiency with random sequences and real quotes. If we thoroughly examine any complex trading system, we will see that it is based on a set of simple trading signals.

Therefore, there is no need for novice developers to start writing complex algorithms immediately. This article provides an example of a trading system that uses semaphore indicators to perform deals. The technical analysis widely implements the indicators showing the basic quotes "more clearly" and allowing traders to perform analysis and forecast market prices movement.

It's quite obvious that there is no sense in using the indicators, let alone applying them in creation of trading systems, unless we can solve the issues concerning initial quotes transformation and the obtained result credibility. In this article we show that there are serious reasons for such a conclusion. Take a look at your trading terminal. What means of price presentation can you see?

We are chasing time and prices whereas we only profit from prices. Shall we only give attention to prices when analyzing the market? This article proposes an algorithm and a script for point and figure charting "naughts and crosses" Consideration is given to various price patterns whose practical use is outlined in recommendations provided.

MetaTrader 5 Examples Indicators Experts Tester Trading Trading Systems Integration Indicators Expert Advisors Statistics and analysis Interviews MetaTrader 4 Examples Indicators Experts Tester Trading Trading Systems Integration Indicators Expert Advisors Statistics and analysis. MetaTrader 5 — Examples. One More Time about the MQL5 Wizard The world around us is changing rapidly, and we try to keep up with it. Five Terrible Classes True, the MQL5 Wizard greatly simplifies the creation of Expert Advisors, but first you need to learn what will be used as input for it.

To automatically create an Expert Advisor using the MQL5 Wizard, make sure that its components adhere to five basic classes of the section Base Classes of Expert Advisors: CExpertBase is a base class for four other classes.

CExpert is the class for creating a trading robot; this is the class that trades. CExpertSignal is a class for creating a module of trading signals; the article is about this class. CExpertTrailing is a class for trailing a protecting Stop Loss. CExpertMoney is the money management class. Creating a Class from Scratch We will not alter any existing module of trading signals to our needs, because it's the way to get confused. Fill in the fields: Class Name - the name of the class.

Base Name is the class from which our class is derived. And we should derive it from the base class CExpertSignal. We only need to add the include declaration to the resulting file so that the compiler knows where to find the base class CExpertSignal include ".. A Handle to the Module Our class is completely empty, it has no errors and we can test it - let's try to create a new Expert Advisor in the MQL5 Wizard based on it.

Thus, the handle of each module contains the following entries: Type - the version of the module of signals. It must always be SignalAdvanced. Name - the name of the module after its is selected in the MQL5 Wizard and is used in comments for describing internal parameters of the generated Expert Advisor preferably specified.

Class - the name of the, which is contained in the module.

How to Code Your Own Algo Trading Robot | Investopedia

Page - a parameter to get Help for this module only for modules from the standard delivery. The name of the function to set the value of the parameter when starting the Expert Advisor. The parameter type can be enumeration. The default value for the parameter, i. Description of the parameter, which you see when you start the Expert Advisor generated in the MQL5 Wizard. We need to set at least four external parameters: FastPeriod - the period of the fast moving average FastMethod - the type of smoothing of the fast moving average SlowPeriod - the period of the slow moving average SlowMethod - the type of smoothing of the slow moving average You could also add a shift and the type of prices to calculate each of the moving averages, but it does not change anything fundamentally.

The name displayed in the MQL5 Wizard - "Signals at the intersection of two moving averages". Four external parameter to configure the trading signals. FastPeriod - the period of the fast moving average with the default value of FastMethod - the type of smoothing of the fast moving average, simple smoothing by default.

SlowPeriod - the period of the slow moving average with the default value of SlowMethod - the type of smoothing of the slow moving average, simple smoothing by default. Congratulations, our module of trading signal looks great now! Methods for Setting Parameters Now it is time to work with the external parameters. We've already described the parameter in the handle and know the following: It's all very simple, you only need to declare public methods of the same name in the class, namely, to add four lines to the public section: Attached files Download ZIP.

All rights to these materials are reserved by MQL5 Ltd.

Copying or reprinting of these materials in whole or in part is prohibited. Last comments Go to discussion 9. Lorenz Funderburk 12 Jun at Here is the beginning. What am I doing wrong?: I try to test this: Please help me what wrong with this parameters: KJG 12 Dec at JD4 23 Jun at Victor 11 Nov at I followed the instructions in steps 1 and 2, but the signal doesn't appear in the Wizard list of available signals.

I tried with other signals and none works. Maybe there's a limitation in the number of signals that can be displayed in the wizard. The Box-Cox Transformation The article is intended to get its readers acquainted with the Box-Cox transformation.

Simple Trading Systems Using Semaphore Indicators If we thoroughly examine any complex trading system, we will see that it is based on a set of simple trading signals. Analyzing the Indicators Statistical Parameters The technical analysis widely implements the indicators showing the basic quotes "more clearly" and allowing traders to perform analysis and forecast market prices movement.

The Last Crusade Take a look at your trading terminal.

Rating 4,4 stars - 289 reviews
inserted by FC2 system