Русский 中文 Español Deutsch 日本語 Português 한국어 Français Italiano Türkçe
preview
Learn Why and How to Design Your Algorithmic Trading System

Learn Why and How to Design Your Algorithmic Trading System

MetaTrader 5Trading | 21 January 2022, 10:35
23 857 16
Mohamed Abdelmaaboud
Mohamed Abdelmaaboud

Introduction

We can say without any doubt that the importance of programming or coding increases every day in all fields of our world. So, we can observe how programming or coding can contribute to ease our life and not only make our life easy but guarantee accurate outputs according to what you want and what you determined before.

When it comes to the world of trading—this magnificent field and career—we can say that programming or coding makes our trading easy and systematic through created programs that contribute to give us that ease and automation once we completed an accurate and good program with what we expect and want to get from this program. So, the world of coding can give us a lot of benefits, but in my opinion the most important benefit for coding to trade is helping you to be disciplined as like we all know as traders there is always a challenge to be disciplined with our trading decision. Discipline is a great and important trait which affects our trading and investing results, so it is not an option to be disciplined at your trading. As I always like to say, Discipline is the key to your success in trading and whole life. To identify discipline in a simple way, it means to do what you have to do at the right time whatever surrounded circumstances. So, when we find a tool which helps to achieve this, we have to be attentive and learn what it is. And the right tool for this is coding.

As we all know, the most popular thing which prevents us from being disciplined while trading is emotions, and we have to avoid these emotions or avoid the effect of these emotions on our trading decisions in a negative way. And I want you to imagine if you have a system which works for you with your predetermined parameters without human interfering. So, in this case the negative effect of emotions on our trading decisions will be avoided. The good news is that we have a tool which helps us do that. Here I’ll write about MQL (MetaQuotes Language) for the MetaTrader platform. This great programming language or tool will help us to design our trading system with our specific parameters which will guarantee specific actions or specific trading decisions.

If you want to understand more about this concept, let us mention this example. If we have two investors (A&B) and their trading strategy is the same, it’s buying and holding during an uptrend and selling when reversing of the trend, but each one of them behave differently—investor A is disciplined but investor B is not. Look at the below figures:


2- Investor_B


So, according to the previous figures, it’s obvious that discipline is essential for the good result, but the lack of discipline will drive to poor results.

And through this article I’ll share with you simple trading system Simple Moving Averages crossover to learn how to design your own one through sharing some of the basics of MQL5 coding with examples to practice these basics and to understand them deeply. The purpose is to give you an overview about what you can do using this magnificent tool.

The main objective of this article is to guide beginners to learn how to design their algorithmic trading system in MQL5 through learning some of the basics of MQL5 for a simple trading system idea which will be coded step by step through this article after explaining some of the MQL5 basics. We'll code them by scripts then we'll present the result after code execution. To enhance your understanding, I advise you to apply and code what you’ll read here by yourself as this will help you to understand concepts of mentioned codes deeply. And be noted that all created codes, programs, and trading strategies in this article are designed for educational purposes only, not for anything else. And be noted that we'll use MQL5 to write codes.


What we need in order to design our algorithmic trading

In this part, I’ll mention what we’ll want to have as tools and what we want to know about these tools:

  • The MetaTrader 5 platform, a.k.a. MetaTrader 5 Terminal. To execute orders and test our codes through the terminal. And MetaTrader is the most popular trading platform.



3- MT5 platform

A Demo Account. You can open a demo account with your broker to have virtual money to test your trading strategies risk-free but in the same market environment. Please be sure to use this demo account not your real account for coding as you will need to create and execute programs which will carry out transactions on your account.

MetaQuotes Language Editor, where we’ll write our codes or programs. The following screenshots will show how to open it as it is installed with MetaTrader. There are three ways to open it.

  • Click Tools menu then MetaQuotes Language Editor:

4- MetaEditor opening  

                   

Or click MetaQuotes Editor Icon:

   5- MetaEditor opening

Or press F4 button from Keyboard while in the open terminal.

The following screenshot shows how it looks like, and here will be the most of our work to write our programs and design our trading systems.

   6- MetaEditor window

 

Now we need to use this editor to write our first code, so follow the following steps to know how to do that.

Click on New then you will find more than one type of programs to choose from:

7- MetaEditor window



8- MetaEditor - New



