Programming your strategy in 10 steps with Universal Expert Advisor The X!

Programming your strategy in 10 steps with Universal Expert Advisor The X!

MetaTrader Market: How do you buy an advisor on mql5.com? How to rent? How to update? How to install it?
Exp – The xCustomEA Advisor on the PipFinite Trend PRO automatic EA
MetaTrader Market: How do you buy an advisor on mql5.com? How to rent? How to update? How to install it?
Exp – The xCustomEA Advisor on the PipFinite Trend PRO automatic EA

Description

The X is a builder of trading strategies.

We created a Universal Expert Advisor that has all the most interesting and necessary functions.

And a block of signals in the form of standard indicators was added.

This is a builder for strategy, and you can program your strategy.

It was a project that became very popular. For 15 years, we have been working on the algorithm, adding new functions and new indicators, and correcting errors in functions.

This article will help you program your strategy, program the advisor on indicators, and add new signals and filters.

Open-source

A persistent question to us was the question:

  1. Can I integrate my indicators into an advisor?
  2. How much does the open code of the Expert Advisor cost?

It’s no secret that we have an open (source) code of our adviser on sale. This is done for those traders who wish to write their advisors or to build their signals.

Also, traders often buy open-source code to implement their secret strategy and resell the adviser.

  1. The open code of the adviser The X can be bought in our market of trade advisors.
  2. Open source The X for MetaTrader5
  3. Open source The X for MetaTrader4
  4. By the way, we have many open codes for our advisors. Section: Strategy Advisors (Open source)
  5. Try our Open Source templates.

Programming advisors on indicators is a difficult process.

We provide programming services for trade advisors. However, the process of writing a sales advisor is delayed each time because the customer (the client) has no idea what functions he needs.

The X is a set of blocks and functions for trading on FOREX, which is used all over the world. We have created functions that are used most often. Thus, the advisor can realize 90% of the needs of each trader.

Naturally, there are unique strategies that experienced programmers should program.

Such strategies are very rare and require careful preparation. The cost of this work depends on complexity.

The real value of our advisor

If we start from our article about how much the programmer’s work costs, then we can calculate the approximate cost of our trading robot, The X:

In our EA, there are more than 10,000 lines of code. That’s not a lot, however, because we do not write advisors on the PLO. Why? With the OOP, you are not familiar! And you will find it very difficult to deal with the OOP!

So, we will calculate:

  1. The cost of 1 hour of programmer’s work = $ 20 when programming functions and algorithms!
  2. The cost of 1 hour of error correction = 5 $.
  3. The cost of writing texts and videos = $ 10 (The price is taken from the Internet, all the instructions and videos I do myself).
  4. Suppose that writing functions and blocks on The X took more than 1000 hours!
  5. Programming signals by indicators is elementary. Therefore, we assume that it took us 10 hours!

But some points can not be fulfilled at once:

  1. Debugging functions can take another 1000 hours!
  2. Drawing up instructions, videos, and promotions also takes 1000 hours!

In total, we have spent more than 3000 hours!

If we ordered such an algorithm, we would refuse. This is not reasonable and not profitable.

The total cost of such an expert advisor (on order) is more than $ 35,000! But our advisor is sold 10 times cheaper !!!!

The cost of our open source is TEN times less!

Such orders are never fulfilled:

  1. Firstly, because the customer will not spend much money.
  2. Secondly, the programmer will not spend so much time programming.

I assure you that programming such a complex trading robot takes more time than creating a trading strategy.

Debugging all the blocks and functions requires an extended time.

We are very grateful to our customers who have found mistakes and corrections for 5 years, offered their functions and indicators for the work of the advisor THE X

Programming your Forex trading strategy

First of all, I want to say that we have updated our expert advisor’s code. Now it is very easy and commented.

You don’t need to go into detail about all the functions. You can program your strategy in just 10 steps.

We have prepared for you a very convenient way of working with our open source.

Remember: We do not program strategies for an advisor for free. If you want to write a code for your strategy – please refer to the Programming section to order!

First steps:

  1. Open our source code of the adviser that you bought in our market of advisers;
  2. Press Ctrl + F and you will see the search string;
  3. Enter the name Step – By this word, in our source code, you can navigate and quickly write your strategy.
  4. Each step is commented on and provides an example.
  5. To learn how to program your strategy, you can step-by-step uncomment lines with an asterisk // *

In total, there are 10 steps in the open code for the adviser to work on your indicators.

We will analyze each step in detail!

Because the programming differs in the two versions of the MT4 and MT5 terminals, we will also divide our article into two parts:

If your version of the advisor THE X for the MT4 terminal, you can immediately go to the programming section of The X for the Metatrader 4 terminal

Programming THE X for MetaTrader 5

You must know the minimum programming rules to write your strategy or ​​add a ready strategy for indicators from the finished file (Expert Advisors from the Internet, for example)

Programming advisors for MetaTrader 5 is more complicated than programming for MetaTrader 4. But the MT5 terminal allows you to create more powerful strategies and also has a more advanced strategy tester.

In any case, you should buy that open code on which the terminal will work.

MetaTrader 5 Step 0: Step loading indicators into resources!

 // Step 0 ############################################## ######################
