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

Trading classes from MT5 to MT4 - MetaTrader 4 library | MT5 EA download - MetaTrader 5 resources

author EAcpu | 3 reads | 0 comments |

During the development of the program for copying positions between accounts, it was necessary to develop essentially the same code for MT5 and MT4. The difference lies only in the functionality of performing trading operations. Therefore, several classes in the MT5 standard library were rewritten to work in MT4.

When using this library, ideally the program can be compiled from MT5 using the compiler in MT4 so that it can be run in MT4.

But there are two important limitations:

In this version, there may be some class methods not found Because it needs to be corrected during testing .

The file TradeLibraryMT5Example.mq4 is a simple EA example compiled and run on MT5 and MT4.

The following code is used to open a pending order:

 CTrade m_trade;

...

resolution = m_trade.OrderOpen (m_symbol, ORDER_TYPE_BUY_LIMIT , m_lot, lowPrice, lowPrice, 0 , 0 , ORDER_TIME_GTC , 0 );
resolution = m_trade.OrderOpen (m_symbol, ORDER_TYPE_SELL_LIMIT , m_lot, HIGH PRICE, HIGH PRICE, 0 , 0 , ORDER_TIME_GTC , 0 );

The function to calculate the total profit of all open market positions can be implemented as follows:

 //+------------------------------------------------------------------+
//|Calculate the total profit of all market orders |
//+------------------------------------------------------------------+
Double Get Profit(){
Total integer = total number of positions ();
Double profit = 0 ;

C location information ;

for ( integer i = total - 1 ; i >= 0 ; i -) {
if ( p.select(i) by index ){
Oolong magic = p.Magic() ;
String symbol = p. symbol () ;
if (magic != m_magicN || symbol != m_symbol) {
continue ;
        }

        Profit += p.Profit() + p.Commission() + p.Swap() ;
      }
  }

return profit;
}

The function of closing all open positions and pending orders can be implemented as follows:

 //+------------------------------------------------------------------+
//|Try to close all pending and market orders |
//+------------------------------------------------------------------+
boolean tryCloseAll() {
boolean resolution = true ;
  
all integers ;
Double profit = 0 ;
own ticket;
Oolong magic;
String symbol;
  
   total = total number of orders () ;

Order information ;

for ( integer i = total - 1 ; i >= 0 ; i -) {
if ( o.select(a) by index ){
        Magic = o.Magic ();
        symbol = o. symbol ();
if (magic != m_magicN || symbol != m_symbol) {
continue ;
        }
        ticket = o.ticket ();

        Resolution&= m_trade.OrderDelete(o.Ticket()) ;
      }
  }

  Total= total number of positions () ;
  
C location information ;

for ( integer i = total - 1 ; i >= 0 ; i -) {
if ( p.select(a) by index ){
        Magic = Magician ();
        symbol = p. symbol ();
if (magic != m_magicN || symbol != m_symbol) {
continue ;
        }
        ticket = p.ticket ();

        Resolution&= m_trade.PositionClose(order) ;
      }
  }

return resource;
}



Attachment download

📎 orderinfo.mqh (43.87 KB)

📎 positioninfo.mqh (15.92 KB)

📎 symbolinfo.mqh (71.44 KB)

📎 trade.mqh (163.76 KB)

📎 tradelibrarymt5example.mq4 (19.6 KB)

📎 tradelibrarymt5example.mq4

Source: MQL5 #39161

Trading classes from MT5 to MT4 - Important points before using the MetaTrader 4 library

This page has been supplemented with structured instructions, focusing on the purpose of EA source code, MQL4/MQL5 platform compatibility, testing process and risk control. Before downloading or deploying, it is recommended to first determine whether it is consistent with your own variety, cycle and account environment.

Suitable for who to use

  • Users who want to quickly screen EA source code and are willing to do simulation disk verification first.
  • People who need to compare MT4/MT5 EAs, indicators, scripts or source code projects.
  • Traders who wish to record backtests, parameters and risk boundaries before placing a trade.

Testing and risk control suggestions

  • First verify the default parameters in the strategy tester or demo account, and then adjust the symbol, period, spread and trading period one by one.
  • Scalping, grid, martin, and high-frequency strategies should focus on observing maximum drawdowns, consecutive losses, slippage, and trading frequency.
  • It is not recommended to run any document directly on the real disk. At least observe the simulated disk performance for 2-4 weeks first.

FAQ

Can MT4 and MT5 be used interchangeably?
Usually cannot be used directly. EX4/MQ4 corresponds to MT4, EX5/MQ5 corresponds to MT5, and the source code needs to be compiled in the corresponding MetaEditor.

Is this resource guaranteed to be profitable?
cannot. EAs, indicators and scripts can only be used as trading tools, and the results depend on parameters, market environment, spreads, slippage and risk control.

What else can I continue to watch?
Forex EA download , foreign exchange indicator download , EA evaluation , MQL5 CodeBase .

English Search Notes

This page is also optimized for English search intent around MT5 to MT4 trading classes - MetaTrader 4 library . Related search terms include: MQL4 source code, MQL5 source code. Test any Forex EA, Expert Advisor, MT4/MT5 indicator, script or MQL source code with historical data and a demo account before live trading.

Verification code Refresh