What we need to mention here in this article are: Expert Advisor, Custom Indicator, and Script.

  • Expert Advisor: EA is a program in the terminal that is developed and used for automation of analytical and trading processes according to the parameters you set.
  • Custom Indicator: is a program coded; it is basically intended for graphical displaying of preliminarily calculated dependencies.
  • Script: is a program intended for a single performing of any action, it can fulfill both analytical and trading functions, and unlike Expert Advisors, it is executed on request, not by ticks.

The Hello World! program

In this part, we will learn how to write our first program and our first code in MQL5. All beginners in programming or coding start their journey by coding the “Hello World” code. So, we’ll start by writing a program that prints on the terminal screen “Hello World”. Let’s do it…

Open the MetaEditor as shown above, then click new, then select from the options (Script), then click next.


9- MetaEditor - New file - Script


After clicking next, the following window will appear. Fill the data of the script that you want:

  • Name is the name of the script
  • Author is the script creator
  • Link is your link
  • Parameter is the parameters that you need to set for the script but we will not set any parameter for this script, so we will skip it.
Then click Finish.

10- MetaEditor - Script info


After clicking Finish the following window will be opened. And in this window, our program will be coded. Here as I mention, we need to design a program that show “Hello World!” in the terminal. So, we’ll start wring our code between the two curly brackets.


 11 - Codes place

Here we will use:

  • Alert: it prints what I determine or displays a predetermined message in the program which is here in our example “Hello World!”
  • ( “ ”): to write in between what I want or the predetermined message "Hello World!" or anything else.
  • ; - to separate sentences.

Our code will be the same as:

//+----------------------------------------------------------+
//|                                         Hello World!.mq5 |
//|                          Copyright 2022, MetaQuotes Ltd. |
//|                                     https://www.mql5.com |
//+----------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+----------------------------------------------------------+
//| Script program start function                            |
//+----------------------------------------------------------+
void OnStart()
  {
   Alert("Hello World!");
  }
//+----------------------------------------------------------+

After writing our code then click Compile, then make sure that there are no errors and warnings. The Errors tab must be without errors after compiling your code to make sure that your program will run the same as what I mention in the following picture. Then go to the terminal by pressing F4 to test our code.

12- Hello World code

After that in the Navigator window of the terminal, under scripts, you will find your program with the name which you determined before (Hello World!), drag it and drop on the chart or double click on it. Then you will see that an Alert message appears with what you determined at your code. It should be the same as in the following screenshot.

13- Hello World alert



Trading strategy idea (Two Simple Moving Average crossover)

It this part, I’ll present a trading strategy idea for educational purposes only. It is aimed at helping you learn the basics of MQL5 and how to program in MQL5.

Disclaimer
Any information is provided ‘as is’ solely for informational purposes and is not intended for trading purposes or advice. Past performance is no guarantee of future results. If you choose to use these materials on any trading accounts, you are doing so at your own risk.

Here, the strategy idea is trying to trade with the trend using a confirmation from the signal of two simple moving average indicator:

  • Simple Moving Average is a lagging technical indicator which calculates the average closing price of a specific period, and it is a lagging as its signal comes after the signal of price action.
  • The Strategy is as follows:
    • If shorter simple moving average (its period is 20) crossovers the longer simple moving average (its period is 50) to be above it, so the signal is Buy.
    • If shorter simple moving average (its period is 20) crossovers the longer simple moving average (its period is 50) to be below it, the signal is Sell.

Here, we need to design a program to do that.

15- Trading Idea1


14- Trading Idea


Algorithmic trading system blueprint