// This part of the code for programming your strategy in open source
// This part of the code for programming your strategy in open source
// 
// In order for the indicator to be built into the Expert Advisor during compilation, you need to load it into the resources of the Expert Advisor
// In order for the indicator to be built in the Expert Advisor during compilation, you need to load it into the resources of the Expert Advisor
// ################################################ ####################
// for example:
// * #resource "\\ Indicators \\ Examples \\ MACD.ex5"
// ################################################ ########################### 

Here, we are obliged to add the indicators needed by Us to the resources.

This will allow us to compile an advisor for the market and also save us from unnecessary problems when the compiled advisor suddenly does not find the desired indicator on another computer!

Rules for writing the name of the market: The full name of the folders, starting with Indicators!

The folder is indicated by \\

For example :

If the path to your indicator looks like this:

C: \ ROBO MT5 \ MQL5 \ Indicators \ Examples \ MACD.ex5

then, the path to this indicator in the programming language is written like this:

“\\ Indicators \\ Examples \\ MACD.ex5”

MetaTrader 5 Step 1: Specify the signal name!

// Step 1 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your signal after // 
// Enter the name of your signal after // 
// ################################################ #################### 
// for example: 
// * Custom = 77, // Signal For open source 
// ################################################ ############################

At this step, we must add our Signal or strategy to the list of possible signals of the expert advisor.

This is done to manage the signals conveniently during the expert setup phase.

For example:

  1. You want to name your strategy MY STRATEGY
  2. The strategy number must also be unique. Signals from standard indicators occupy the first 22 numbers! Remember this figure. We will need it in the future.
  3. The name must also be unique. For example MYSTRATEGY1

In the end, we should write in the code like this:

MYSTRATEGY1 = 77, // MY STRATEGY

You can also add more strategies and write, for example, like this:

  1. MYSTRATEGY1 = 77, // MY STRATEGY a
  2. MYSTRATEGY2 = 78, // MY STRATEGY b
  3. MYSTRATEGY3 = 79, // MY STRATEGY with

MetaTrader 5 Step 2: Step to specify the name of the Signal to display the name on the screen!

// Step 2 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your signal, which will be displayed on the screen. after // 
// Enter the name of your signal, which will be displayed on the screen. after // 
// ################################################ #################### 
// for example: 
// * if (i == 77) s1 = "Custom"; 
// ################################################ ###########################

This step serves to display the name of your strategy in our panel for the EAPADpro EA.

There is nothing complicated! But you must use the numbers from the last step.

For example:

  1. if (i == 77) s1 = “My1”;
  2. if (i == 78) s1 = “My2”;
  3. if (i == 79) s1 = “My3”;

MetaTrader 5 Step 3: Step of specifying the filter name!

// Step 3 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your filter after // 
// Enter the name of your filter after // 
// ################################################ #################### 
// for example: 
// * FILTERCustom = 77, // Filter For open source 
// ################################################ ###########################

At this step, we need to add our Filter to the list of possible filters of the Expert Advisor.

This is done to make it convenient to manage the filters during the expert setup phase.

Usually, The Filter uses the same indicator as the Signal. Still, with the condition that the Filter determines the current position of the Signal, and the Signal is a fact of the Signal. Examples of differences between filters and signals

For example:

  1. Do you want to name your Filter MY FILTER?
  2. The number of the filter should also be the same as the number of the strategy (This is done in order not to be confused with figures in the future). Filters occupy the first 22 numbers from standard indicators! Remember this figure. We will need it in the future.
  3. The name must also be unique. For example MYFILTER1

In the end, we should write in the code like this:

MYFILTER1 = 77, // MY FILTER

You can also add more strategies and write, for example, like this:

  1. MYFILTER1 = 77, // MY FILTER a
  2. MYFILTER2 = 78, // MY FILTER b
  3. MYFILTER3 = 79, // MY FILTER s

MetaTrader 5 Step 4: Step to specify the parameters of the indicator or strategy in external variables!

// Step 4 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Specify all the external variables of your indicator or indicators that will be used in your strategy 
// Write all external variables of your indicator or indicators that will be used in your strategy 
// ################################################ #################### 
// for example: 
// * input string IndicatorName = "-------- Custom Indicator ----------------"; 
// * input int CUSTOMMAFastPeriod = 1; 
// * input int CUSTOMMASlowPeriod = 100; 
// * input ENUM_APPLIED_PRICE CUSTOMMAprice = PRICE_CLOSE; 
// ################################################ ###########################

At this step, you must enter all external settings of the indicators of your strategy, which can be changed from the Expert Advisor. For example, for optimization.

Also, here, you can take out specific parameters of the strategy. For example, the levels of the intersection of lines, the limitation of the indicator’s operation on MAX and MIN prices on the chart, and so on …

You do not have to enter all the parameters for the indicators. It is enough to make those that you can change in the future from the settings of the Expert Advisor. For all other parameters, you can set the indicator by default.

MetaTrader 5 Step 5: The step of creating indicator handles to load into the Expert Advisor!

