stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

MQL5 to MQL4 functions
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=6440
Page 1 of 1
Author:  Thunderbolt4x [ Tue Aug 19, 2025 10:16 am ]
Post subject:  MQL5 to MQL4 functions

Hi,

I started to post this subject on another forum but there wasn’t any interest in it so I thought I might try here.

The majority of the code on the Internet that I have seen allowing people to write programs for both MQL4 and MQL5 is written from an MQL5 point of view, which means that coders still need to know how to code for the MT5 platform.

I think that there are still a lot of NON object oriented coders out there who would like to make their existing programs run on MQL5 as well as MQL4 without learning OO and with the minimum of fuss.

What I plan to do is convert MQL4 functions and use the same parameters as the existing functions in MQL4 but make them also work in MQL5. The function name will be the same as that in MQL4 except that it will have an underscore appended to the name. This way it will be displayed in the MQL4 MetaEditor IntelliSense screen beneath the standard one. The function will also return the same value as the standard MQL4 one.

Functions that work the same on both platforms will generally be left alone, it is the ones that are used in MQL4 but not MQL5 or ones that return different values that will be the initial focus of this project.

E.g. in MQL5 the OrdersTotal() function is used differently to how it is in MQL4 and returns the number of pending orders placed. So I use the function below.

Code: Select all

/*
    Count open market and pending orders
*/
int OrdersTotal_()
{
    int orderCount=0;
    
    #ifdef __MQL4__
    // Pending and market orders are held in the same pool
    orderCount = OrdersTotal();
    #endif

    #ifdef __MQL5__
    // Pending and market orders are held in different pools
    orderCount = PositionsTotal() + OrdersTotal();
    #endif

    return orderCount;
}
 
If coders here think there is a need for this project then I will proceed. I say this because I really appreciate comments and suggestions from other coders as I believe there is always a better way to do something.

Thanks,
TB
Author:  dydynamic [ Thu Aug 21, 2025 10:08 pm ]
Post subject:  Re: MQL5 to MQL4 functions

I'm not a coder but i would love to see you pursue your desire of making a better mql4 for the sake of all retail traders that are interested in coding but are finding it hard to code.
thanks for coming here. i really appreciate your kind gesture.
Author:  Thunderbolt4x [ Fri Aug 22, 2025 2:45 am ]
Post subject:  Re: MQL5 to MQL4 functions

OK, I'll start posting the functions but it is important to know that you will still need to know how to code MQL4 programs. All I am doing is making the MQL4 functions compile on both platforms.

With each function I will create scripts to enable the code to run on both platforms as an example. I will also include an explanation of what has been done.
Author:  Thunderbolt4x [ Fri Aug 22, 2025 8:39 am ]
Post subject:  Re: MQL5 to MQL4 functions

Before we dive into the different functions I will explain how we get the same code to work on both the Empty4 and MT5 platforms without doing a lot of copy and pasting.

The code for this example is called TestScript and is attached to this post.

We create 3 files. A file to compile the code file for Empty4, a file to compile the code file for MT5 and the code file itself which contains all the working code. All 3 files are put in the Scripts folder.

The Empty4 compile file is called TestScript.mq4.
The MT5 compile file is called TestScript.mq5.
The code file is called TestScript.mqh.

The code for TestScript.mq4

Code: Select all

/*
    TestScript.mq4
    
    Author: Thunderbolt4x@gmail.com
	
	Description
	==============
    MQ4 stub to compile the script for the Empty4 platform
*/

#property copyright     ""
#property description   ""
#property version       ""
#property strict

// Main code file
#include "TestScript.mqh"
The code for TestScript.mq5

Code: Select all

/*
    TestScript.mq5
    
    Author: Thunderbolt4x@gmail.com
	
	Description
	==============
    MQ5 stub to compile the script for the MT5 platform
*/

#property copyright     ""
#property description   ""
#property version       ""
#property strict        // Not needed for MT5 but the compiler will ignore it

// Main code file
#include "TestScript.mqh"
The lines containing #property must be in the MQ4 and MQ5 files

The code for TestScript.mqh

Code: Select all

/*
    TestScript.mqh
    
    Author: Thunderbolt4x@gmail.com
	
	Description
	==============
    Main code file for the initial example     
*/

void OnStart()
{
    string text = "";

    #ifdef __MQL4__
    text = "Empty4";
    #endif

    #ifdef __MQL5__
    text = "MT5";
    #endif

    Alert("This is a test example script using "+text);
}   
If you have any question then please ask. Also, if anyone has suggestions to improve the code I am going to present then I would love to hear that too.
Author:  Thunderbolt4x [ Mon Aug 25, 2025 12:59 am ]
Post subject:  OrderSend_()

Hi,

I thought that we would start with a function with a bit of meat in it, so this is the OrderSend_() function which replaces the built in Empty4 OrderSend() function.

