Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MQL_Easy - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 2 reads | 0 comments |

MQL_Easy is an open source cross-platform library for developing MQL4 and MQL5 applications. The purpose of this library is to make MQL development simple, safe and fast so that you can focus more on implementing complex trading concepts. The cross-platform feature ensures that the same piece of code can run on both platforms. Additionally, it implements some unique features that make development easier and faster. However, it does not cover the entire application programming interface of mql5/4 language. Our goal is not to replace the entire MQL standard library, where metareferences are already well implemented; it simply fills the gap between MQL4 and MQL5 programming and simplifies the development of trading applications.

The library contains the following classes:

It is worth mentioning that part of the code was collected from other resources on the Internet. The goal is to collect any useful code into a framework/library.
The current documentation does not contain all the functionality of the MQL_Easy framework/library. It will be updated regularly in the future.
Feel free to test, change or customize it to better suit your needs.
If you find any issues/bugs/improvements please let me know.

To start using the library, you only need 3 steps.

 #include

(The above line of code includes all files of the library and makes it available)

//+------------------------------------------------------------------+
//| MQL_Easy_Example.mq5 |
//| Copyright 2021, Dionysius Nikolopoulos. |
//| |
//+------------------------------------------------------------------+
#PropertyCopyright " Copyright 2021, Dionysius Nikolopoulos."
#property- related 
# Property version "1.00"
#propertystrict

#include //-- The object C that executes the transaction executes the execution; //-- The object CPosition position that manages the transaction ; //-- The object CPrinter printer that prints the message ; //+----------------------------------------------------------------------------------+
//|Script program startup function |
//+------------------------------------------------------------------+
blank start ()
  { //--- //-- Set the object's symbol and magic number string symbol = _symbol ;
Integer magic number = 12345 ;
  Execute.SetSymbol(symbol);
  Execute.SetMagicNumber(magicNumber);
  Position.SetGroupSymbol(symbol);
  Position.SetGroupMagicNumber(magicNumber);
  
//-- Create trading position BUY ENUM_TYPE_POSITION type = TYPE_POSITION_BUY;
Double volume = 0.10 ;
Double stop loss = 20 ;
Double profit = 20integer deviation = 10 ;
string comment = "test position execution" ;
  execute.Position(type, trading volume, stop loss, take profit, SLTP_PIPS, deviation, comments);
//-- delayed sleep for visual purposes ( 2000 );
//-- Check if the transaction is executed if (position.GroupTotal() > 0 && position.select by index( 0 ) != - 1 ){
//-- Collect information about the transaction long ticket =position.GetTicket();
Double openPrice=position.GetPriceOpen(); 
string message = "#ticket:" + ( string ) ticket + ", opening price:" + ( string ) opening price;
      printer.setTitle( "report" );
      Printer.Add( "Location Execution" , information); 
//-- delayed sleep for visual purposes ( 2000 );
//-- Modify location using quick access features location[ 0 ]. adjust( 50 , 50 ,SLTP_PIPS);
      Printer.Add( "Modify stop loss by 50 points" , "Completed" ); 
//-- delayed sleep for visual purposes ( 2000 );
//-- Use the quick access feature again to modify the location location[ 0 ]. adjust ( 300 , 300 , SLTP_POINTS);
      Printer.Add( "Modify TP SL 300 points" , "Complete" ); 
//-- delayed sleep for visual purposes ( 2000 );
//-- close position [ 0 ]. closure(); 
       Printer.Add( "Close" , "Complete" ); 
      Printer.PrintContent();
  } 
 
       } //+------------------------------------------------------------------+
This code example was written with the MQL5 Editor, but it runs and works fine on both platforms.


MQL_Easy.mql

MQL_ Easy.mqh is an include file that contains all other include files.
This is a quick way to get started without knowing the name of the class. Furthermore, if your development requires extensive use of the MQL_Easy libraries, you can find them all in one place.
Once you become familiar with the library, you will easily include only the classes you need.