// Step 5 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// You need to create an entry in the array in order for the Expert Advisor to assign the indicator handle when initializing 
// You need to create an entry in the array in order for the Expert Advisor to assign an indicator handle when initializing 
// ################################################ #################### 
// for example: 
// * case 77: 
// * h_custom [tf_num] [sym_num] = iCustom (array_symbol, array_tf, ":: Indicators \\ Examples \\ MACD", CUSTOMMAFastPeriod, CUSTOMMASlowPeriod, 9, CUSTOMMAprice); 
// * if (h_custom [tf_num] [sym_num] == INVALID_HANDLE) 
// * { 
// * PrintToLogs ("Could not get MACD indicator handle"); 
// * return (INIT_FAILED); 
// *} 
// ################################################ ###########################

This step is necessary to ensure that our indicators are loaded 1 time during the initialization of the Expert Advisor. In MetaTrader 5, there is no need to load the indicator on the chart constantly.

When creating a handle, we remember the handle of an already open indicator and use it for further work.

Here, you need to be very careful. When creating a handle, we must specify all external variables of the indicator and specify the correct name. Otherwise, the Expert Advisor will not be able to load the indicator.

Remember that we loaded the indicator into the Expert Advisor resources. Therefore, we will call the indicator from the resources!

You can read the help on calling indicators through iCustom

A strategy number is also needed for us to write this code. If you have several indicators in one strategy, then you need to specify several indicators but with the same number. That the advisor correctly loaded your strategy.

According to our examples above, we need to do this:

 case 77: h_custom [tf_num] [sym_num] = iCustom (array_symbol, array_tf, ":: Indicators \\ Examples \\ MACD", CUSTOMMAFastPeriod, CUSTOMMASlowPeriod, 9, CUSTOMMAprice);
if (h_custom [tf_num] [sym_num] == INVALID_HANDLE)
{
PrintToLogs ("Could not get MACD indicator handle");
return (INIT_FAILED);
}
break; 

Here, I explicitly specified only two external parameters. I replaced the parameter of this Indicator InpSignalSMA with a default number, so you understand the essence of programming!

By analogy above, you can add several indicators and handles.

If the strategy uses several indicators, then you need to write several indicators.

For example:

 case 77: h_custom [tf_num] [sym_num] = iCustom (array_symbol, array_tf, ":: Indicators \\ Examples \\ MACD", CUSTOMMAFastPeriod, CUSTOMMASlowPeriod, 9, CUSTOMMAprice);
 h_custom2 [tf_num] [sym_num] = iCustom (array_symbol, array_tf, ":: Indicators \\ Examples \\ MACD", CUSTOMMAFastPeriod, CUSTOMMASlowPeriod, 15, CUSTOMMAprice);
 if (h_custom [tf_num] [sym_num] == INVALID_HANDLE || h_custom2 [tf_num] [sym_num] == INVALID_HANDLE)
 {
 PrintToLogs ("Could not get MACD indicator handle");
 return (INIT_FAILED);
 }
 break; 

Also, with other strategies that you want to insert into the Expert advisor!

MetaTrader 5 Step 6: Declare the variables to load the indicator/ indicators of the strategy!

// Step 6 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Declare the variable of the handle array 
// Declare the variable of the handle array 
// ################################################ #################### 
// for example: 
// * int h_custom [6] [12]; 
// * int h_custom2 [6] [12]; 
// ################################################ ###########################

Here, we indicate the names of our handles that were created above!

There is nothing complicated here.

Handles are created in the form of arrays to use multi-currency in the EA and use the ability to specify 6 signals or filters at once!

MetaTrader 5 Step 7: Declare variables for storing indicator data!

// Step 7 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Declare the variable of the array of indicator values 
// Declare the variable of the handle array 
// ################################################ #################### 
// for example: 
// * double custom1_buffer []; 
// * double custom2_buffer []; 
// ################################################ ###########################

Here, we indicate the names of our data for buffers!

There is nothing complicated here.

In these arrays, data about the values ​​of the indicator on each bar will be stored. If you need to use multiple buffers, you can use several variables.

In our case, for example, we need 2 buffers for the signal line and the main line of the MACD indicator.

MetaTrader 5 Step 8: The primary step in creating strategies and signals for opening positions!