There are a couple of ways to create orders in MT5 but I think that this is the most transparent way. Another way is to use pre-defined MT5 classes which I think adds another element of complexity to this project and I am trying to keep it as MT4ish as possible.

The example script that calls the function creates two sell orders, one is a market order and the second is a pending order.

MT5 isn’t as straight forward as Empty4 when it comes to placing orders as as they are divided onto separate pools of Orders and Positions. When they go to the broker they become a deal. My goal is to create functions where the Empty4 coder doesn’t need to worry about these different types.

Below is the code for the OrderSend_() function. I have added quite a few comments for those that are interested to read them but it isn't necessary to do that, or to understand the function code at all for that matter. The purpose of this project is to make your Empty4 code work on MT5 without needing to know a lot of the ins and outs of the language differences.

Code: Select all

/*
    Send pending and market orders
*/
int OrderSend_(string symbol, ENUM_ORDER_TYPE cmd, double volume, double price, ulong slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_colour=clrNONE)
{
    int ticket = -1;
    
    #ifdef __MQL4__
    // Nothing needs changing for MQL4
    ticket = OrderSend(symbol, cmd, volume, price, (int)slippage, stoploss, takeprofit, comment, magic, expiration, arrow_colour);
    #endif

    #ifdef __MQL5__
    // We need to create structures to send input properties to the server and also to return result values from the server.    
    MqlTradeRequest request={};
    MqlTradeResult  result={};
      
    // This is what type of order we want to place. DEAL = Market Order and PENDING = Pending order
    ENUM_TRADE_REQUEST_ACTIONS action;
    
    if(cmd <= ORDER_TYPE_SELL)
        action = TRADE_ACTION_DEAL;     // The equivalent of the Empty4 market order 
    else
        action = TRADE_ACTION_PENDING;  // The equivalent of the Empty4 pending order 

    // Add the properties to the sending structure
    request.action          = action;                        
    request.symbol          = symbol;                              
    request.volume          = volume;                                   
    request.type            = cmd;                        
    request.price           = price; 
    request.deviation       = slippage;                                     
    request.magic           = magic;                          
    request.sl              = stoploss;                    
    request.tp              = takeprofit;
    request.type_filling    = ORDER_FILLING_IOC; 
    request.comment         = comment;
    
    if(OrderSend(request, result))
        // Retieve the ticket number from the result structure. The ticket number is actually a long type so to avoid a 
        // compiler warning we explicitly convert it to an int
        ticket = (int)result.order;
    else
        // retcode adds a little more information if there is an error so we might as well report it
        Print("OrderSend error: retcode_external = "+IntegerToString(result.retcode_external)+"   retcode = "+IntegerToString(result.retcode));
    
    #endif

    return ticket;
}
The calling script is probably a bit more interesting. All of the code works in Empty4 and MT5 as is.

I think that the main changes to standard Empty4 code will be the missing predefined variables in MT5. E.G. the following variables are not in MT5
Bid, Ask, Digits and Point.
The code change to get these values is the same in Empty4 and MT5 and is as follows.

Code: Select all

    
    double bid      = SymbolInfoDouble(_Symbol,SYMBOL_BID);
    double ask      = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    // SymbolInfoInteger() actually returns a long but will will it return an int so that the compiler doesn't give a warning
    int digits      = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);  
    double point    = SymbolInfoDouble(_Symbol, SYMBOL_POINT); 
The other "gotcha" is the different properties for the OrderSend() cmd parameter.
OP_BUY, OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT and OP_SELLSTOP are not in MT5 and need to be replaced with

ORDER_TYPE_BUY, ORDER_TYPE_SELL, ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT,ORDER_TYPE_BUY_STOP and ORDER_TYPE_SELL_STOP and are of type ENUM_ORDER_TYPE.

***** Installation Instructions *****
* Copy all three files to the Empty4 and MT5 Scripts folder.
* For Empty4 compile theOrderSend.MQ4 file
* For MT5 compile theOrderSend.MQ5 file
*****************************************************

If you have any questions or suggestions please let me know.
Author:  Thunderbolt4x [ Mon Aug 25, 2025 3:00 am ]
Post subject:  WindowExpertName_()

Hi,

This is a very simple one. This is the WindowExpertName_() function which replaces the built in Empty4 WindowExpertName() function.

In MT5 there is no WindowExpertName() function so we will call MQLInfoString() which is available on both platforms. Below is the code.

Code: Select all

/*
    Return the name of the EA, Indicator or Script
*/
string WindowExpertName_()
{
    return MQLInfoString(MQL_PROGRAM_NAME);
}


***** Installation Instructions *****
* Copy all three files to the Empty4 and MT5 Scripts folder.
* For Empty4 compile file with the MQ4 extension.
* For MT5 compile file with the MQ5 extension.
*****************************************************

If you have any questions or suggestions please let me know.
All times are UTC Page 1 of 1