The MQL_Easy library has as many folders as implemented classes. Each folder contains an include mql file and another folder called "Includes". In order to use any class, just include the mql include file in your project. The "Includes" folder contains only the implementation of the class for each platform. In some cases, the MQL4 and MQL5 platforms share exactly the same code. However, in many cases the implementation is different, so the "Includes" folder has three files. One has the suffix "base" for implementing shared code, and the other two have the suffix "MT4" and "MT5" for platform-related code. So, this way, not only is the platform-dependent code in a separate file, but it can also be more easily extended with features that one platform supports but not another.

The CExecute class is responsible for transaction execution. There are two types of transactions: positions (Buy, Sell) and orders (BuyLimit, SellLimit, BuyStop and SellStop). A time-saving feature of this class is the ENUM_SLTP_TYPE enumeration, which allows developers to set the type of stop loss and take profit values ​​(price, pips, pips, percentage). So, with a single line of code, the library is responsible for converting the ENUM_SLTP_TYPE function into a price, doing validation checks to ensure that the trade request is valid before sending it to the broker's server and executing the trade on both platforms. If any error occurs, a user-friendly message with the error code and details is displayed in the Expert tab. In addition to printing error information, the CExecute class also populates these details into its own CError object, which developers can use to handle these situations as needed.

 //-- Create an object of CExecute class and set the sign and magic number
string symbol = _symbol ; integer magic number = 12345 ;
CExecute execute(symbol, magicNumber); //-- or CExecute*execute = new CExecute(symbol, magicNumber);
//-- Alternative method C execution for setting symbols and magic numbers ;
Execute.SetSymbol(symbol);
Execute.SetMagicNumber(magicNumber); //-- Create a trading position to buy ENUM_TYPE_POSITION type = TYPE_POSITION_BUY; double the volume = 0.10 ; double the stop loss = 20 ; double the profit = 20 ; 
   Execution.Position(type, trading volume, stop loss, profit, SLTP_PIPS); //-- Create a trading order SellStop execution. Order(TYPE_ORDER_SELLSTOP, 0.10 , 1.1550 , 20 , 20 , SLTP_PIPS);


The CPosition and COrder classes are responsible for managing the account's active and pending transactions, such as collecting information about transactions or closing transactions. With proper configuration, developers can group trades by symbol, magic number, type, or all to easily manipulate these trade groups. In general, the grouping properties of the MQL_Easy library save a lot of time and enable the easy creation of complex trading ideas. Another unique feature implemented by these courses is "quick access" to trade. With just one line of code, developers can retrieve trade attributes such as opening price, opening time, etc.

 //-- Create a Position object that will group trades using the same symbol, magic number //- and type. In this example, the group contains the symbol EURUSD, the magic number trade //-- 12345 and the trade type "Buy".
string symbol = "EUR/USD";
Integer magic number = 12345 ;
  CPosition position(symbol,magicNumber, GROUP_POSITIONS_BUYS);
  
   //-- Returns the total number of active transactions with a specific symbol, magic number and type, 
  //-- ignore all other integers total position = position.GroupTotal(); 

  //-- iterate over a specific trading group for ( integer i = 0 ; i < total number of positions; i++) {
      //-- Select a position by index if (position.SelectByIndex(i)){
Long ticket = position.GetTicket();
Double openPrice = position.GetPriceOpen();
Date and time openTime =position.GetTimeOpen();
Double profit = position.GetProfit();
Print ("#ticket: "+ticket+", openPrice: "+openPrice+", openTime: +openTime+", profit: "+profit);
      } other {
        //-- If the location is not selected the default message error with user-friendly description //-- will be displayed in the experts tab //-- Additionally, you can also retrieve the error message integer error code = location.Error.GetLastErrorCode();  
         //-- Perform custom operations 
           }
  }