// Step 8 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Here we create our signal to open positions. Fill in the data buffers 
// Here we create our signal for opening positions. Fill in the data buffer 
// ################################################ #################### 
// for example: 
// * if (sigInd == 77) 
// * { 
// 
// Fill the data, 
// We fill in the data 
// 1. 
// * if (CopyBuffer (h_custom [array_tf] [array_symbol], 0,0,3 + OWNSIGNAL_shift, custom1_buffer) <3 + OWNSIGNAL_shift) 
// * {Alert ("CopyBuffer1 ERROR" + custom1_buffer [0] + "NameError =" + GetLastError ()); return (0);} 
// * if (! ArraySetAsSeries (custom1_buffer, true)) 
// * {Alert ("ArraySetAsSeries ERROR"); return (0);} 
// * if (CopyBuffer (h_custom [array_tf] [array_symbol], 1,0,3 + OWNSIGNAL_shift, custom2_buffer) <3 + OWNSIGNAL_shift) 
// * {Alert ("CopyBuffer2 ERROR" + custom2_buffer [0] + "NameError =" + GetLastError ()); return (0);} 
// * if (! ArraySetAsSeries (custom2_buffer, true)) 
// * {Alert ("ArraySetAsSeries ERROR"); return (0);} 
// 
// Here we write our strategy, in this example it's the intersection of two lines, 
// Here we write our strategy, in this example it is the intersection of two lines 
// 2. 
// * if (custom1_buffer [OWNSIGNAL_shift + 1] <custom2_buffer [OWNSIGNAL_shift + 1] && custom1_buffer [OWNSIGNAL_shift]> custom2_buffer [OWNSIGNAL_shift]) 
// * sig = 1; 
// * else if (custom1_buffer [OWNSIGNAL_shift + 1]> custom2_buffer [OWNSIGNAL_shift + 1] && custom1_buffer [OWNSIGNAL_shift] <custom2_buffer [OWNSIGNAL_shift]) 
// * sig = -1; 
// * else sig = 0; 
// 
// We write down the data that will be displayed in the log, when the positions are opened at the signal 
// We write down the data that will be displayed in the log, when you open positions on a signal 
// 3. 
// * InfoAboutSignal = InfoAboutSignal + "" + "sigInd =" + sigInd + "array_tf =" + array_tf + "array_symbol =" + array_symbol + "sig =" + sig + "" + __ FUNCTION __ + "" 
// * + "custom1_buffer [OWNSIGNAL_shift + 1] =" + custom1_buffer [OWNSIGNAL_shift + 1] 
// * + "custom2_buffer [OWNSIGNAL_shift + 1] =" + custom2_buffer [OWNSIGNAL_shift + 1] 
// * + "custom1_buffer [OWNSIGNAL_shift] =" + custom1_buffer [OWNSIGNAL_shift] 
// * + "custom2_buffer [OWNSIGNAL_shift] =" + custom2_buffer [OWNSIGNAL_shift]; 
// // --- return the trading signal 
// * return (sig); 
// *} 
// ################################################ ###########################

This step is very important for the Expert advisor. Although all other steps are mandatory, otherwise, the advisor will not work.

I indicated my comments in the code!

The strategy number is the same as in the first steps!

  1. Programming code for strategy and signal individually. It depends on the chosen strategy and indicators. Getting buffers strategies ……..
  2. I want to note only that in this block, you can specify any strategy.
  3. All the indicators that we declared in the handles will be loaded into memory and used to build the strategy.

This step can be divided into 3 substeps:

  1. 1. We are obliged to copy data buffers to our data sets. In the copying parameters, we must specify the line numbers (In our case, these are MACD indicator lines)
  2. We use two lines, MAIN and SIGNAL
  3. You can read help about the function CopyBuffer
  4. 2. Programming the strategy. In this case, it is the intersection of two lines.
  5. Important: The Signal for BUY is 1, and for SELL this is -1
  6. 3. Here, we record the information that will be displayed in the log when the position is opened. Strategy Information

If you filled all the handles and buffers correctly, then the following should appear on the screen:

Alert (“Congratulations, you’ve made a lesson that shows the programming of your strategy!”); 
Alert (“Congratulations! You have done a lesson that shows the programming of your strategy!”); 
If you received an error message, then you need to find out the cause of this error.

Error Codes

Most common errors:

ERR_INDICATOR_UNKNOWN_SYMBOL4801Unknown character
ERR_INDICATOR_CANNOT_CREATE4802The indicator can not be created
ERR_INDICATOR_NO_MEMORY4803There is not enough memory to add an indicator
ERR_INDICATOR_CANNOT_APPLY4804The indicator can not be applied to another indicator
ERR_INDICATOR_CANNOT_ADD4805Error while adding the indicator
ERR_INDICATOR_DATA_NOT_FOUND4806The requested data was not found
ERR_INDICATOR_WRONG_HANDLE4807Wrong indicator handle
ERR_INDICATOR_WRONG_PARAMETERS4808Wrong number of parameters when creating an indicator
ERR_INDICATOR_PARAMETERS_MISSING4809Missing parameters when creating the indicator
ERR_INDICATOR_CUSTOM_NAME4810The first parameter in the array must be the name of the custom indicator
ERR_INDICATOR_PARAMETER_TYPE4811The wrong type of parameter in the array when creating the indicator
ERR_INDICATOR_WRONG_INDEX4812Invalid index of the requested indicator buffer

MetaTrader 5 Step 9: Step to specify the filters from your indicators, or use your indicator as a filter for the strategy!