In this part I’ll talk about a very important step which you have to do if you want to code your system easily and smoothly. This step is to create a blueprint for your strategy and trading idea in manner of sequence of steps of what you want your system to do exactly, and you can do that through a diagram for example which will gives you a clear blueprint. And here is an example for our (Two Simple Moving Average Crossover) system blueprint to see in a clear way what we need to code and what we expect to get from this system.

    16- Simple 2MA-Blueprint-–-MQL5


      Now, let us first understand some of the basics of MQL5, then use what we need to design.


      Variables and types of them and how can we use

      In this part, we will identify and understand:

      • What are Variables?
      • Types of Variables.
      • How can we use them?

      Generally speaking, in a program, a data values can be constant or variable. If values are variable they can be changed by the program and the user. A variable is a memory location. It has a name that is associated with that location. So, the memory location is used to hold data. A program in MQL5 can contain tens and hundreds of variables. A very important property of each variable is the possibility to use its value in a program. The limitation of this possibility is connected with the variable scope which is a location in a program where the value of the variable is available. Every variable has its scope.

      So, according to the scope there are two types of variables in MQL5, local and global. A local variable is a variable declared within a function. The scope of local variables is the body of the function, in which the variable is declared. A local variable can be initialized by a constant or an expression corresponding to its type. Global variables are declared beyond all functions. The scope of global variables is the entire program. A global variable can be initialized only by a constant corresponding to its type (and not expression). Global variables are initialized only once before stating the execution of special functions.

      Regardless of the scope of variables, now we will look at the following types of variables:

      • int is a numerical type; there are different types of integers to store numerical values of different length.
      • double. Building a program that will handle numbers used in trading requires a data type capable of managing floating-point numbers. So MetaTrader offers the following data types to deal with these data: float and double. The difference between them is the bytes allocated in memory, 4 for float and 8 for double, resulting in the following minimal and maximum values:
        • float - minimum 1.175494351e-38, maximum 3.402823466e+38
        • double - minimum 2.2250738585072014e-308, maximum 1.7976931348623158e+308
          To declare these types of data, use float and double keywords.
      • string is a very important data type, which is widely used as well in MQL5 coding. String allows to store and manipulate any alphanumerical sequence of characters.
      • bool - Boolean is a logical type that can assume value of either true or false.

      Let us take an example for using variable:

      //+------------------------------------------------------------------+
      //|                                                    Variables.mq5 |
      //|                                  Copyright 2022, MetaQuotes Ltd. |
      //|                                             https://www.mql5.com |
      //+------------------------------------------------------------------+
      #property copyright "Copyright 2022, MetaQuotes Ltd."
      #property link      "https://www.mql5.com"
      #property version   "1.00"
      //+------------------------------------------------------------------+
      //| Script program start function                                    |
      //+------------------------------------------------------------------+
      void OnStart()
        {
          int myInteger = 5;
          double myDouble = 10.56;
          string myString = "My name is Mohamed";
          bool myBoolean = true;
          
          Alert(myInteger);
          Alert(myDouble);
          Alert(myString);
          Alert(myBoolean); 
        }
      //+------------------------------------------------------------------+
      

      After compiling the previous code if you write like what I mention above, you will find no errors or warnings and you must see the Alert window like below screen shot.

      18- Variables

      Another example for using variables.

        Here we need to store or memorize variables and the value of them, A and its value 10, B = 10, C = 10 + 5, var1 = 2.5, var2 = 4, result = 2.5/4, message1 = Hello Mohamed, and message2 = Value of A is: 10. So, when we execute this code, the alert message will contain 4 elements:
        • The equivalent value of message1
        • The equivalent value of C
        • The equivalent value of result
        • The equivalent value of message2
        //+------------------------------------------------------------------+
        //|                                                  Variables 2.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        void OnStart()
          {
           int A = 10;
           int B = 5;
           int C;
         
           double var1 = 2.5;
           double var2 = 4;
           double result = var1 / var2;
           
           string greeting = "Hello";
           string space = " ";
           string name = "Mohamed";
           string message1;
           string message2;
           
           C = A + B;
           message1 = greeting + space + name;  
           message2 = "Value of A is: " + string(A);
           
           Alert(message1);
           Alert(C);
           Alert(result);
           Alert(message2);
          }
        //+------------------------------------------------------------------+
        


         19- Variables 2

        Boolean operations

        Boolean: simply it returns true or false for a logical operation.

        • == means equal
        • != means not equal
        • < means lesser than
        • <= means lesser than or equal
        • > means greater than
        • >= means greater than or equal
        //+------------------------------------------------------------------+
        //|                                           Boolean Operations.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        void OnStart()
          {
           bool result = 4 < 5;
           Alert (result);     //true
          }
        //+------------------------------------------------------------------+
        

        Here it must return true at alert message as 4 < 5.

         20- Boolean Operations

        The While loop

        The while operator consists of a checked expression and the operator, which must be fulfilled. If the expression is true, the operator is executed until the expression becomes false. In the following example, we can understand that first alerting at the start to know the start and end of the loop then the program will check the value of the counter and show its value if it is less than 3. Then it will add one to previous result till became false in this case = or > 3, then give the last alert when the loop has finished. So, we’ll see the following messages from this code in the alert message: Start of script, 1, 2, Loop has finished.
        //+------------------------------------------------------------------+
        //|                                                   While Loop.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        void OnStart()
          {
           //While (Loop)
           Alert("Start of script");
           
           int counter = 1;
           
           while(counter < 3) //true?
            {
              Alert(counter);
              counter = counter + 1;
            }
            
            Alert("Loop has finished");
          }
        


         21- While Loop


        The For loop

        The for operator consists of three expressions and an executable operator:

                 for(expression1; expression2; expression3)
                 operator;

        Expression1 describes the loop initialization. Expression2 checks the conditions of the loop termination. If it is true, the loop body for is executed. The loop repeats expression2 until it becomes false. If it is false, the loop is terminated, and control is given to the next operator. Expression3 is calculated after each iteration.

        So according to the following loop example of for, we can expect that the execution of the code will result five messages of Hello, (I =0, I = 2, …………,I = 4), then it will = 5, so the loop will be terminated.

        //+------------------------------------------------------------------+
        //|                                                     For Loop.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        void OnStart()
          {
           for(int i=0; i<5 ; i++)
           {
           Alert("Hello");
           }
          }
        //+------------------------------------------------------------------+
        


        22- For Loop

        The IF, Else statement

        The IF - ELSE operator is used when a choice must be made. Formally, the syntax is as follows:


        if (expression)
        operator1
        else
        operator2


        If the expression is true, operator1 is executed and control is given to the operator that follows operator2 (operator2 is not executed). If the expression is false, operator2 is executed.

        Example: according to the following example, we need to receive an alert with the Bid value first, then we determine if Bid value > 1.146600, then we will be alerted with “The price is above 1.146600 -> BUY”. If it is not, then we will be alerted with “The price is below 1.146600 -> SELL”. And the following is the code and its execution result.

        //+------------------------------------------------------------------+
        //|                                            If-else statement.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        void OnStart()
          {
           double level = 1.146600;
           double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
           Alert("Bid Price = " + string(Bid));
        
          if(Bid > level)
          {
          Alert ("The price is above " + string(level) + " -> BUY");
          }
        // What if the condition is false and we need to take an action also here we can use else function instead of writing another if code
           else
           {
           Alert ("The price is below " + string(level) + " -> SELL");
           } 
            
          }
        //+------------------------------------------------------------------+
        


         23- If-Else Statement


        Trader inputs

        In this part we will learn how to determine our inputs or preferred parameters for the program instead of adjusting the code.

        Properties (#property): Every MQL5-program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs without the necessity to launch them explicitly.

        script_show_inputs: Display a window with the properties before running the script and disable this confirmation window

        Input variables: The input storage class defines the external variable. The input modifier is indicated before the data type.

        //+------------------------------------------------------------------+
        //|                                                Trader Inputs.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        #property script_show_inputs
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        input int TakeProfit = 10;
        input int StopLoss = 10;
        
        void OnStart()
          {
           double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
           double TakeProfitLevel = Bid + TakeProfit * 0.00001; // 0.00001 (5 digits broker, so multiplied by 10)
           double StopLossLevel = Bid - StopLoss * 0.00001;
           
           Alert("Price now = " + string(Bid));
           Alert ("TakeProfitLevel = ", TakeProfitLevel);
           Alert ("StopLossLevel = ", StopLossLevel);
          }
        //+------------------------------------------------------------------+
        

        24- Trader Input 1


        24- Trader Input 2


         24- Trader Input 3


        24- Trader Input 4


        Opening orders

        In this part, I’ll present how to write your code to open an order:

        //+------------------------------------------------------------------+
        //|                                                         TEST.mq5 |
        //|                                  Copyright 2022, MetaQuotes Ltd. |
        //|                                             https://www.mql5.com |
        //+------------------------------------------------------------------+
        #property copyright "Copyright 2022, MetaQuotes Ltd."
        #property link      "https://www.mql5.com"
        #property version   "1.00"
        #property script_show_inputs
        #include <Trade\Trade.mqh>
        CTrade trade;
        //+------------------------------------------------------------------+
        //| Script program start function                                    |
        //+------------------------------------------------------------------+
        input int TakeProfit = 150;
        input int StopLoss = 100;
        
        void OnStart()
          {
           double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
           double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
           double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
           double Equity = AccountInfoDouble(ACCOUNT_EQUITY); 
           double TakeProfitLevel = (Ask+TakeProfit*0.00001);
           double StopLossLevel = (Ask-StopLoss*0.00001);
           
           
           if(Equity >= Balance)
           trade.Buy(0.01,NULL,Ask,StopLossLevel,TakeProfitLevel,NULL);
           
           for (int i=PositionsTotal()-1; i>=0; i--)
           {
             ulong ticket = PositionGetTicket(i);
             ENUM_POSITION_TYPE position = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));
        
              
             Alert (" Order Ticket # ", ticket);
             Alert("TakeProfit = ", TakeProfitLevel);
             Alert("StopLoss = ", StopLossLevel);
           }
          }
        //+------------------------------------------------------------------+
          After the execution of the script, the result will be as follows:

          25- Opening orders


          Errors handling technique

          When the program runs, anyone can find crashes, and something went wrong. So, we have to include this technique to our code to check every executed order or code and warns if something went wrong. In other words, it is a protection technique for the trader to protect him and his fund from any inappropriate situation. 

          //+------------------------------------------------------------------+
          //|                                                         TEST.mq5 |
          //|                                  Copyright 2022, MetaQuotes Ltd. |
          //|                                             https://www.mql5.com |
          //+------------------------------------------------------------------+
          #property copyright "Copyright 2022, MetaQuotes Ltd."
          #property link      "https://www.mql5.com"
          #property version   "1.00"
          #property script_show_inputs
          #include <Trade\Trade.mqh>
          CTrade trade;
          //+------------------------------------------------------------------+
          //| Script program start function                                    |
          //+------------------------------------------------------------------+
          input int TakeProfit = 150;
          input int StopLoss = 100;
          
          void OnStart()
            {
             double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
             double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
             double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
             double Equity = AccountInfoDouble(ACCOUNT_EQUITY); 
             double TakeProfitLevel = (Ask+TakeProfit*0.00001);
             double StopLossLevel = (Ask-StopLoss*0.00001);
             
             
             if(Equity >= Balance)
             trade.Buy(0.01,NULL,Ask,StopLossLevel,TakeProfitLevel,NULL);
             
                for (int i=PositionsTotal()-1; i>=0; i--)
                {
                   ulong ticket = PositionGetTicket(i);
                   ENUM_POSITION_TYPE position = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));
                  
                 if (ticket <= 0)
                  {
                   Alert("Error!");  //in Case of error and the order did not open, appears "Error!"
             
                  }
                else
                  {
                   Alert("Your ticket # is: " + string(ticket));
                   Alert("TakeProfit = ", TakeProfitLevel);
                   Alert("StopLoss = ", StopLossLevel);
                  }
                }
            }
          //+------------------------------------------------------------------+
          

          26- Errors programming techniques 1


          27- Errors programming techniques 2


          Closing orders

          In this part, we’ll create code to open and close an order to learn how to close an order.
          //+------------------------------------------------------------------+
          //|                                                         TEST.mq5 |
          //|                                  Copyright 2022, MetaQuotes Ltd. |
          //|                                             https://www.mql5.com |
          //+------------------------------------------------------------------+
          #property copyright "Copyright 2022, MetaQuotes Ltd."
          #property link      "https://www.mql5.com"
          #property version   "1.00"
          #property script_show_inputs
          #include <Trade\Trade.mqh>
          CTrade trade;
          //+------------------------------------------------------------------+
          //| Script program start function                                    |
          //+------------------------------------------------------------------+
          input int TakeProfit = 150;
          input int StopLoss = 100;
          
          void OnStart()
            {
             double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
             double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
             double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
             double Equity = AccountInfoDouble(ACCOUNT_EQUITY); 
             double TakeProfitLevel = (Ask+TakeProfit*0.00001);
             double StopLossLevel = (Ask-StopLoss*0.00001);
             
             
             
             trade.Buy(0.01,NULL,Ask,StopLossLevel,TakeProfitLevel,NULL);
             
             for (int i=PositionsTotal()-1; i>=0; i--)
             {
               ulong ticket = PositionGetTicket(i);
               ENUM_POSITION_TYPE position = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));
          
                
               Alert (" Order Ticket # ", ticket);
               Alert("TakeProfit = ", TakeProfitLevel);
               Alert("StopLoss = ", StopLossLevel);
               
               Sleep(2000);
               
               trade.PositionClose(ticket,-1);
               Alert("Order Closed...");
             }
          
            }
          //+------------------------------------------------------------------+


           28- Closing orders

          Adjusting orders by OrderModify

          In this part, we will learn how to create code which help to modify order. It will modify the characteristics of the previously opened or pending orders.

          //+------------------------------------------------------------------+
          //|                                                         TEST.mq5 |
          //|                                  Copyright 2022, MetaQuotes Ltd. |
          //|                                             https://www.mql5.com |
          //+------------------------------------------------------------------+
          #property copyright "Copyright 2022, MetaQuotes Ltd."
          #property link      "https://www.mql5.com"
          #property version   "1.00"
          #property script_show_inputs
          #include <Trade\Trade.mqh>
          CTrade trade;
          //+------------------------------------------------------------------+
          //| Script program start function                                    |
          //+------------------------------------------------------------------+
          input int TakeProfit = 150;
          input int StopLoss = 100;
          
          void OnStart()
            {
             double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
             double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
             double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
             double Equity = AccountInfoDouble(ACCOUNT_EQUITY); 
             double TakeProfitLevel = (Ask+TakeProfit*0.00001);
             double StopLossLevel = (Ask-StopLoss*0.00001);
             double TakeProfitLevel2 = (TakeProfitLevel+0.00100);
             double StopLossLevel2 = (StopLossLevel-0.00050);   
             
             
             trade.Buy(0.01,NULL,Ask,StopLossLevel,TakeProfitLevel,NULL);
             
             for (int i=PositionsTotal()-1; i>=0; i--)
             {
               ulong ticket = PositionGetTicket(i);
               ENUM_POSITION_TYPE position = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));
          
                
               Alert (" Order Ticket # ", ticket);
               Alert("TakeProfit = ", TakeProfitLevel);
               Alert("StopLoss = ", StopLossLevel);
               
               Sleep(5000);
               
               trade.PositionModify(ticket,StopLossLevel2,TakeProfitLevel2);
               Alert("Order Modified...");
               Alert("Modified TakeProfit = ", TakeProfitLevel2);
               Alert("Modified StopLoss = ", StopLossLevel2);
             }
          
            }
          //+------------------------------------------------------------------+
          

          29- Modifying orders



          The Two Simple Moving Average Crossover system

          In this part, we’ll put all together to code two simple moving averages crossover. All will do everything required according to the above-mentioned blueprint.

          Here, we’ll choose an Expert Advisor when starting a new project by clicking new from MetaEditor.

          To remember everything, here is our detailed trading system blueprint:

          16- Simple 2MA-Blueprint-–-MQL5


          What we need to do now is to code this strategy:
          • If shorter Simple Moving Average (its period is 20) crosses the longer simple moving average (its period is 50) to be above it, the signal is Buy.
          • If shorter Simple Moving Average (its period is 20) crosses the longer simple moving average (its period is 50) to be below it, the signal is Sell.
          And the following is the code to this program and how to execute it.
          //+------------------------------------------------------------------+
          //|                                                SMA crossover.mq5 |
          //|                                  Copyright 2022, MetaQuotes Ltd. |
          //|                                             https://www.mql5.com |
          //+------------------------------------------------------------------+
          #property copyright "Copyright 2022, MetaQuotes Ltd."
          #property link      "https://www.mql5.com"
          #property version   "1.00"
          //+------------------------------------------------------------------+
          //| Expert tick function                                             |
          //+------------------------------------------------------------------+
          
          void OnTick()
            {
             //create an array for several prices
             double myMovingAverageArray1[], myMovingAverageArray2[];
             
             //define the properties of  MAs - simple MA, 1st 20 / 2nd 50
             int movingAverage1 = iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE);
             int movingAverage2 = iMA(_Symbol,_Period,50,0,MODE_SMA,PRICE_CLOSE);
             
             //sort the price arrays 1, 2 from current candle
             ArraySetAsSeries(myMovingAverageArray1,true);
             ArraySetAsSeries(myMovingAverageArray2,true);
             
             //Defined MA1, MA2 - one line - currentcandle, 3 candles - store result
             CopyBuffer(movingAverage1,0,0,3,myMovingAverageArray1);
             CopyBuffer(movingAverage2,0,0,3,myMovingAverageArray2);
             
             //Check if we have a buy entry signal
             if (
                (myMovingAverageArray1[0]>myMovingAverageArray2[0])
             && (myMovingAverageArray1[1]<myMovingAverageArray2[1])
                )
                   {
                   Comment("BUY");
                   }
              
             //check if we have a sell entry signal      
             if (
                (myMovingAverageArray1[0]<myMovingAverageArray2[0])
             && (myMovingAverageArray1[1]>myMovingAverageArray2[1])
                )
                   {
                   Comment("SELL");
                   }          
            }
          //+------------------------------------------------------------------+
          


          After executing the program, it will display comments on the chart with the current signal (buy or sell) according to this strategy. This is shown in the following picture:


          30- SMA - comment


           31- SMA - Sell comment

          Now the following pictures will explain how we can find our designed system from the terminal and how to execute it:

          32- Simple MA program place

          After drag and drop or double click on the program, the following window will pop up:

          33- Simple MA program interface

          Enable the 'Allow Algo Trading' option, then click Ok. After that the Expert Advisor will be attached to the chart and a message will appear in the Journal tab as it is loaded successfully:


           34- Simple MA program activated1


          35- Simple MA program activated2

          So, we have created and executed our automation Two Simple Moving Averages Crossover program. And what I want to mention here is that this is a simple example for what we can code for our trading. However, we can code and automate any other trading strategy using this magnificent tool MQL5.

          Again, the main objective from this article is not to use this strategy but the main objective is to learn some of the basics of MQL5 and to learn what we can create to imagine what we can do and to make our trading easier and profitable. So, this strategy needs a lot of enhancements and if you use it, it will be your only responsibility.


          Conclusion

          MQL5 is a good and magnificent tool to help us make our trading easier and profitable through designing good and profitable algorithmic trading systems. So, your investment in learning and using this magnificent tool will be a highly rewardable investment.

          Last comments | Go to discussion (16)
          1000193559
          1000193559 | 8 May 2022 at 16:10

          I'm so glad I saw this today.

          I'll certainly work with this to start my Algo Trading.

          paulus itumeleng
          paulus itumeleng | 8 May 2022 at 19:44
          I'm new in trading I need a scalping robot
          Juvenille Emperor Limited
          Eleni Anna Branou | 8 May 2022 at 19:45
          paulus itumeleng #:
          I'm new in trading I need a scalping robot

          Don't we all?

          Such discussions are not allowed in the forum, you should make your own search.

          BND
          BND | 8 Aug 2022 at 14:09
          I really like your work and ideas. If you don't mind could you consider the Ultimate Oscillator indicator. I would connect it with Mass Indicator (MI). What do you think?
          Johnson Mukiri Mbuthia
          Johnson Mukiri Mbuthia | 8 Sep 2022 at 09:17

          I like the simplicity of how you explain things in all your articles Mohammed. I develop in MQL4 mainly. What's the quickest way to transition to MQL5 and what are the benefits in your opinion?

          Thanks

          Graphics in DoEasy library (Part 89): Programming standard graphical objects. Basic functionality Graphics in DoEasy library (Part 89): Programming standard graphical objects. Basic functionality
          Currently, the library is able to track standard graphical objects on the client terminal chart, including their removal and modification of some of their parameters. At the moment, it lacks the ability to create standard graphical objects from custom programs.
          WebSockets for MetaTrader 5 — Using the Windows API WebSockets for MetaTrader 5 — Using the Windows API
          In this article, we will use the WinHttp.dll to create a WebSocket client for MetaTrader 5 programs. The client will ultimately be implemented as a class and also tested against the Deriv.com WebSocket API.
          Universal regression model for market price prediction (Part 2): Natural, technological and social transient functions Universal regression model for market price prediction (Part 2): Natural, technological and social transient functions
          This article is a logical continuation of the previous one. It highlights the facts that confirm the conclusions made in the first article. These facts were revealed within ten years after its publication. They are centered around three detected dynamic transient functions describing the patterns in market price changes.
          Manual charting and trading toolkit (Part III). Optimization and new tools Manual charting and trading toolkit (Part III). Optimization and new tools
          In this article, we will further develop the idea of drawing graphical objects on charts using keyboard shortcuts. New tools have been added to the library, including a straight line plotted through arbitrary vertices, and a set of rectangles that enable the evaluation of the reversal time and level. Also, the article shows the possibility to optimize code for improved performance. The implementation example has been rewritten, allowing the use of Shortcuts alongside other trading programs. Required code knowledge level: slightly higher than a beginner.