//-- Group method example position.GroupTotalProfit(); // Returns the total profit position of a group.GroupTotalVolume(); // Returns the total volume position of a group.GroupCloseAll( 10 ); // Closes all transactions of a group. The number 10 is attempted in case // of failure, by default it is 20 .
//-- Example of quick access via index Double opening price = position[ 0 ].GetPriceOpen();
//-- The above code selects position 0 (the first one) of the group with index and retrieves //-- the opening price.
//-- Here, it retrieves the opening time of the last one. Date and time position opening time = position [position.GroupTotal()- 1 ].GetTimeOpen();

//-- Example of quick ticket access long ticket = 265748761 ; //-- Type long is very important double openPrice = location[ticket].GetPriceOpen();
//-- The code above selects a position by ticket number and retrieves //-- the opening price.

    The difference between Quick Access by Index and Quick Access by Ticket is the type of variable set in parentheses. Indexes are integers, notes are long integers.

    CHistoryPosition and CHistoryOrder are responsible for collecting information about past active and pending transactions. They have the same functionality as the CPosition and COrder classes, such as grouping and quick access. Additionally, they have a start date and an end date to allow specifying a time period as an additional grouping filter. If you don't specify a time period, it will search the entire history of the account.

     C historical positions historical positions;
    Historical location.SetHistoryRange(D' 2019.03 . 25 00 : 00 : 00 ', time current ()); for ( integer i = 0 ; i Print("#ticket: "+historyPosition[i].GetTicket()+" Time off:
      "+historyPosition[i].GetTimeClose());
      }
    //-- The above code prints the tickets and closing time of all positions since the date 2019.03 . 25 // -- 00:00:00 so far .


    The CUtility class applies some common useful functions that may be required by trading applications.

     CUtilities Utilities( "EUR/USD" ); if (utils.IsNewBar( PERIOD_M1 )){
    print ( "One minute has passed!!!" );
    }


    The CPrinter class implements a fast and nice way to send custom formatted messages to a terminal. It can be useful for error messages and debugging.

     CPrinter printer;  
     printer.setTitle( "attention" );
    Printer.SetContainer( “-” );
    printer.add( "Take action for user" , "You need to enable automatic trading!!!" );
    printer.add( "Step" , "Press the AutoTrade button at the top of the terminal" );
    printer. Print ();


    The above code is displayed in the Expert tab:

    - - - - - - - - Attention - - - - - - - -

    Note to users: You need to enable automatic trading!

    Steps: Press the AutoTrade button at the top of the terminal

    - - - - - - - - Attention - - - - - - - -

    The CError class is responsible for handling errors. By default, it includes all available error codes and their descriptions. As a result, developers can handle errors faster or send meaningful information to users.

     Double question = symbol information double ( "WRONG_SYMBOL" , SYMBOL_ASK ); //This line generates an error CError error;
    Error.CreateErrorCustom( "An error occurred!!!" , true );


    The above code will display:

    - - - - - - - - mistake- - - - - - - -

    Message: An error occurred! ! !

    Error (4301): Unknown symbol

    - - - - - - - - mistake- - - - - - - -

    The CValidationCheck class implements useful functions for order checking and validation. Before sending a transaction request to the server, it is important to pass some checks. Most of them are well known, which is why the CExecute class uses them by default. Therefore, it is very useful to be able to use this class whenever needed.

     //-- Function to check if the trade volume (lots) is valid Check the volume value ( string symbol, double the volume) //-- Function to check if there is enough available balance to make another trade Check the transaction currency ( string symbol, double the volume, ENUM_ORDER_TYPE type)


    The power of any library/framework is not only that it does its job correctly, but also that it builds a community around it.
    The community should lead the MQL_Easy framework like any other open source project. Please feel free to contribute at any level (github: https://github.com/Denn1Ro/MQL_Easy ).


    Attachment download

    📎 mql_easy_example.mq5 (2.27 KB)

    📎 mql_easy_example.mqh (261.83 KB)

    Source: MQL5 #25090

    Verification code Refresh