// Step 9 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Here we create our filter for signal filtering. Fill in the data buffers 
// Here we create our filter for signal filtering. Fill in the data buffers 
// ################################################ #################### 
// for example: 
// * if (sigInd == 77) 
// * { 
// 
// Fill the data, 
// We fill in the data 
// 
// * if (CopyBuffer (h_custom [array_tf] [array_symbol], 0,0,3 + shift, custom1_buffer) <3 + shift) 
// * {Alert ("CopyBuffer1 ERROR" + custom1_buffer [0] + "NameError =" + GetLastError ()); return (0);} 
// * if (! ArraySetAsSeries (custom1_buffer, true)) 
// * {Alert ("ArraySetAsSeries ERROR" + "NameError =" + GetLastError ()); return (0);} 
// * if (CopyBuffer (h_custom [array_tf] [array_symbol], 1,0,3 + shift, custom2_buffer) <3 + shift) 
// * {Alert ("CopyBuffer2 ERROR" + custom2_buffer [0] + "NameError =" + GetLastError ()); return (0);} 
// * if (! ArraySetAsSeries (custom2_buffer, true)) 
// * {Alert ("ArraySetAsSeries ERROR" + "NameError =" + GetLastError ()); return (0);} 
// 
// Here we write our strategy, in this example it's the intersection of two lines, 
// Here we write our strategy, in this example it is the intersection of two lines 
// 
// * if (custom1_buffer [shift]> custom2_buffer [shift]) 
// * sig = 1; 
// * else if (custom1_buffer [shift] <custom2_buffer [shift]) 
// * sig = -1; 
// * else sig = 0; 
// 
// We write down the data that will be displayed in the log, when the positions are opened at the signal 
// We write down the data that will be displayed in the log, when you open positions on a signal 
// 
// * InfoAboutSignal = InfoAboutSignal + "" + "sigInd =" + sigInd + "array_tf =" + array_tf + "array_symbol =" + array_symbol + "sig =" + sig + "" + __ FUNCTION __ + "" 
// * + "custom1_buffer [shift + 1] =" + custom1_buffer [shift + 1] 
// * + "custom2_buffer [shift + 1] =" + custom2_buffer [shift + 1] 
// * + "custom1_buffer [shift] =" + custom1_buffer [shift] 
// * + "custom2_buffer [shift] =" + custom2_buffer [shift]; 
// // --- return the trading signal 
// * return (sig); 
// *}

In the programming of the code in this step, there is nothing complicated, and it is similar to the previous step.

The only difference is that at this stage, you program your indicators in the role of Filter.

You can not specify signals by the indicator but only specify the filter. Or vice versa! It all depends on the strategy.

 

MetaTrader 5 Step 10: This is the final step!

// Step 10 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// This is the end of our code, If you have reached this step, then you have made your strategy. 
// For example, I entered a secret strategy. If you did everything correctly, then you will get the result in the form of an alert on the screen! 
// Thank you for choosing our product. We are working for you! 
// 
// Our website: 
// Our store: /shop 
// MQL5 store: https://www.mql5.com/users/vladon/seller 
// 
// 
// This is the end of our code, If you have reached this step, then you have made your strategy. 
// For example, I entered a secret strategy. If you did everything correctly, Then you will get the result in the form of an alert on the screen! 
// Thank you for choosing our product. We are working for you! 
// 
// Our website: 
// Our store: /shop 
// MQL5 store: https://www.mql5.com/users/vladon/seller 
// ################################################ ###########################

It indicates that the programming of your strategy is over.

Good luck and profits for you!

MetaTrader 5 Conclusions and verification of the strategy.

If you do everything right, then your strategy will be included in the advisor, and you will be able to trade it with the help of my advisor.

Now, we need to test our advisor and strategy.

  1. Load the terminal;
  2. Open a demo account;
  3. Establish an advisor on the chart;
  4. If all is OK, you will receive a message about the successful addition of the strategy:
  5. Or you will get an error. Then, you need to look again at your code carefully and repeat all the steps!

Checking the Expert Advisor in the Strategy Tester:

  1. We open the terminal;
  2. Open the strategy tester;
  3. We choose our expert, currency, TF, and date. We check the tick on every tick based on real ticks;
  4. Open the Settings tab;
  5. We select our strategy;
  6. Turn on Visualization;
  7. Press START;
  8. A graph should open;
  9. If the Expert Advisor opens positions, you did everything right!

Programming THE X for MetaTrader 4

You must know the minimum programming rules to write your strategy Or ​​add a ready strategy for indicators from the finished file (Expert Advisors from the Internet, for example)

Programming advisors for MetaTrader 5 is more complicated than programming for MetaTrader 4. But the MT5 terminal allows you to create more powerful strategies and also has a more advanced strategy tester.

On the other hand, There are a lot of strategies and indicators for the MetaTrader 4 terminal on the Internet. Therefore, you can use the code for mt4!

And one more important reminder: The coding strategy in mt4 is much easier than encoding in mt5!

In any case, you should buy that open code on which terminal you will work.

MetaTrader4 Step 0: Step loading indicators into resources!

 // Step 0 ############################################## ######################
// This part of the code for programming your strategy in open source
// This part of the code for programming your strategy in open source
//
// In order for the indicator to be built into the Expert Advisor during compilation, you need to load it into the resources of the Expert Advisor
// In order for the indicator to be built in the Expert Advisor during compilation, you need to load it into the resources of the Expert Advisor
// ################################################ ####################
// for example:
// * #resource "\\ Indicators \\ MACD.ex4"
// ################################################ ###########################

Here, we are obliged to add the indicators needed by Us to the resources.

This will allow us to compile an advisor for the market and also save us from unnecessary problems when the compiled Expert Advisor suddenly does not find the desired indicator on another computer!

Rules for writing the name of the market: The full name of the folders, starting with Indicators!

The folder is indicated by \\

For example :

If the path to your indicator looks like this:

C: \ ROBO MT4 \ MQL4 \ Indicators \ Examples \ MACD.ex4

then, the path to this indicator in the programming language is written like this:

“\\ Indicators \\ Examples \\ MACD.ex4”

MetaTrader 4 Step 1: Specify the signal name!

// Step 1 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your signal after // 
// Enter the name of your signal after // 
// ################################################ #################### 
// for example: 
// * Custom = 77, // Signal For open source 
// ################################################ ############################

At this step, we must add our Signal or strategy to the list of possible signals from the adviser.

This is done to manage the signals conveniently during the expert setup phase.

For example:

  1. You want to name your strategy MY STRATEGY
  2. The strategy number must also be unique. Signals from standard indicators occupy the first 22 numbers! Remember this figure. We will need it in the future.
  3. The name must also be unique. For example MYSTRATEGY1

In the end, we should write in the code like this:

MYSTRATEGY1 = 77, // MY STRATEGY

You can also add more strategies and write, for example, like this:

  1. MYSTRATEGY1 = 77, // MY STRATEGY a
  2. MYSTRATEGY2 = 78, // MY STRATEGY b
  3. MYSTRATEGY3 = 79, // MY STRATEGY with

MetaTrader 4 Step 2: Step to specify the name of the Signal to display the name on the screen!

// Step 2 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your signal, which will be displayed on the screen. after // 
// Enter the name of your signal, which will be displayed on the screen. after // 
// ################################################ #################### 
// for example: 
// * if (i == 77) s1 = "Custom"; 
// ################################################ ###########################

This step serves to display the name of your strategy in our panel for the EApadpro EA.

There is nothing complicated! But you must use the numbers from the last step.

For example:

  1. if (i == 77) s1 = “My1”;
  2. if (i == 78) s1 = “My2”;
  3. if (i == 79) s1 = “My3”;

MetaTrader 4 Step 3: Step of specifying the filter name!

// Step 3 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Enter the name of your filter after // 
// Enter the name of your filter after // 
// ################################################ #################### 
// for example: 
// * FILTERCustom = 77, // Filter For open source 
// ################################################ ###########################

At this step, we need to add our Filter to the list of possible filters of the Expert Advisor.

This is done to make it convenient to manage the filters during the expert setup phase.

Usually, The Filter uses the same indicator as the Signal. Still, with the condition that the Filter determines the current position of the Signal, and the Signal is a fact of the Signal. 

Examples of differences between filters and signals

For example:

  1. Do you want to name your Filter MYFILTER?
  2. The number of the filter should also be the same as the number of the strategy (This is done in order not to be confused with figures in the future). Filters occupy the first 22 numbers from standard indicators! Remember this figure. We will need it in the future.
  3. The name must also be unique. For example MYFILTER1

In the end, we should write in the code like this:

MYFILTER1 = 77, // MY FILTER

You can also add more strategies and write, for example, like this:

  1. MYFILTER1 = 77, // MY FILTER a
  2. MYFILTER2 = 78, // MY FILTER b
  3. MYFILTER3 = 79, // MY FILTER s

MetaTrader 4 Step 4: Step to specify the parameters of the indicator or strategy in external variables!

// Step 4 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Specify all the external variables of your indicator or indicators that will be used in your strategy 
// Write all external variables of your indicator or indicators that will be used in your strategy 
// ################################################ #################### 
// for example: 
// * input string IndicatorName = "-------- Custom Indicator ----------------"; 
// * input int CUSTOMMAFastPeriod = 1; 
// * input int CUSTOMMASlowPeriod = 100; 
// ################################################ ###########################

At this step, you must enter all external settings of the indicators of your strategy, which can be changed from the Expert Advisor. For example, for optimization.

Also, here, you can take out specific parameters of the strategy. For example, the levels of the intersection of lines, the limitation of the indicator’s operation on MAX and MIN prices on the chart, and so on …

You do not have to enter all the parameters for the indicators. It is enough to make those that you can change in the future from the settings of the Expert Advisor. For all other parameters, you can leave the indicator by default.

MetaTrader 4 Step 5,6,7: Relax!

// Step 5 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Step 5 is only needed for the MT5 terminal. So at this step you can drink coffee 
// Step 5 is only necessary for the MT5 terminal. So at this step you can drink coffee 
// ################################################ ###########################

// Step 6 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Step 6 is only needed for the MT5 terminal. So at this step you can eat sandwiches 
// Step 6 is only needed for the MT5 terminal. So at this step you can eat sandwiches 
// ################################################ ###########################

// Step 7 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Step 7 is needed only for MT5 terminal. Make a workout 
// Step 7 is only necessary for the MT5 terminal. Make a workout 
// ################################################ ###########################

MetaTrader 4 does not require loading of indicators into memory for further work. When the indicator is called again, in mt4, the Expert Advisor uses the same handle.

MetaTrader 4 Step 8: The primary step in creating strategies and signals for opening positions!

// Step 8 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Here we create our signal to open positions. Fill in the data buffer 
// Here we create our signal for opening positions. Fill in the data buffer 
// ################################################ #################### 
// for example: 
// * if (sigInd == 77) 
// * { 
// 
// Fill the data, 
// We fill in the data 
// 
// 
// * double CustomMain = iCustom (Symbolt, TF_IndSigToTrade1, ":: Indicators \\ MACD", 0, OWNSIGNAL_shift); 
// * double CustomSig = iCustom (Symbolt, TF_IndSigToTrade1, ":: Indicators \\ MACD", 1, OWNSIGNAL_shift); 
// 
// * double CustomMain2 = iCustom (Symbolt, TF_IndSigToTrade1, ":: Indicators \\ MACD", 0, OWNSIGNAL_shift + 1); 
// * double CustomSig2 = iCustom (Symbolt, TF_IndSigToTrade1, ":: Indicators \\ MACD", 1, OWNSIGNAL_shift + 1); 
// 
// 
// Here we write our strategy, in this example it's the intersection of two lines, 
// Here we write our strategy, in this example it is the intersection of two lines 
// 1. 
// * if (CustomMain> CustomSig && CustomMain2 <CustomSig2) 
// // Buy Signal 
// * Signal = 1; 
// * if (CustomMain <CustomSig && CustomMain2> CustomSig2) 
// // Sell Signal 
// * Signal = -1; // 
// We write down the data that will be displayed in the log, when the positions are opened at the signal 
// We write down the data that will be displayed in the log, when you open positions on a signal 
// 2. 
// * InfoAboutSignal = "InfoAboutSignal sigInd =" + sigInd + "Signal =" + Signal 
// * + "CustomMain =" + CustomMain 
// * + "CustomSig =" + CustomSig 
// * + "CustomMain2 =" + CustomMain2 
// * + "CustomSig2 =" + CustomSig2 
// *; 
// // --- return the trading signal 
// *}

This step is very important for the Expert advisor. Although all other steps are mandatory, otherwise, the advisor will not work.

I indicated my comments in the code!

The strategy number is the same as in the first steps!

  1. Programming code for strategy and signal individually. It depends on the chosen strategy and indicators. Getting buffers strategies ……..
  2. I want to note only that in this block, you can specify any strategy.
  3. All the indicators that we declared in the handles will be loaded into memory and used to build the strategy.

This step can be divided into 2 substeps:

  1. 1.   Programming strategy. In this case, it is the intersection of two lines.
  2. Important: The Signal for BUY is 1, and for SELL this is -1
  3. 2. Here, we record the information that will be displayed in the log when the position is opened. Strategy Information


If you received an error message, then you need to find out the cause of this error.

Error Codes

Most common errors:

ERR_INDICATOR_UNKNOWN_SYMBOL4801Unknown character
ERR_INDICATOR_CANNOT_CREATE4802The indicator can not be created
ERR_INDICATOR_NO_MEMORY4803There is not enough memory to add an indicator
ERR_INDICATOR_CANNOT_APPLY4804The indicator can not be applied to another indicator
ERR_INDICATOR_CANNOT_ADD4805Error while adding the indicator
ERR_INDICATOR_DATA_NOT_FOUND4806The requested data was not found
ERR_INDICATOR_WRONG_HANDLE4807Wrong indicator handle
ERR_INDICATOR_WRONG_PARAMETERS4808Wrong number of parameters when creating an indicator
ERR_INDICATOR_PARAMETERS_MISSING4809Missing parameters when creating the indicator
ERR_INDICATOR_CUSTOM_NAME4810The first parameter in the array must be the name of the custom indicator
ERR_INDICATOR_PARAMETER_TYPE4811The wrong type of parameter in the array when creating the indicator
ERR_INDICATOR_WRONG_INDEX4812Invalid index of the requested indicator buffer

MetaTrader 4 Step 9: Step to specify the filters from your indicators, or use your indicator as a filter for the strategy!

// Step 9 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// Here we create our filter for signal filtering. Fill in the data buffers 
// Here we create our filter for signal filtering. Fill in the data buffers 
// ################################################ #################### 
// for example: 
// * if (sigInd == 77) 
// * { 
// 
// Fill the data, 
// We fill in the data 
// 
// 
// * double CustomMain = iCustom (Symbolt, TFTOFILTER, ":: Indicators \\ MACD", MODE_MAIN, shift); 
// * double CustomSig = iCustom (Symbolt, TFTOFILTER, ":: Indicators \\ MACD", MODE_SIGNAL, shift); 
// 
// Here we write our strategy, in this example it's the intersection of two lines, 
// Here we write our strategy, in this example it is the intersection of two lines 
// 
// * if (CustomMain> CustomSig) 
// // Buy Signal 
// * Signal = 1; 
// * if (CustomMain <CustomSig) 
// // Sell Signal 
// * Signal = -1; // 
// We write down the data that will be displayed in the log, when the positions are opened at the signal 
// We write down the data that will be displayed in the log, when you open positions on a signal 
// 
// * InfoAboutSignal = "InfoAboutSignal sigInd =" + sigInd + "Signal =" + Signal 
// * + "CustomMain =" + CustomMain 
// * + "CustomSig =" + CustomSig 
// *; 
// // --- return the trading signal 
// *}

In the programming of the code in this step, there is nothing complicated, and it is similar to the previous step.

The only difference is that at this stage, you program your indicators in the role of Filter.

You can not specify signals by the indicator but only specify the filter. Or vice versa! It all depends on the strategy.

 

MetaTrader 4 Step 10: This is the final step!

// Step 10 ############################################## ###################### 
// This part of the code for programming your strategy in open source 
// This part of the code for programming your strategy in open source 
// 
// This is the end of our code, If you have reached this step, then you have made your strategy. 
// For example, I entered a secret strategy. If you did everything correctly, Then you will get the result in the form of an alert on the screen! 
// Thank you for choosing our product. We are working for you! 
// 
// Our website: 
// Our store: /shop 
// MQL5 store: https://www.mql5.com/users/vladon/seller 
// 
// 
// This is the end of our code, If you have reached this step, then you have made your strategy. 
// For example, I entered a secret strategy. If you did everything correctly, Then you will get the result in the form of an alert on the screen! 
// Thank you for choosing our product. We are working for you! 
// 
// Our website: 
// Our store: /shop 
// MQL5 store: https://www.mql5.com/users/vladon/seller 
// ################################################ ###########################

This step was created so that when using the search, you are not lost.

It indicates that the programming of your strategy is over.

Good luck and profits for you!

MetaTrader 4 Conclusions and strategy check.

If you do everything right, then your strategy will be included in the advisor, and you will be able to trade it with the help of my advisor.

Now, we need to test our advisor and strategy.

  1. Load the terminal;
  2. Open a demo account;
  3. Establish an advisor on the chart;

Checking the Expert Advisor in the Strategy Tester:

  1. We open the terminal;
  2. Open the strategy tester;
  3. We choose our expert, currency, TF, and date. We check the tick on every tick based on real ticks;
  4. Open the Settings tab;
  5. We select our strategy;
  6. Turn on Visualization;
  7. Press START;
  8. A graph should open;
  9. If the advisor opens positions, then you did everything right!

What else can be changed in the Expert advisor?

Of course, you want to personalize your strategy and give our advisor a different look!

Parameters that you can change at your discretion:

 // Change 1 Version of Expert Advisor // Version of EA
#define versionea "18.008" 
// Change 2 Advisor's logo // LOGO of EA
#define BMPLOGO "Images \\ dollar.bmp"
// Change 3 Icon of the Expert Advisor // Icon of EA
// # property icon "\\ Images \\ dollar.bmp"
// Change 4 Name of the Expert Advisor on the graph // Name of EA on Graph
#define defEANAME "EXP-The X (OS)"
// Change 5 Link to the full instruction // Link to the full instruction
#define linkTOfull "www.expforex.com/publ/4-1-0-166" 
// Change 6 Demo version of the Expert Advisor. Work only on a demo account or on a live account on a pair USDJPY // Demo version of the advisor. Work only on a demo account or on a live account on a pair USDJPY
bool DEMO = false;

All other parameters I recommend not to change because you can break the work of the Expert Advisor!

Debugging a strategy

The next step is to test and optimize The X and look for your best settings.

Or debug and upgrade your strategy.

Sometimes, it takes days and weeks. Sometimes, even months.

Forex Market – Risk Notifications! This is a very risky way of earning. Improve your strategy.

Good luck and profit!

The output of additional information in the Strategy block

This is an optional option in our open-source code.

You can output absolutely any value of any parameter in the block   EAPADPRO Strategy

1. To do this, you need to create a local variable,

e.g., double EAPADPRO_StrategyValue1 = 0; double EAPADPRO_StrategyValue2 = 0;

2. Assign the value of this variable,

for example, assign the value of the ATR indicator (Find the line   double atr = iATR ):

 EAPADPRO_StrategyValue1 = atr;
EAPADPRO_StrategyValue2 = atrma ; 

Full filter code:

 if (sigInd == 18)
 {

 // + ----------------------------------------------- ------------------- +
 double atrma = iMA (Symbolt, TFTOFILTER, ATR_MA_period, 0, MODE_SMA, ATRprice, shift);
 double atrma2 = iMA (Symbolt, TFTOFILTER, ATR_MA_period, 0, MODE_SMA, ATRprice, shift + 1);
 double atr = iATR (Symbolt, TFTOFILTER, ATR_period, shift);

 if (atrma> atrma2 + atr * ATR_MULTIPLIER) // Buy Signal
 Signal = 1;
 if (atrma <atrma2-atr * ATR_MULTIPLIER) // Sell Signal
 Signal = 2;
 EAPADPRO_StrategyValue1 = atr;
 EAPADPRO_StrategyValue2 = atrma ;
 InfoAboutSignal = "InfoAboutSignal FILTER sigInd =" + sigInd + "Signal =" + Signal
 + "atr =" + atr
 + "atrma =" + atrma
 + "atrma2 =" + atrma
 ;
 } 

3. Next, you need to add the value of the variable to the information output block:

We find in the code function CreateStrategy ()   and add a new line:

ArrayAddStrategy ("StrategyValue1", DoubleToString (EAPADPRO_StrategyValue1,5), "", "");

ArrayAddStrategy ("StrategyValue2", DoubleToString (EAPADPRO_StrategyValue2,5), "", "");

The first parameter of the function = The name of the variable

The second parameter of the function = The value of the variable in a convenient format!

Attention! Add an even number of parameters so that it’s beautiful on the screen!

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


    This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.
    Read more